Spring 使用 RequestHeaderMatcher 的引导安全 PermitAll 不工作
Spring Boot Security PermitAll with RequestHeaderMatcher not working
在遵循 Maven Spring Boot 中有关微服务和 OAuth2 的教程后,我遇到了问题。我想从身份验证中排除请求,因此可以获得未经授权的数据。这似乎并不像我那样工作。有人可以帮我解决这个问题吗?
我尝试了什么:
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new RequestHeaderRequestMatcher("Authorization"))
.authorizeRequests()
.antMatchers("/**").authenticated()
.and()
.authorizeRequests()
.andMatchers(HttpMethod.GET, "/beers").permitAll();
}
}
我尝试执行请求时必须进行身份验证。我该如何解决?
spring-security-oauth2-自动配置:2.1.1.RELEASE
首先,您的配置与以下相同。只需删除那些不必要的重复 authorizeRequests()
和 and()
,这样看起来更清晰:
http.requestMatcher(new RequestHeaderRequestMatcher("Authorization"))
.authorizeRequests()
.antMatchers("/**").authenticated()
.andMatchers(HttpMethod.GET, "/beers").permitAll();
这意味着 spring 安全将仅处理具有 Authorization
header 的请求。否则,该请求将被忽略,并且 spring 安全性内容不会应用到它。
所以如果请求有 Authorization
header ,它就会开始检查规则(即那些由 authorizeRequests()
配置的匹配器的东西)从上到下根据到声明 order.Once 一条规则被匹配,底部的规则将被忽略并且不会被检查。
由于您的第一个规则是匹配每个 HTTP 请求(“/**”),这使得它下面的所有规则永远不会执行并且没有任何意义。
另一方面,如果您希望 spring 安全性完全忽略“/beers”,即使它的请求有 Authorization
header,您应该配置 WebSecurity
以忽略它:
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(HttpMethod.GET, "/beers");
}
在遵循 Maven Spring Boot 中有关微服务和 OAuth2 的教程后,我遇到了问题。我想从身份验证中排除请求,因此可以获得未经授权的数据。这似乎并不像我那样工作。有人可以帮我解决这个问题吗?
我尝试了什么:
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new RequestHeaderRequestMatcher("Authorization"))
.authorizeRequests()
.antMatchers("/**").authenticated()
.and()
.authorizeRequests()
.andMatchers(HttpMethod.GET, "/beers").permitAll();
}
}
我尝试执行请求时必须进行身份验证。我该如何解决?
spring-security-oauth2-自动配置:2.1.1.RELEASE
首先,您的配置与以下相同。只需删除那些不必要的重复 authorizeRequests()
和 and()
,这样看起来更清晰:
http.requestMatcher(new RequestHeaderRequestMatcher("Authorization"))
.authorizeRequests()
.antMatchers("/**").authenticated()
.andMatchers(HttpMethod.GET, "/beers").permitAll();
这意味着 spring 安全将仅处理具有 Authorization
header 的请求。否则,该请求将被忽略,并且 spring 安全性内容不会应用到它。
所以如果请求有 Authorization
header ,它就会开始检查规则(即那些由 authorizeRequests()
配置的匹配器的东西)从上到下根据到声明 order.Once 一条规则被匹配,底部的规则将被忽略并且不会被检查。
由于您的第一个规则是匹配每个 HTTP 请求(“/**”),这使得它下面的所有规则永远不会执行并且没有任何意义。
另一方面,如果您希望 spring 安全性完全忽略“/beers”,即使它的请求有 Authorization
header,您应该配置 WebSecurity
以忽略它:
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(HttpMethod.GET, "/beers");
}