概述
xml配置文件过滤器内容
<!--登录拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/**/fonts/**"/>
<mvc:exclude-mapping path="/**/css/**"/>
<mvc:exclude-mapping path="/**/images/**"/>
<mvc:exclude-mapping path="/**/js/**"/>
<mvc:exclude-mapping path="/**/css/**"/>
<mvc:exclude-mapping path="/**/login.html"/>
<mvc:exclude-mapping path="/**/lib/**"/>
<mvc:exclude-mapping path="/background/login.do"/>
<mvc:exclude-mapping path="/background//loginpwd.do"/>
<bean class="com.qy.controller.LoginIncerteptor"/>
</mvc:interceptor>
</mvc:interceptors>
<mvc:mapping path> 表示要拦截的路径 path中写被 拦截的路径
<mvc:exclude-mapping path>表示不被拦截的路径 path中写不被拦截的路径
<bean class>关联的类class中填写类的全限定名称
LoginIncerteptor.java文件
package com.qy.controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginIncerteptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
Object user = httpServletRequest.getSession().getAttribute("user");
String path =httpServletRequest.getContextPath();
String bassPath = httpServletRequest.getScheme()+"://"+httpServletRequest.getServerName()+":"+httpServletRequest.getServerPort()+path+"/";
if (user == null){
System.out.println("尚未登录,调到登录页面");
httpServletResponse.sendRedirect(bassPath+"background/login.html");
return false;
}
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 {
}
}
如果登录成功将用户名和密码保存在session中 然后再LoginIncerteptor类中的继承HandlerIntercepter 重写preHandel方法
在方法中获取session中的数据,如果数据为空跳到登录页面,否则继续访问
最后
以上就是斯文花卷为你收集整理的springmvc过滤器的全部内容,希望文章能够帮你解决springmvc过滤器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复