使用注释和全局denyAll的Spring安全白名单方法
目前,我试图弄清楚Spring安全性如何评估给定的URL、表达式和注释。到目前为止,它似乎总是先检查使用注释和全局denyAll的Spring安全白名单方法,spring,spring-mvc,spring-security,annotations,whitelist,Spring,Spring Mvc,Spring Security,Annotations,Whitelist,目前,我试图弄清楚Spring安全性如何评估给定的URL、表达式和注释。到目前为止,它似乎总是先检查security context.xml中的条目。如果这是一个denyAll,它将停止对请求的进一步处理 也许我忘了设置一些配置选项,但是(在我看来)不可能使用Spring Security的注释(比如@Secured,@PermitAll,等等)构建一个好的白名单 我想要的基本上是注释@Controller中允许访问的方法。例如: @Controller @RequestMapping("/te
security context.xml
中的条目。如果这是一个denyAll
,它将停止对请求的进一步处理
也许我忘了设置一些配置选项,但是(在我看来)不可能使用Spring Security的注释(比如@Secured
,@PermitAll
,等等)构建一个好的白名单
我想要的基本上是注释@Controller
中允许访问的方法。例如:
@Controller
@RequestMapping("/test")
public MyController {
@RequestMapping("")
public void tryToGetSomething() {
// no security annotation -> denyAll
}
@RequestMapping("/public")
@PermitAll
public void tryToGetSomethingPublic() {
// this will always have access allowed
}
@RequestMapping("/admin")
@Secured({"ROLE_ADMIN"})
public void tryToGetSomethingReallyImportant() {
// this can only be accessed by admins
}
}
这种方法的主要原因是:安全性;-)代码>。在编写代码时,总是可能忘记一些注释。使用这种方法,这样的错误不会影响敏感数据的安全性
所以我的问题是:如何实现这一点?您可以尝试将安全切入点与注释结合使用:
@控制器
@请求映射(“/test”)
公共MyController{
@请求映射(“”)
public void tryToGetSomething(){
//切入点规则->没有人有角色\u但\u不存在->没有人可以调用此代码
}
@请求映射(“/public”)
@预授权(“许可”)
public void trytogetsomething public(){
//注释优先于切入点,因此根据@PreAuthorized(“permitAll”)规则,任何人都可以调用此代码
}
}
见官方文件。也许您可以使用denyAll
而不是不存在的ROLE\u
希望这能有所帮助。我也试图实现同样的效果,但问题是方法安全级别适用于通过AOP调用的每个方法。如果默认情况下拒绝访问,则必须对几乎所有内容进行注释:)
使用基于URL的安全性,您可以通过白名单继续:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.anyRequest().denyAll();
}
不幸的是,明显的缺点是每个URL都必须在这里授权,这会产生一种依赖磁铁。但是,集中URL路径映射可能是一件好事?这看起来很有趣,我来检查一下。但首先,我必须了解如何在SpringSecurityJavaConfig中使用切入点。我待会再报告,你准备好了吗?