本文基于此文章:https://luoyuer.com/archives/springbootshiroqh.html

1.引入依赖

        <dependency>
            <groupId>org.crazycake</groupId>
            <artifactId>shiro-redis</artifactId>
            <version>3.3.1</version>
        </dependency>

2.修改ShiroConfig

1.构建所需的类

private RedisManager redisManager() {
        RedisManager redisManager = new RedisManager();
        //这里按需设置,默认localhost:6379,
        redisManager.setHost(host+":"+port);
        redisManager.setDatabase(database);
        return redisManager;
    }
    @Bean
    public RedisCacheManager cacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(redisManager());
        // 必须要设置主键名称,shiro-redis 插件用过这个缓存用户信息
        redisCacheManager.setPrincipalIdFieldName("id");
        return redisCacheManager;
    }
    @Bean
    public RedisSessionDAO redisSessionDAO() {
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
        redisSessionDAO.setRedisManager(redisManager());
//这里的expire单位为秒,需要将所有有效期设置一致
        redisSessionDAO.setExpire(1296000);
        return redisSessionDAO;
    }
    @Bean
    public SimpleCookie cookie() {
        // cookie的name,对应的默认是 JSESSIONID
        SimpleCookie cookie = new SimpleCookie("JSESSIONID");
        cookie.setHttpOnly(true);
//session最大存活时间。若不填写,则关闭浏览器后失效
        cookie.setMaxAge(1296000);
        //  path为 / 用于多个系统共享 JSESSIONID
        cookie.setPath("/");
        return cookie;
    }

2.修改securityManager()

添加如下代码

manager.setCacheManager(cacheManager());

3.修改sessionManager()

添加如下代码

//超时时间需与上面的超时一致。但是注意这里的单位是毫秒。1秒=1000毫秒
sessionManager.setGlobalSessionTimeout(1296000000);
sessionManager.setSessionIdCookie(cookie());

至此,结束


山内有樱名为良,树本无名只待春。