(四) Session管理 --《springboot与shiro整合》

登录成功后使用Subject.getSession()即可获取会话;其等价于Subject.getSession(true),即如果当前没有创建Session对象会创建一个;

另外Subject.getSession(false),如果当前没有创建Session则返回null(不过默认情况下如果启用会话存储功能的话在创建Subject时会主动创建一个Session)。

JAVA代码

session.getId();

获取session唯一id

session.getHost(); 

获取当前Subject的主机地址,该地址是通过HostAuthenticationToken.getHost()提供的。

session.getTimeout();
session.setTimeout(毫秒);

获取/设置当前Session的过期时间;

session.getStartTimestamp();
session.getLastAccessTime();

获取会话的启动时间及最后访问时间

会话管理器

会话管理器管理着应用中所有Subject的会话的创建、维护、删除、失效、验证等工作。是Shiro的核心组件,顶层组件SecurityManager直接继承了SessionManager,且提供了SessionsSecurityManager实现直接把会话管理委托给相应的SessionManager,DefaultSecurityManager及DefaultWebSecurityManager默认SecurityManager都继承了SessionsSecurityManager。

1.自定义SessionDao 持久化session 相关信息

@Component
public class ShiroSessionDao extends CachingSessionDAO     {

    @Autowired
    private SessionDao sessionDao;

    @Override
    public Serializable doCreate(Session session) {
        ShiroSession ss = new ShiroSession();
        Serializable sessionId = generateSessionId(session);
        assignSessionId(session, sessionId);
        ss.setSession(sessionId.toString());
        ss.setHost(session.getHost());
        sessionDao.createSession(ss);
        return sessionId;
    }

    @Override
    public void doDelete(Session session) {
        Serializable sessionId = session.getId();
        sessionDao.deleteSession(sessionId.toString());
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {

        return super.getCachedSession(sessionId);
    }

    @Override
    protected void doUpdate(Session session) {

    }

}

2.配置SessionManager

   //配置设置会话ID生成器,
    @Bean
    public JavaUuidSessionIdGenerator sessionIdGenerator(){

        return new JavaUuidSessionIdGenerator();
    }

    @Bean
    public SessionDAO getSessionDao(){
        ShiroSessionDao sessionDao = new ShiroSessionDao();
        sessionDao.setSessionIdGenerator(sessionIdGenerator());
        return sessionDao;
    }

    @Bean
    public SessionManager sessionManager(){
        SessionManager sessionManager = new SessionManager();
        ExecutorServiceSessionValidationScheduler scheduler = new ExecutorServiceSessionValidationScheduler();
        scheduler.setInterval(180000);
        scheduler.setSessionManager(sessionManager);
        sessionManager.setGlobalSessionTimeout(180000);
        sessionManager.setSessionValidationScheduler(scheduler);
        sessionManager.setSessionDAO(getSessionDao());
        return sessionManager;
    }

    @Bean
    public EhCacheManager  cacheManager(){
        EhCacheManager cache = new EhCacheManager();
        cache.setCacheManagerConfigFile("classpath:ehcache.xml");
        return cache;
    }

    @Bean
    public SecurityManager securityManager() {
      DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
      securityManager.setRealm(myShiroRealm());
      securityManager.setSessionManager(sessionManager());
      securityManager.setCacheManager(cacheManager());
      return securityManager;
    }



ExecutorServiceSessionValidationScheduler:会话验证调度器,定时检测会话是否过期

现在用户会话都会保存到数据库中,由ExecutorServiceSessionValidationScheduler定时检测是否过期



原文地址:https://www.cnblogs.com/zls1218/p/8856347.html

时间: 2024-08-01 15:09:58

(四) Session管理 --《springboot与shiro整合》的相关文章

SpringBoot与Shiro整合

修改pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <

springboot,vue,shiro整合 关于登录认证功能

首先是session问题 传统session认证 http协议是一种无状态协议,即浏览器发送请求到服务器,服务器是不知道这个请求是哪个用户发来的.为了让服务器知道请求是哪个用户发来的,需要让用户提供用户名和密码来进行认证.当浏览器第一次访问服务器(假设是登录接口),服务器验证用户名和密码之后,服务器会生成一个sessionid(只有第一次会生成,其它会使用同一个sessionid),并将该session和用户信息关联起来,然后将sessionid返回给浏览器,浏览器收到sessionid保存到C

SpringBoot 优雅的整合 Shiro

Apache Shiro是一个功能强大且易于使用的Java安全框架,可执行身份验证,授权,加密和会话管理.借助Shiro易于理解的API,您可以快速轻松地保护任何应用程序 - 从最小的移动应用程序到最大的Web和企业应用程序.网上找到大部分文章都是以前SpringMVC下的整合方式,很多人都不知道shiro提供了官方的starter可以方便地跟SpringBoot整合. 请看shiro官网关于springboot整合shiro的链接:Integrating Apache Shiro into S

008-shiro与spring web项目整合【二】认证、授权、session管理

一.认证 1.添加凭证匹配器 添加凭证匹配器实现md5加密校验. 修改applicationContext-shiro.xml: <!-- realm --> <bean id="customRealm" class="com.lhx.ssm.shiro.CustomRealm"> <!-- 将凭证匹配器设置到realm中,realm按照凭证匹配器的要求进行散列 --> <property name="creden

项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器,整合ehcache环境,只需要配置即可.     <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>

细说shiro之六:session管理

官网:https://shiro.apache.org/ - org.apache.shiro.session.Session - org.apache.shiro.session.mgt.DelegatingSession - org.apache.shiro.web.session.HttpServletSession - org.apache.shiro.session.ProxiedSession - org.apache.shiro.session.mgt.ImmutableProxi

Shiro Quartz之Junit测试Session管理

Shiro的quartz主要API上提供了org.apache.shiro.session.mgt.quartz下session管理的两个类:QuartzSessionValidationJob和QuartzSessionValidationScheduler. 下面我们来看看shiro quartz使用Junit是怎样通过测试的: package org.apache.shiro.session.mgt.quartz; import java.util.Date; import org.apa

SpringBoot初始教程之Redis集中式Session管理

1.介绍 有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器).基于Cookie(安全性差).基于全局的统一Session管理(redis.mysql)等多种方式 针对于像淘宝这种超大型网站来说Session如何管理的就无从得知了.但是可以通过yy的方式想象一下,这种大型架构都需要部署多台认证Server,但是一般来说集中式Session无法存储那么多的Session 那么就可以通过UID分片的形式来存储,不同UID分布在不同的Se

Shiro企业级实战详解,统一的Session管理。

基础的什么配置这些都不说了,百度一下什么都有,直接上干货. Shiro切入点是从web.xml文件,通过filter进行拦截. 直接看DelegatingFilterProxy这个类,很简单,父类就是一个filter,肯定会初始化filter,后面会调用这个方法: @Override protected void initFilterBean() throws ServletException { synchronized (this.delegateMonitor) { if (this.de