1.在springmvc的配置文件中

  1. <!-- 配置拦截器 -->
  2. <mvc:interceptors>
  3. <mvc:interceptor>
  4. <mvc:mapping path="/back/manager/**"/>
  5. <mvc:mapping path="/dev/flatform/**"/>
  6. <bean class="cn.appsys.interceptor.LoginInterceptor"/>
  7. </mvc:interceptor>
  8. </mvc:interceptors>

2.编写拦截器Java代码

  1. public class LoginInterceptor extends HandlerInterceptorAdapter {
  2. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  3. HttpSession session = request.getSession();
  4. BackendUser backendUser = (BackendUser) session.getAttribute(Constants.USER_SESSION);
  5. DevUser devUser = (DevUser) session.getAttribute(Constants.DEV_USER_SESSION);
  6. if (null != devUser) {
  7. return true;
  8. } else if (null != backendUser) {
  9. return true;
  10. } else {
  11. response.sendRedirect(request.getContextPath() + "/403.jsp");
  12. return false;
  13. }
  14. }
  15. }