springboot实现登录超时跳转到别的页面

springboot实现登录超时跳转到别的页面

创建一个拦截器继承HandlerInterceptor,实现其中的preHandle方法

public class TimeOutIntercepter implements HandlerInterceptor{
    //可以随意访问的url
    public String[] allowUrls;

    public void setAllowUrls(String[] allowUrls) {
        this.allowUrls = allowUrls;
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {


        String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession(true);
        if(StringUtils.isNoneBlank(requestUrl)){
            for(String url:allowUrls){
                if(requestUrl.contains(url)){
                    return true;
                }
            }
        }
        //session持续时间
        int maxInactiveInterval = session.getMaxInactiveInterval();
       //session创建时间
        long creationTime = session.getCreationTime();
        //session最新链接时间
        long lastAccessedTime = session.getLastAccessedTime();

        System.out.println("-----> maxInactiveInterval: "+maxInactiveInterval);
        System.out.println("-----> creationTime: "+creationTime);
        System.out.println("-----> lastAccessedTime: "+lastAccessedTime);

        //从session获取上次链接时间
        Long operateTime = (Long)session.getAttribute("operateTime");
        System.out.println("-----> operateTime: "+operateTime);

        //如果operateTime是空,说明是第一次链接,对operateTime进行初始化
        if(operateTime ==null){
            session.setAttribute("operateTime",lastAccessedTime);
            return true;
        }else{
        //计算最新链接时间和上次链接时间的差值
            int intervalTime = (int)((lastAccessedTime - operateTime)/1000);
            System.out.println("-----> intervalTime: "+intervalTime);
            //如果超过十秒没有交互的话,就跳转到超时界面
            if(intervalTime>10){
                response.sendRedirect(request.getContextPath()+"/static/timeout.html");
                return true;
            }
            //更新operateTime
            session.setAttribute("operateTime",lastAccessedTime);
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

创建一个类继承WebMvcConfigurerAdapter
添加拦截器

@Configuration
public class SpirngMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WeixinSMSIntercepter weixinSMSIntercepter = new WeixinSMSIntercepter();
        String allowUrls[] = {"login","logout"};
        weixinSMSIntercepter.setAllowUrls(allowUrls);
        //添加拦截器
        registry.addInterceptor(weixinSMSIntercepter);

        super.addInterceptors(registry);
    }
}

完成

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值