拦截器
在web.xml中配置
<!--
欢迎页面
当访问http://127.0.0.1:8080/crm时,会自动找到index.jsp
-->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
在index.jsp中配置
<!--跳转到登录页面-->
<script type="text/javascript">
document.location.href = "settings/user/toLogin.do";
</script>
在applicationContext-web.xml中配置
<!--
权限控制的拦截器
-->
<mvc:interceptors>
<mvc:interceptor>
<!--对所有的请求都拦截-->
<mvc:mapping path="/**"/>
<!--对登录请求放行-->
<mvc:exclude-mapping path="/settings/user/login.do"/>
<!--对跳转到登录页面的请求放行-->
<mvc:exclude-mapping path="/settings/user/toLogin.do"/>
<!--注册的拦截器-->
<bean class="com.mryang.crm.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
在拦截器中设置
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
User user = (User) request.getSession().getAttribute("user");
if (user == null){
throw new InterceptorException();
}
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}