最近使用vue做前后端分离的项目,每次重启服务或者session失效之后,导致Security 302 到登录页面,而且前端无法获取到302的状态,没法拦截后自己跳转,所以需要我们返回Json;
首先我们需要实现AuthenticationEntryPoint接口,重写它的commence方法,其中 RespBean.noPass 只是我封装的工具类,在这里直接放想要返回的json就可以了;
代码:
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.setStatus(200); response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); RespBean error = RespBean.noPass("尚未登录,请登录!"); out.write(new ObjectMapper().writeValueAsString(