- HttpServletRequest httpRequest=(HttpServletRequest)request;
- String strBackUrl = "http://" + request.getServerName() //服务器地址
- + ":"
- + request.getServerPort() //端口号
- + httpRequest.getContextPath() //项目名称
- + httpRequest.getServletPath() //请求页面或其他地址
- + "?" + (httpRequest.getQueryString()); //参数
public class LoginInterceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
String contextPath=arg0.getContextPath();
HttpSession session = arg0.getSession();
User user = (User) session.getAttribute("user");
if (null ==user || "".equals(user)) {
arg1.sendRedirect(contextPath+"/user/login.do");
return false;
}
return true;
}
}
<!-- 拦截器 -->
<mvc:interceptors>
<!-- 多个拦截器,顺序执行 -->
<!-- 登录认证拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/user/index.do" />
<mvc:exclude-mapping path="/user/login.do" />
<bean class="cn.wujinkuaibao.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>