首页 > 代码库 > 利用Filter实现session拦截
利用Filter实现session拦截
1、在web.xml中配置
<!-- Session监听器 --> <filter> <filter-name>sessionValidateFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>sessionValidateFilter</filter-name> <url-pattern>/admin/*</url-pattern> </filter-mapping>
2、在applicationContent.xml中配置
<!-- 配置登录验证拦截器 --> <bean id="sessionValidateFilter" class="cn.jxufe.core.filter.SessionValidateFilter"></bean>
3、创建自定义过滤器类
public class SessionValidateFilter implements Filter { public void destroy() {} public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; HttpSession session = request.getSession(); if ((session == null) || (session.getAttribute("username") == null)) { String PATH=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; response.sendRedirect(PATH+"login.jsp"); } chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException {}}
利用Filter实现session拦截
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。