通过api:Shiro的Session接口有一个setTimeout()方法

//登录后,可以用如下方式取得session
SecurityUtils.getSubject().getSession().setTimeout(30000);

查看Shiro的api文档,

setTimeout

void setTimeout(long maxIdleTimeInMillis) throws InvalidSessionExceptionSets the time in milliseconds that the session may remain idle before expiring.A negative value means the session will never expire.A non-negative value (0 or greater) means the session
expiration will occur if idle for that length of time.*Note: if you are used to the HttpSession's getMaxInactiveInterval() method, the scale on this method is different: Shiro Sessions use millisecond values for timeout whereas HttpSession.getMaxInactiveInterval
uses seconds. Always use millisecond values with Shiro sessions.Parameters:maxIdleTimeInMillis - the time in milliseconds that the session may remain idle before expiring.Throws:InvalidSessionException - if the session has been stopped or expired prior to
calling this method.Since:0.2

设置的最大时间,正负都可以,为负数时表示永不超时。开发过程中,设置负数时,遇到点儿问题:

 
SecurityUtils.getSubject().getSession().setTimeout(-1l);
 

这样调用后,总是抛出session已经过时的异常,一直找不到原因,后来调试源码才发现,这里设置的时间单位是:ms,但是Shiro会把这个时间转成:s,而且是会舍掉小数部分,这样我设置的是-1ms,转成s后就是0s,马上就过期了,所以后面再对这个会话进行操作时,总会抛异常,正确的设置永不超时的方式应该是:

// timeout:-1000ms 永不超时
SecurityUtils.getSubject().getSession().setTimeout(-1000l);

Shiro 设置session超时时间的更多相关文章

  1. shiro设置session超时时间

    系统默认超时时间是180000毫秒(30分钟) long timeout = SecurityUtils.getSubject().getSession().getTimeout(); System. ...

  2. WebLogic如何设置session超时时间

    1.web.xml  设置WEB应用程序描述符web.xml里的<session-timeout>元素.这个值以分钟为单位,并覆盖weblogic.xml中的TimeoutSecs属性   ...

  3. shiro设置session超时

    通过api:Shiro的Session接口有一个setTimeout()方法 //登录后,可以用如下方式取得session SecurityUtils.getSubject().getSession( ...

  4. 【python】Django设置SESSION超时时间没有生效?

    按手册和网上的方法在settings.py中设置“SESSION_COOKIE_AGE” 和 “SESSION_EXPIRE_AT_BROWSER_CLOSE” 均不生效. 通过查看django的源代 ...

  5. Springboot设置session超时时间

    按优先级高到低说: 第一种: spring boot 启动类里面: package com.mycenter; import org.mybatis.spring.annotation.MapperS ...

  6. Spring Boot 应用使用spring session+redis启用分布式session后,如何在配置文件里设置应用的cookiename、session超时时间、redis存储的namespace

    现状 项目在使用Spring Cloud搭建微服务框架,其中分布式session采用spring session+redis 模式 需求 希望可以在配置文件(application.yml)里设置应用 ...

  7. springboot设置session超时和session监听

    2.0版本以下设置session超时时间 1.  springboot 2.0版本以下配置session超时 1.1 application.properties配置文件: spring.sessio ...

  8. Javaweb设置session过期时间

    在Java Web开发中,Session为我们提供了很多方便,Session是由浏览器和服务器之间维护的.Session超时理解为:浏览器和服务器之间创建了一个Session,由于客户端长时间(休眠时 ...

  9. Spring Boot Session 超时时间

    springboot session https://www.jianshu.com/p/523572937db8 springboot2以上版本设置session超时时间 https://blog. ...

随机推荐

  1. bzoj 1303 杂题

    首先如果一个数是中位数,在这段区间中比他大的数量=比他小的数量,那么如果一个数比他大设为1,比他小设为-1,设要求的数在数组中的位置是mid,那么我们可以用num[i] 表示1-mid这一段中,j-m ...

  2. vim的插件管理与配置

    vim作为我做喜欢的编辑器,可扩展性几乎是没有尽头的,前阵子一直在配置这.vimrc文件,原来搞过配色文件,现在主要就自动补全来设置下,同时作为自己配置的记录文档.……………………………………………… ...

  3. pymysql 线程安全pymysqlpool

    # -*-coding: utf-8-*- # Author : Christopher Lee # License: Apache License # File : test_example.py ...

  4. 【乱入】Uva11021麻球繁衍

    就是根据概率公式入门算算. #include<bits/stdc++.h> ; int n,m,k; double p[N],f[N]; int main(){ int T;scanf(& ...

  5. 带你入门代理模式/SpringAop的运行机制

    SpringAop 是spring框架中最重要的一项功能之一,同时也是企业级开发记录事物日志等不可或缺的一部分,如果说你的系统需要记录用户访问接口的操作,那SpringAop是很完美的了,当然,拦截器 ...

  6. mysql内连接、左连接、右连接举例说明

    如下: CREATE TABLE tb ( id INT PRIMARY KEY, NAME VARCHAR (20) ) ; CREATE TABLE ta ( id INT PRIMARY KEY ...

  7. [ 总结 ] Linux系统测试硬盘I/O

    检测硬盘I/O相对来说还是一个比较抽象的概念,但是对系统性能的影响还是至关重要的. (1)使用hdparm命令检测读取速度:    hdparm命令提供了一个命令行的接口用于读取和设置IDE和SCSI ...

  8. 【 Linux 】lvs-dr模型实现HA,后端Nginx、PHP、MySQL分离 搭建wordpress站点

    要求:    1. wordpress程序通过nfs共享给各个realserver    2. 后端realserver中的nginx和php分离 网络结构图: 环境说明:    OS:centos6 ...

  9. 怎样用css来美化一个html页面

    # 转载请留言联系 我们都知道html写出来的东西是一个文本内容,很单调.和我们平时刷网页看到的内容不一样.那普通的网页是怎样对html超文本进行装饰的呢?没错,就是CSS. css的基本语法 选择器 ...

  10. 【C++】继承时构造函数和析构函数

    1. 顺序 先调用基类的构造函数,再调用派生类构造函数.析构顺序相反. 2. 构造函数 派生类 不用初始化列表调用基类构造函数->调用基类的默认构造函数 派生类 使用初始化列表调用基类带参构造函 ...