Apache Shiro使用EnterpriseCacheSessionDAO将会话ID存储到Redis中获取错误密钥

时间:2018-08-14 07:33:58

标签: java spring-mvc spring-boot redis shiro

我是shiro安全框架的新手

我正在使用DefaultWebSessionManager配置shiro,并将SessionDao设置为EnterpriseSessionDao

存储会话信息后,存储会话ID进入redis是乱码

redis keys image

商店缓存代码为:

public class RedisSessionDao extends EnterpriseCacheSessionDAO {
  private static final Logger logger = LoggerFactory.getLogger(RedisSessionDao.class);
  @Autowired
  private RedisTemplate<String, Object> redistemplate;
  private String key = "shiro-activeSessionsCache:";
  private static int expireTime = 1800;

  @Override
  protected Serializable doCreate(Session session) {
   Serializable sessionId = super.doCreate(session);
   logger.debug("create session:\t{}", sessionId);
   redistemplate.opsForValue().set(key + sessionId.toString(), session);
   return sessionId;
  }
}

shiro bean配置:

@Configuration
public class ShiroConfig {
  @Autowired
  private RedisCacheManager cacheManager;

  @Bean(name = "securityManager")
  public SecurityManager securityManager() {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    securityManager.setRealm(myRealm());
    securityManager.setSessionManager(defaultWebSessionManager());
    securityManager.setCacheManager((org.apache.shiro.cache.CacheManager) cacheManager);
    return securityManager;
  }

  @Bean
  public DefaultWebSessionManager defaultWebSessionManager() {
    DefaultWebSessionManager defaultSessino = new DefaultWebSessionManager();
    defaultSessino.setSessionDAO(enterpriseCacheSessionDAO());
    defaultSessino.setSessionValidationScheduler(executorServiceSessionValidationScheduler());
    return defaultSessino;
  }

  @Bean
  public EnterpriseCacheSessionDAO enterpriseCacheSessionDAO() {
    EnterpriseCacheSessionDAO enterpriseCacheSessionDAO = new RedisSessionDao();
    return enterpriseCacheSessionDAO;
  }
 }

问题是:doCreate方法应将缓存会话ID存储为"shiro-activeSessionsCache:" + sessionid

为什么redis show键是错误键? :)

0 个答案:

没有答案