401 instead of 403 with Spring Boot 2

前端 未结 4 882
情歌与酒
情歌与酒 2020-12-09 17:49

With Spring Boot 1.5.6.RELEASE I was able to send HTTP Status code 401 instead of 403 as described in How let spring security response una

4条回答
  •  离开以前
    2020-12-09 18:38

    Just to elaborate @lealceldeiro's answer:

    Before Spring Boot 2 my Securiy Configuration class looked like this:

    @Configuration
    public class MyConfig extends WebSecurityConfigurerAdapter {
    
        @Bean
        public Http401AuthenticationEntryPoint securityException401EntryPoint() {
          return new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\"");
        }
    
        @Autowired
        private Http401AuthenticationEntryPoint authEntrypoint;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
          // some http configuration ...
    
          // Spring Boot 1.5.x style
          http.exceptionHandling().authenticationEntryPoint(authEntrypoint);
        }
    //...
    }
    

    And now in Spring Boot 2 it looks like this:

    @Configuration
    public class MyConfig extends WebSecurityConfigurerAdapter {
    
        //Bean configuration for Http401AuthenticationEntryPoint can be removed
    
        //Autowiring also removed
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
          // some http configuration ...
    
          // Spring Boot 2 style
          http.exceptionHandling().authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));
        }
    //...
    }
    

    See also this comment in Spring Boot Github Repo > PR Remove Http401AuthenticationEntryPoint.

提交回复
热议问题