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);
    }
}

完成

可以使用 JavaScript 来实现一定时间无操作自动跳转登录页面。具体的实现思路如下: 1. 定义一个计时器变量,用于记录用户最后一次操作的时间。 2. 在页面加载时,给整个页面绑定一个鼠标移动事件和键盘事件,当用户进行操作时,将计时器重置为当前时间。 3. 定义一个函数,用于检查当前时间与计时器记录的时间差是否超过指定的时间间隔,如果超过了,则跳转登录页面。 4. 在页面加载时启动定时器,定时检查时间间隔是否超过指定值,如果超过了则跳转登录页面。 以下是一个简单的实现示例代码: ```javascript // 定义一个计时器变量,初始为当前时间 var lastActiveTime = new Date().getTime(); // 绑定鼠标移动事件和键盘事件,重置计时器 document.addEventListener('mousemove', function() { lastActiveTime = new Date().getTime(); }); document.addEventListener('keypress',function(){ lastActiveTime = new Date().getTime(); }); // 定义跳转函数,用于检查时间间隔并跳转登录页面 function checkTimeout() { var now = new Date().getTime(); var timeout = 60000; // 设定超时时间为60秒 if (now - lastActiveTime > timeout) { window.location.href = 'login.html'; // 跳转登录页面 } } // 启动定时器,每10秒检查一次时间间隔 setInterval(checkTimeout, 10000); ``` 上述代码会在用户最后一次操作后的60秒内不断检查时间间隔,并在超时跳转登录页面。你可以根据需要修改超时时间和定时器的时间间隔。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值