微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Spring Boot 2的java – 401而不是403

使用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,发现不再有这样的课程了(至少在那个包中).

问:
Spring Boot中是否存在此类(Http401AuthenticationEntryPoint)?如果不是,那么在现有项目中保持相同行为以保持与依赖于此状态代码(401)而不是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));
        //...
    }
}

原文地址:https://www.jb51.cc/spring/431708.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐