The springmvc.xml configuration is as follows:
Except for all interfaces under sysFile and the loginUser interface under user, all other interfaces will be processed by the interceptor UserInterceptor
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <mvc:exclude-mapping path="/sysFile/**" /> <mvc:exclude-mapping path="/users/loginUser.do" /> <bean class="com.test.interceptor.UserInterceptor" /> </mvc:interceptor> </mvc:interceptors>
public class UserInterceptor extends HandlerInterceptorAdapter{ @Autowired private IUsersService usersService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { return false; }
//Get the incoming userId from the foreground String userId = request.getParameter("userId" );
//Get the token passed in by the foreground String token = request.getParameter("token" );
//Get user information from database according to userId UsersVo usersVo = usersService.findUserById(userId); if(usersVo == null){ return false;//拦截 }
//Compare the token passed in by the foreground with the token obtained from the data if (usersVo.getToken() == null || "".equals(usersVo.getToken()) || ! usersVo.getToken().equals(token )){ return false ;//Intercept }
//Do not intercept return true ; } }