webmvc configuration class:
@Configuration public class WebMvcConfig extends WebMvcConfigurationSupport{ @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/user/login"); } }
Note: registry.addinterceptor (interceptor class object). addPathPatterns("request path to be intercepted"). excludePathPatterns("request path to be released");
Interceptor class, here is login interceptor
public class LoginInterceptor implements HandlerInterceptor{ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preHandle(HttpServletRequest hServletRequest, HttpServletResponse hServletResponse, Object object) throws Exception { // The case uses session to store the login user, so the login user is obtained from session
HttpSession session = hServletRequest.getSession(); TUser tUser=(TUser)session.getAttribute("user");
// Determine whether the user can be removed from the session if(tUser==null) { returnErrorMessage(hServletResponse, "Current operation requires user login first"); return false; } return true; } private void returnErrorMessage(HttpServletResponse response, String errorMessage) throws IOException { response.setCharacterEncoding("utf-8"); Map<String, Object> var=new HashMap<>(); var.put("success", false); var.put("errorMessage", errorMessage); response.setContentType("application/json"); PrintWriter out = response.getWriter(); ObjectMapper mapper = new ObjectMapper(); String jsonOfRST =mapper.writeValueAsString(var); out.print(jsonOfRST); out.flush(); } }
The simple login interception is implemented here, but the above output stream obtained by response object is used to write error information, and the following user-defined exception is used to handle it
Exception class
public class UnloggedException extends RuntimeException{ private static final long serialVersionUID = 181074719716690931L; public UnloggedException() { super("Current operation requires login first"); } }
Modify the preHandle method of the interceptor class, so the returnErrorMessage method can be removed
@Override public boolean preHandle(HttpServletRequest hServletRequest, HttpServletResponse hServletResponse, Object object) throws Exception { HttpSession session = hServletRequest.getSession(); TUser tUser=(TUser)session.getAttribute("user"); if(tUser==null) { throw new UnloggedException(); } return true; }
Finally, configure exception handling class
@ControllerAdvice @ResponseBody public class GlobalExceptionHandler { @ExceptionHandler public JSONObject handleException(Exception e) { return ActionHelper.responseFailed(e.getMessage()); } }
The above return value and ActionHelper are the return tool classes I used. They rely on fastjson of com.alibaba. For the version, refer to 1.2.49.
You can modify the return method and return value according to the actual situation.
After that, all exceptions can be handled by the exception handling class in the way of user-defined exceptions to return results.