SpringMVC的HandlerInterceptor
package com.xs.demo.inteceptor;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.xs.demo.entity.Userinfo;
import com.xs.demo.util.Constants;
public class PermissionsInteceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("最后执行");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("第二步执行");
}
public boolean isTimeOut(long old) {
long now = new Date().getTime();
long maxTime = Long.parseLong(Constants.SESSIONOUTTIME);
if(now - old > maxTime*1000){
return true;
}else{
return false;
}
}
/**
* 控制页面的访问
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
System.out.println("第一步");
String path = request.getServletPath();
if(path.startsWith("/user/")){
Userinfo userinfo = (Userinfo) request.getSession().getAttribute(Constants.LOGIN_INFO);
if(null == userinfo && !path.startsWith("/user/gotoAdd/")){
response.sendRedirect(request.getContextPath()+"/system/login.jsp");
return true;
}else{
return false;
}
}
System.out.println(handler.getClass().getName());
return true;
}
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.xs.demo.entity.Userinfo;
import com.xs.demo.util.Constants;
public class PermissionsInteceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("最后执行");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("第二步执行");
}
public boolean isTimeOut(long old) {
long now = new Date().getTime();
long maxTime = Long.parseLong(Constants.SESSIONOUTTIME);
if(now - old > maxTime*1000){
return true;
}else{
return false;
}
}
/**
* 控制页面的访问
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
System.out.println("第一步");
String path = request.getServletPath();
if(path.startsWith("/user/")){
Userinfo userinfo = (Userinfo) request.getSession().getAttribute(Constants.LOGIN_INFO);
if(null == userinfo && !path.startsWith("/user/gotoAdd/")){
response.sendRedirect(request.getContextPath()+"/system/login.jsp");
return true;
}else{
return false;
}
}
System.out.println(handler.getClass().getName());
return true;
}
}
这个是验证是否登录的拦截器。但是当我登录以后。走的拦截只走一步。页面显示空白
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就这个还没搞清楚。谢谢你的建议
使用 WebRequestInterceptor接口~
原来是自己写错了