Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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
Java Spring Security即使使用@PreAuthorize(";permitAll()";)也会阻止匿名请求_Java_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Java Spring Security即使使用@PreAuthorize(";permitAll()";)也会阻止匿名请求

Java Spring Security即使使用@PreAuthorize(";permitAll()";)也会阻止匿名请求,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我有很多API端点需要经过身份验证的请求,还有一些端点允许任何请求。我希望Spring Security在默认情况下阻止匿名请求,但允许我覆盖它: aHttpSecurity.authorizeRequests().anyRequest().authorized()) ... @预授权(“permitAll()”) @请求映射(“/foobar”) 公众反应{ ... } 不幸的是,这不起作用:/foobar输出401。知道怎么做吗?@PreAuthorize只是一个注释,它包装了方法,以检查

我有很多API端点需要经过身份验证的请求,还有一些端点允许任何请求。我希望Spring Security在默认情况下阻止匿名请求,但允许我覆盖它:


aHttpSecurity.authorizeRequests().anyRequest().authorized())
...
@预授权(“permitAll()”)
@请求映射(“/foobar”)
公众反应{
...
}

不幸的是,这不起作用:
/foobar
输出401。知道怎么做吗?

@PreAuthorize
只是一个注释,它包装了方法,以检查用户是否可以执行带注释的方法。它不仅适用于控制器

当您有http请求时,首先请求会通过一些spring安全过滤器,当您编写
.anyRequest().authenticated()
时,您不会转到某个包装好的控制器端点

所以,如果你有几个端点,你可以排除它


aHttpSecurity.authorizeRequests()
.antMatchers(HttpMethod.GET,“/foobar/**”).permitAll()
.antMatchers(“/**”).authenticated()


和delete
@PreAuthorize(“permitAll()”)

Spring安全性有两个级别的安全性

过滤器级别和方法级别

过滤器级别将与URL一起工作,如果URL未配置为可访问,则将相应地使用401或403拒绝访问。这是由


方法级别通常用作第二级防御,以授权谁可以访问方法以及他/她可以操作哪些操作或对象。这由aHttpSecurity.authorizeRequests().anyRequest().authorized()处理。

这将在请求到达带注释的方法之前阻止请求@AliDehghani是的,这也是我注意到的,问题是:如何改变这种行为,使
@PreAuthorize
具有更高的优先级?