Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用注释和全局denyAll的Spring安全白名单方法_Spring_Spring Mvc_Spring Security_Annotations_Whitelist - Fatal编程技术网

使用注释和全局denyAll的Spring安全白名单方法

使用注释和全局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

目前,我试图弄清楚Spring安全性如何评估给定的URL、表达式和注释。到目前为止,它似乎总是先检查
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中使用切入点。我待会再报告,你准备好了吗?