Filter实现session超时自动跳转到login页,超过试用期不许登录

新建一个过滤器

package com.autumn.filter;

import com.autumn.pojo.Users;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 是否登录判断
 */
public class CheckLoginFilter implements Filter {

    private String USER ="";

    private String ROOTPATH;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.USER = filterConfig.getInitParameter("user").trim();
        this.ROOTPATH = filterConfig.getInitParameter("rootPath");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        Object userid = null;
        HttpServletResponse res=(HttpServletResponse)response;
        HttpServletRequest req=(HttpServletRequest)request;
        Users users = (Users)req.getSession().getAttribute(USER);
        if (users!=null){
            userid =users.getId();
        }

        System.out.println("req.getRequestURI():"+req.getRequestURI());

        if(isShakedown(3018,3,16))
        {
            System.out.println("系统试用期已到,请联系商务!");
            res.sendRedirect(ROOTPATH+"?rtnCode=200");
        }else if(req.getRequestURI().contains("/loginController/login")){   //当是登录请求时,继续往下执行
            filterChain.doFilter(req, res);
        }else if(userid==null||userid.toString().trim().isEmpty())  //当没有session时,即未登录,跳到登录页
        {
            //System.out.println("CheckLoginFilter userid:"+userid);
            System.out.println("CheckLoginFilter userId is null");
            res.sendRedirect(ROOTPATH);
        }else{   //继续往下执行
            //System.out.println("CheckLoginFilter chain.doFilter");
            filterChain.doFilter(req, res);
        }
    }

    @Override
    public void destroy() {
        if(this.USER !=null&&this.USER.trim().isEmpty())
        {
            this.USER ="";
        }

        if(this.ROOTPATH!=null&&this.ROOTPATH.trim().isEmpty())
        {
            this.ROOTPATH="";
        }
    }

    //试用期判断
    public static boolean isShakedown(int year,int month,int day)
    {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();// 获取一个Claender实例
        cal.set(year, month-1, day);
        Date endDate;
        try {
            endDate = sf.parse(sf.format(cal.getTime()));
            if (date.getTime() > endDate.getTime()) {
                return true;
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return true;
        }
        return false;
    }
}

web.xml中加入过滤器配置

<filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>
        com.autumn.filter.CheckLoginFilter
    </filter-class>
    <init-param>
        <param-name>excludedPages</param-name>
        <param-value>/login.jsp</param-value><!-- 登陆页不做拦截的请求声明,filter中登录的接口也被排除-->
    </init-param>
    <init-param>
        <param-name>user</param-name>   <!--配置的session参数-->
        <param-value>user</param-value>
    </init-param>
    <init-param>
        <param-name>rootPath</param-name>  <!-- 配置的登录页 -->
        <param-value>/Bookkeeping/login.jsp</param-value>
    </init-param>
</filter>
<filter-mapping>  <!--拦截映射一 -->
    <filter-name>loginFilter</filter-name>
    <url-pattern>/manager/*</url-pattern>   <!--拦截后台的请求接口-->
</filter-mapping>
<filter-mapping><!--拦截映射二 -->
    <filter-name>loginFilter</filter-name>
    <url-pattern>/jsp/*</url-pattern>    <!--拦截直接访问前端页面-->
</filter-mapping>

 

转载于:https://www.cnblogs.com/aeolian/p/9234357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值