实现 AuthenticationEntryPoint
@Component
public class AjaxAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
AjaxResponseBody responseBody = new AjaxResponseBody();
responseBody.setStatus("000");
responseBody.setMsg("Need Authorities!");
httpServletResponse.getWriter().write(JSON.toJSONString(responseBody));
}
}
在 SpringSecurityConf 中:
//异常处理(权限拒绝、登录失效等)
.and().exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint);//匿名用户访问无权限资源时的异常处理;