2019年7月1日 | Leave a comment 问题: 默认返回的 解决: public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.exceptionHandling().accessDeniedHandler(new CustomAccessDeniedHandler()).and() .exceptionHandling().authenticationEntryPoint(new CustomHttp403ForbiddenEntryPoint()); } public class CustomAccessDeniedHandler implements AccessDeniedHandler { @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException arg2) throws IOException, ServletException { response.getWriter().print("You don't have required role to perform this action."); } } public class CustomHttp403ForbiddenEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.getWriter().print("You need to login first in order to perform this action."); } } } 1234567891011121314151617181920212223242526272829 public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Overrideprotected void configure(HttpSecurity http) throws Exception { http.exceptionHandling().accessDeniedHandler(new CustomAccessDeniedHandler()).and() .exceptionHandling().authenticationEntryPoint(new CustomHttp403ForbiddenEntryPoint());} public class CustomAccessDeniedHandler implements AccessDeniedHandler { @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException arg2) throws IOException, ServletException { response.getWriter().print("You don't have required role to perform this action."); } } public class CustomHttp403ForbiddenEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.getWriter().print("You need to login first in order to perform this action."); } }} 参考: https://stackoverflow.com/questions/28057592/spring-boot-accessdeniedhandler-does-not-work https://blog.csdn.net/jmppok/article/details/44828829 https://blog.csdn.net/jiangshanwe/article/details/73234988