401 instead of 403 with Spring Boot 2

前端 未结 4 906
情歌与酒
情歌与酒 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:23

    You can customize your logic with overriding the class AuthenticationEntryPoint this should be working:

    @Component public class AuthEntryPointException implements AuthenticationEntryPoint, Serializable {
    
        private static final long serialVersionUID = -8970718410437077606L;
    
        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException authException) throws IOException {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            response.setContentType("application/json");
            response.getWriter().write("{\"result\":\"UNAUTHORIZED\",\"message\":\"UNAUTHORIZED or Invalid Token\"}");
        }
    }
    

提交回复
热议问题