使用Spring Boot 1.5.6.RELEASE我能够发送HTTP状态代码 401
而不是 403
,如How let spring security response unauthorized(http 401 code) if requesting uri without authentication中所述,通过这样做:
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,发现不再有这样的类了(至少在那个包中) .
Q: Spring Boot中是否存在此类( Http401AuthenticationEntryPoint
)?如果不是,那么在现有项目中保持相同行为以保持与依赖于此状态代码( 401
)而不是 403
的其他实现的一致性可能是一个很好的替代方法?
4 回答
删除了
org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint
类,以支持org.springframework.security.web.authentication.HttpStatusEntryPoint
.在我的情况下,代码将是这样的:
Http401AuthenticationEntryPoint已删除,请参阅10715:
根据您的要求,您可以使用:
HttpStatusEntryPoint
BasicAuthenticationEntryPoint
只是为了详细说明@ lealceldeiro的答案:
在Spring Boot 2之前,我的Securiy Configuration类看起来像这样:
现在在Spring Boot 2中它看起来像这样:
另见:https://github.com/spring-projects/spring-boot/issues/10715#issuecomment-363592444
您可以通过覆盖AuthenticationEntryPoint类来自定义您的逻辑,这应该是正常的: