1、写一个拦截器的类实现filter
2、配置在web.xml配置文件
3、在dofilter中写入需要拦截的内容、与放行的内容
代码:
one:配置的web.xml代码:
SessionFilter
com.qf.filter.LoginFilter
SessionFilter
/*
two:实现类的代码:
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpSession session = request.getSession();
//修改为自己塞入session内的用户的信息值、如果有代表登录了,如果没有代表没登录需要拦截
Object obj = request.getSession().getAttribute(“userInfo”);
//获取连接路径uri,解锁不需要拦截的样式资源
String uri = request.getRequestURI();
//判断uri包含的内容,登录页面不进行拦截,资源加载不进行拦截
if(uri.contains(“login”) || uri.contains(“before”) || uri.contains("/css/") || uri.contains("/js/") || uri.contains("/fonts/") || uri.contains(“img”)){
filterChain.doFilter(request, response);
}else{
if(null != obj && !obj.equals("")){
filterChain.doFilter(request, response);
}else{
//没有登录过、返回登录页面
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}
}//dofiter
@Override
public void destroy() {
}
}
ssm框架中filter的用法、拦截器
最新推荐文章于 2022-07-18 22:05:41 发布