1.在springmvc的配置文件中
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/back/manager/**"/>
<mvc:mapping path="/dev/flatform/**"/>
<bean class="cn.appsys.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
2.编写拦截器Java代码
public class LoginInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
BackendUser backendUser = (BackendUser) session.getAttribute(Constants.USER_SESSION);
DevUser devUser = (DevUser) session.getAttribute(Constants.DEV_USER_SESSION);
if (null != devUser) {
return true;
} else if (null != backendUser) {
return true;
} else {
response.sendRedirect(request.getContextPath() + "/403.jsp");
return false;
}
}
}