我公开了这样的Spring REST服务。
@RequestMapping(value = "/{id}", method = RequestMethod.GET, headers = Constant.ACCEPT_APPLICATION_JSON)
@ResponseBody
public IndividualProviderDto showJson(@PathVariable("id") Long id) {
IndividualProviderDto individualProviderDto = individualProviderService.findIndividualProvider(id);
if (SecurityUtils.getCurrentLogin().equals(individualProviderDto.getUserName())) {
individualProviderDto.setCredential("");
HttpHeaders headers = new HttpHeaders();
headers.add(Constant.CONTENT_TYPE, Constant.APPLICATION_JSON_CHARSET_UTF8);
return individualProviderDto;
}
throw new IllegalArgumentException("User not found");
}
在上面的代码中,我明确进行检查以确保ID属于登录的USER。
SecurityUtils.getCurrentLogin().equals(individualProviderDto.getUserName()
无论何时需要保护资源,都必须应用此检查。当然,我可以拥有一个Aspect并使用切入点表达式从一个地方应用它。我也听说过伪造网址的ESAPI
但是我想知道Spring Security Configuration是否提供了一些开箱即用的东西,这样我就不会重新发明轮子了。
最佳答案
Spring安全性不是解决方案-它提供了进行身份验证和授权的工具。检查特定实体(在您的情况下为id
)的可用性应该是服务层的一部分,因为它已在代码中实现。