Java 允许少数不进行身份验证的请求,并对剩余的所有请求进行身份验证
我已使用以下http配置配置了我的资源服务器。基本上,我想设置一些配置,比如允许一些没有身份验证的请求,并且只有当用户拥有有效的访问令牌时,才允许所有其他我没有指定的请求 注意,我使用的是SpringSecurityOAuth2和SpringBootJava 允许少数不进行身份验证的请求,并对剩余的所有请求进行身份验证,java,spring,authentication,spring-security,spring-security-oauth2,Java,Spring,Authentication,Spring Security,Spring Security Oauth2,我已使用以下http配置配置了我的资源服务器。基本上,我想设置一些配置,比如允许一些没有身份验证的请求,并且只有当用户拥有有效的访问令牌时,才允许所有其他我没有指定的请求 注意,我使用的是SpringSecurityOAuth2和SpringBoot @Override public void configure(HttpSecurity http) throws Exception { http .exceptionHandling()
@Override
public void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.and()
.csrf()
.requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
.disable()
.headers()
.frameOptions().disable().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.antMatcher("**")
.authorizeRequests()
.antMatchers("/hello/")
.permitAll()
.anyRequest()
.authenticated();
}
当我尝试访问http://localhost:8889/hello
,即使我已将其配置为permitAll,它仍会给出401以下未经授权的响应
{
"error": "unauthorized",
"error_description": "An Authentication object was not found in the SecurityContext"
}
相反,我检查了以下配置
.authorizeRequests()
.antMatchers("/hello/").permitAll()
.antMatchers("/hello2/").permitAll()
.antMatchers("/secure/**").authenticated()
.antMatchers("/secure2/**").authenticated();
它工作得很好,就像hello和hello2在没有令牌的情况下被允许一样,如果令牌未传递或传递了无效令牌,则secure和secure2会给出错误响应
有人能告诉我我遗漏了什么吗?问题是,当您不这样访问antmatcher时,它以斜线结尾。我厌倦了“”,但面临相同的错误。我还尝试更改为.antMatchers(“/hello”)并访问“”,但再次出现相同的错误。