请问,java高手,spring mvc拦截器如何拦截所有的请求啊,包括html和jsp页面?
目前配置的只能拦截controller的请求。我配置如下:<mvc:interceptors><mvc:interceptor><mvc:mappingpath="/**...
目前配置的只能拦截controller的请求。
我配置如下:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="loginInterceptor" class="com.blibao.dianping.web.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
----
/**
* 登陆拦截器.
*
* @author leizhimin 2014/6/26 16:08
*/
public class LoginInterceptor extends HandlerInterceptorAdapter {
// private static final String[] IGNORE_URI = {"/login.jsp", "/Login/","backui/","frontui/"};
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURL().toString();
boolean flag = false;
System.out.println(">>>: " + url);
/* for (String s : IGNORE_URI) {
if (url.contains(s)) {
flag = true;
break;
}
}
if (!flag) {
T_supplier_user user = LoginController.getLoginUser(request);
if (user != null) flag = true;
}*/
//跳转登录
if(url.contains("user/toLogin.do")){
return true;
}
request.getRequestDispatcher("/user/toLogin.do").forward(request, response);
return flag;
} 展开
我配置如下:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="loginInterceptor" class="com.blibao.dianping.web.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
----
/**
* 登陆拦截器.
*
* @author leizhimin 2014/6/26 16:08
*/
public class LoginInterceptor extends HandlerInterceptorAdapter {
// private static final String[] IGNORE_URI = {"/login.jsp", "/Login/","backui/","frontui/"};
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURL().toString();
boolean flag = false;
System.out.println(">>>: " + url);
/* for (String s : IGNORE_URI) {
if (url.contains(s)) {
flag = true;
break;
}
}
if (!flag) {
T_supplier_user user = LoginController.getLoginUser(request);
if (user != null) flag = true;
}*/
//跳转登录
if(url.contains("user/toLogin.do")){
return true;
}
request.getRequestDispatcher("/user/toLogin.do").forward(request, response);
return flag;
} 展开
6个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询