无法在 Spring 引导中访问不安全的端点
Cannot access to unsecured endpoints in Spring Boot
在我的控制器中,我有两个端点,一个是安全的,一个是 public:
@GetMapping("/public")
public String getPublic() {
return "public";
}
@PreAuthorize("hasRole('USER')")
@GetMapping("/private")
public String getPrivate() {
return "public";
}
安全端点仅在我登录并且具有正确角色的令牌放入请求时才起作用header。但是当我想在没有令牌的情况下访问 public 端点时,我总是得到状态 401,错误
Full authentication is required to access this resource
这是我的安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests().anyRequest().authenticated()
.and()
.csrf().disable();
}
}
和授权服务器配置:
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private final UserDetailsService appUserDetailService;
private final AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.tokenStore(tokenStore())
.tokenEnhancer(tokenEnhancer())
.authenticationManager(authenticationManager)
.userDetailsService(appUserDetailService);
}
}
我也尝试将 .authorizeRequests().anyRequest().authenticated()
更改为:.authorizeRequests().anyRequest().permitAll()
,但没有任何更改。我的首选方法是使用注释处理安全性。谢谢。
antMatchers() 可以解决问题。我们经常使用它。最好在不同的 class 中设置不安全的端点,并通过请求映射在 class 级别控制安全性。
antMatchers("/public").permitAll()
Link 到 spring 安全 api - https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/config/annotation/web/builders/HttpSecurity.html#antMatcher-java.lang.String-
你有两个选择,可以选择其中一个。
选项1:在你的端点,像这样改变。
@PreAuthorize("permitAll()")
@GetMapping("/public")
public String getPublic() {
return "public";
}
然后改变你的configure(HttpSecurity http)
方法,像这样。
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.anyRequest().permitAll()
.and()
.csrf().disable();
}
选项 2:在您的 configure(HttpSecurity http)
方法中,只需这样做即可。
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/public").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable();
}
在我的控制器中,我有两个端点,一个是安全的,一个是 public:
@GetMapping("/public")
public String getPublic() {
return "public";
}
@PreAuthorize("hasRole('USER')")
@GetMapping("/private")
public String getPrivate() {
return "public";
}
安全端点仅在我登录并且具有正确角色的令牌放入请求时才起作用header。但是当我想在没有令牌的情况下访问 public 端点时,我总是得到状态 401,错误
Full authentication is required to access this resource
这是我的安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests().anyRequest().authenticated()
.and()
.csrf().disable();
}
}
和授权服务器配置:
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private final UserDetailsService appUserDetailService;
private final AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.tokenStore(tokenStore())
.tokenEnhancer(tokenEnhancer())
.authenticationManager(authenticationManager)
.userDetailsService(appUserDetailService);
}
}
我也尝试将 .authorizeRequests().anyRequest().authenticated()
更改为:.authorizeRequests().anyRequest().permitAll()
,但没有任何更改。我的首选方法是使用注释处理安全性。谢谢。
antMatchers() 可以解决问题。我们经常使用它。最好在不同的 class 中设置不安全的端点,并通过请求映射在 class 级别控制安全性。
antMatchers("/public").permitAll()
Link 到 spring 安全 api - https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/config/annotation/web/builders/HttpSecurity.html#antMatcher-java.lang.String-
你有两个选择,可以选择其中一个。
选项1:在你的端点,像这样改变。
@PreAuthorize("permitAll()")
@GetMapping("/public")
public String getPublic() {
return "public";
}
然后改变你的configure(HttpSecurity http)
方法,像这样。
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.anyRequest().permitAll()
.and()
.csrf().disable();
}
选项 2:在您的 configure(HttpSecurity http)
方法中,只需这样做即可。
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/public").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable();
}