【发布时间】:2019-08-30 11:08:04
【问题描述】:
我有一个使用自定义 AccessDecisionVoter 的 Spring Web 应用程序。这个自定义的决策投票者将找到访问 URL 所需的权限,然后检查登录用户是否授予了所需的权限。
如果登录用户没有授予所需的权限,则此自定义决策投票者应返回 ACCESS_DENIED,否则应返回 ACCESS_GRANTED。
现在的问题是:当用户尝试访问他没有权限的 URL 时,应用服务器会出现 HTTP 405。请注意,当用户通过 GET 方法访问 URL 时(例如,在浏览器中输入 URL地址栏),他会得到 HTTP 403。HTTP 405 只发生在 POST 方法中。请注意,我的 spring-mvc 控制器不限制 HTTP 方法。
根据我的日志文件,我确认决策投票者正在返回 ACCESS_DENIED (-1)。不知何故,我的浏览器刚刚收到了 HTTP 405。
我正在使用 spring-security 5.0.1
以下是我的代码:
我的自定义决策投票者:
@Override
public int vote(Authentication authentication, Object object, Collection securityConfigs) {
logger.debug("Authorization in progress");
if (authentication == null) {
logger.info("No authentication. Access Denied.");
return ACCESS_DENIED;
}
if (securityConfigs.size() == 0) {
logger.info("No matching Page Config found for the given URL. Access Denied.");
return ACCESS_DENIED;
}
int result = ACCESS_ABSTAIN;
Set<String> authorities = extractAuthorities(authentication);
String username = authentication.getName().toUpperCase();
logger.debug("authentication.getName() = " + username);
for (Object configObject : securityConfigs) {
SecurityConfig config = (SecurityConfig) configObject;
if (this.supports(config.getAttribute())) {
result = ACCESS_DENIED;
String attributeUpperCase = config.getAttribute().toUpperCase();
logger.debug("config attribute = " + attributeUpperCase);
if (authorities.contains(attributeUpperCase)) {
logger.info("The request url has config attribute that matches the login user's granted Master Function Code. Access Granted. The matching config attribute = " + attributeUpperCase);
return ACCESS_GRANTED;
}
}
}
logger.info("Voting Result from DaxVoter = " + result);
return result;
}
我的控制器:
@ResponseBody
@RequestMapping(value ="/road/retrieveRoad.do")
public Map<String, Object> retrieveRoad(HttpServletRequest request, @RequestBody DataParamsBean dataParams) {
logger.info("CommonSupportCtrl | retrieveRoad | Start");
Map<String, Object> resultMap = new HashMap<String, Object>();
int start = dataParams.getSkip();
int limit = (dataParams.getTake() == 0) ? 10 : (int) dataParams.getTake();
String sortBy = (dataParams.getSorted() == null) ? null : (String) dataParams.getSorted().get(0).get("name");
String sortDirection = (dataParams.getSorted() == null) ? null : (String) dataParams.getSorted().get(0).get("direction");
String roadCode = dataParams.getParams().get("id") == null ? null : (String) dataParams.getParams().get("id");
String roadName = dataParams.getParams().get("roadName") == null ? null : (String) dataParams.getParams().get("roadName");
if(sortDirection != null) {
if(sortDirection.equalsIgnoreCase("ascending")) {
sortDirection = "asc";
} else {
sortDirection = "desc";
}
}
GenericSearchResults<RoadBean> searchResults = commonSupportService.retrieveRoadByCriteria(roadName, roadCode, start, limit,
sortBy, sortDirection);
resultMap.put("result", searchResults.getResult());
resultMap.put("count", searchResults.getCount());
logger.info("CommonSupportCtrl | retrieveRoad | End");
return resultMap;
}
【问题讨论】:
标签: java spring-mvc spring-security