Java--过滤器LoginFilter实现会话超时跳转登录页面

一、说明

1、网站系统登录,从安全的角度来考虑,登录会话超时,再次页面会退到登录界面。
2、本文配置如何通过过滤器(Filter)实现会话超时(如30分钟)跳转到登录页面,分LoginFilter.java类和web.xml配置两部分。

二、实现代码

过滤器类LoginFilter.java

 

package com.sale.filter;
import java.io.IOException;  

import javax.servlet.Filter;  
import javax.servlet.FilterChain;  
import javax.servlet.FilterConfig;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
/**
 * @author 作者:Justin
 * @version 创建时间:2018年1月25日 上午10:36:23
 * 类说明
 */


public class LoginFilter implements Filter {  
	 @Override
    public void destroy() {  
        // TODO Auto-generated method stub  
  
    }  
    @Override
    public void doFilter(ServletRequest req, ServletResponse res,  
            FilterChain chain) throws IOException, ServletException {  
  
        HttpServletRequest httpReq=(HttpServletRequest)req;  
        HttpServletResponse httpRes=(HttpServletResponse)res;  
        HttpSession httpSession=httpReq.getSession();  
        String path = httpReq.getRequestURI();	//当前请求相对url
        String loginUrl = httpReq.getContextPath()+ "/loginout.action";	//1.登录界面url
        String initUrl = httpReq.getContextPath()+   "/tevo_loginInit.action";	//2.初始化界面url
        String userName = (String)httpSession.getAttribute("currentUsername");	//在session中获取当前用户名
        // 1、登陆页面、初始化页面不过滤
        if(loginUrl.equals(path) || initUrl.equals(path)) {
            chain.doFilter(req, res);
            return;
        }
//
        if(userName==null){  
            httpRes.sendRedirect(loginUrl);
            return;
        }else{  
            chain.doFilter(req, res); 
            return;
        }  
    }  
    @Override
    public void init(FilterConfig arg0) throws ServletException {  
        // TODO Auto-generated method stub  
  
    }  
  
}  

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">


  <!-- configure loginFilter -->  
  <filter>  
     <filter-name>loginFilter</filter-name>  
     <filter-class>com.sale.filter.LoginFilter</filter-class>  
  </filter>  
  <filter-mapping>  
     <filter-name>loginFilter</filter-name>  
     <url-pattern>*.action</url-pattern>  
  </filter-mapping>  
  <!-- configure session timeout 30 minute -->  
  <session-config>  
      <session-timeout>30</session-timeout>  
  </session-config>  

</web-app>

 

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吾日三省贾斯汀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值