用Spring Boot 2代替403而不是403

使用Spring Boot 1.5.6.RELEASE我能够发送HTTP状态代码401而不是403 ,如下所示: 如果请求uri未经身份validation , Spring Spring 安全响应未经授权(http 401代码) ,通过这样做:

 public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //... http.exceptionHandling() .authenticationEntryPoint(new Http401AuthenticationEntryPoint("myHeader")); //... } } 

使用org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint类。

我刚刚升级到Spring Boot 2.0.0.RELEASE ,发现不再有这样的类了(至少在那个包中)。

问: Spring Boot中是否存在此类( Http401AuthenticationEntryPoint )? 如果不是,那么在现有项目中保持相同行为以保持与依赖于此状态代码( 401 )而不是403其他实现的一致性的403什么?

删除了类org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint ,转而使用org.springframework.security.web.authentication.HttpStatusEntryPoint

在我的情况下,代码将是这样的:

 public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //... http.exceptionHandling() .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)); //... } }