SpringMVC拦截器实现-创新互联
自定义拦截器类
public class SessionInterceptor extends HandlerInterceptorAdapter {
public SessionInterceptor() {
// TODO Auto-generated constructor stub
}
private List excludedUrls;
//通过属性注册不需要过滤的url list
public void setExcludedUrls(List excludedUrls) {
this.excludedUrls = excludedUrls;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String requestUrl = request.getRequestURI();
//排除不需要过滤的URL
for(String url:excludedUrls) {
if(requestUrl.endsWith(url)) {
return true;
}
}
//获取当前的会话session
HttpSession session = request.getSession();
if(session.getAttribute("userid") == null) {
//若登录session过期或不存在就跳转到login页面
request.getRequestDispatcher("/login.jsp").forward(request, response);
return false;
}
return true;
}
}
springmvc-servlet.xml 拦截器注册
HandlerInterceptorAdapter不能拦截WEB-INF目录以外的jsp文件;若需拦截默认index.jsp;可以将index.jsp移动目录到WEB-INF下即可;
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
文章标题:SpringMVC拦截器实现-创新互联
本文网址:http://scgulin.cn/article/dscdis.html