import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.IOException;
/**
* 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面
* 配置参数
* checkSessionKey 需检查的在 Session 中保存的关键字
* redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath
* notCheckURLList 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath
*/
public class CheckLoginFilter implements Filter {
protected FilterConfig filterConfig = null;
private String redirectURL = null;
private List notCheckURLList = new ArrayList();
private String sessionKey = null;
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
if (sessionKey == null) {
filterChain.doFilter(request, response);
return;
}
if ((!checkRequestURIIntNotFilterList(request))
&& session.getAttribute(sessionKey) == null) {
response.sendRedirect(request.getContextPath() + redirectURL);
return;
}
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy() {
notCheckURLList.clear();
}
private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {
String uri = request.getServletPath()
+ (request.getPathInfo() == null ? "" : request.getPathInfo());
return notCheckURLList.contains(uri);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
redirectURL = filterConfig.getInitParameter("redirectURL");
sessionKey = filterConfig.getInitParameter("checkSessionKey");
String notCheckURLListStr = filterConfig
.getInitParameter("notCheckURLList");
if (notCheckURLListStr != null) {
StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");
notCheckURLList.clear();
while (st.hasMoreTokens()) {
notCheckURLList.add(st.nextToken());
}
}
}
}
分享到:
相关推荐
登录拦截器(通用版)
理解拦截器 1.1. 什么是拦截器: 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。 在Webwork的中文文档的解释...
Jfinal通过拦截器实现登录验证,要访问控制层的方法必须先登录通过验证
一个简单的基于java SSM框架的登录拦截器demo,里面有非常详细的代码,简单易懂,方便初次接触拦截器的人使用
使用spring+struts2+hibernate实现简单的登录拦截器。实现登录。用户注销功能。单表增删查实现分页功能
例如通过拦截器可以进行权限验证、记录请求信息的日志、判断用户是否登录等。 15.1.1 拦截器的定义 要使用Spring MVC中的拦截器,就需要对拦截器类进行定义和配置。通常拦截器类可以通过两种方式来定义。 通过实现...
初学者很容易将 Spring MVC 拦截器 和 Spring Cloud OpenFeign 拦截器搞混,误以为OpenFeign拦截器就是SpringMVC拦截器:Spring MVC拦截器发生在客户端 和 服务端之间,在客户端向服务端发送请求时进行拦截处理。...
SpringBoot 简单登录功能 拦截器实现。 在实际开发中,我们经常会遇到某些页面需要登录才能查看的情况。下面使用拦截器实现该功能,在用户没有登录的情况下,将网站的所有访问都指向登录页面。 Springboot 登录
简单的通过SSM技术利用拦截器实现登录拦截,
SpringBoot整合Mybatis完整详细版含注册、登录、拦截器配置功能
Springmvc框架进行登录验证,除了某些特定的action请求不需要登录验证,例如登录,注册等。其他的action请求均需要登录验证,若没有登录则直接调到登录界面。若登录成功,则通过拦截
uni-app 请求拦截器
基于Springboot前后端分离式,利用拦截器完成登录拦截的demo ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信...
该压缩包实现了利用过滤器或者拦截器对登录信息进行验证跳转登陆页的功能,利用的是SpringBoot和thymeleaf,使用前请先看使用说明
spring boot jpa连接数据库,设置拦截器拦截指定路径下的文件。
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
mybatis mysql 分页拦截器,可以针对方法名称,或指定的参数进行拦截
spring boot简单应用,配置yml文件,实现mysql数据库的连接,对数据进行增删改查,另有登录拦截器。
spring-boot 拦截器 登录拦截spring-boot 拦截器 登录拦截spring-boot 拦截器 登录拦截spring-boot 拦截器 登录拦截
JavaEE 拦截器用户登录案例源代码