在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ shiro使用redisSessionDao,登錄驗證可以成功,但是獲取不到用戶

shiro使用redisSessionDao,登錄驗證可以成功,但是獲取不到用戶信息

項目使用springboot+shiro來驗證用戶,shiro原本是用MemorySessionDAO(shiro默認(rèn))來存儲的,整個項目都沒有問題,近期我將sessionDao調(diào)整成redisSessionDao(代碼見下)之后,session可以存入的redis,登錄驗證也可以驗證成功,在獲取用戶信息的時候確獲取不到用戶信息,例如SecurityUtils.getSubject().getPrincipal()為null,SecurityUtils.getSubject().isAuthenticated()為false,當(dāng)sessionDao更改為MemorySessionDAO就一切都正常了,試了好長時間,沒有解決,請大牛解答?

redisSessionDao代碼如下:

public class RedisSessionDAO extends AbstractSessionDAO{

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    // session 在redis過期時間是30分鐘30*60
    private static int expireTime = 1800;

    private static String redisPrefix = "shiro-redis-session:";

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private String getKey(String originalKey) {
        return redisPrefix + originalKey;
    }

    // 創(chuàng)建session,保存到數(shù)據(jù)庫
    @Override
    protected Serializable doCreate(Session session) throws UnknownSessionException {
        Serializable sessionId = generateSessionId(session);
        assignSessionId(session, sessionId);
        logger.debug("createSession:{}", session.getId().toString());
        try {
            redisTemplate.opsForValue().set(getKey(session.getId().toString()), session);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage(),e);
        }
        return sessionId;
    }

    // 獲取session
    @Override
    protected Session doReadSession(Serializable sessionId) {
        logger.debug("readSession:{}", sessionId.toString());
        // 先從緩存中獲取session,如果沒有再去數(shù)據(jù)庫中獲取
        Session session = null ;
        try {
            session = (Session) redisTemplate.opsForValue().get(getKey(sessionId.toString()));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage(),e);
        }
        return session;
    }

    // 更新session的最后一次訪問時間
    @Override
    public void update(Session session) {
        logger.debug("updateSession:{}", session.getId().toString());
        String key = getKey(session.getId().toString());
        if (!redisTemplate.hasKey(key)) {
            redisTemplate.opsForValue().set(key, session);
        }
        redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
    }

    // 刪除session
    @Override
    public void delete(Session session) {
        logger.debug("delSession:{}", session.getId());
        redisTemplate.delete(getKey(session.getId().toString()));
    }

    @Override
    public Collection<Session> getActiveSessions() {
        logger.debug("activeSession");
        return Collections.emptySet();
    }
}

shiroConfig配置代碼

@Bean(name = "redisSessionDAO")
public RedisSessionDAO sessionDAO(){
    return new RedisSessionDAO();
}

/**
 * @see DefaultWebSessionManager
 * @return
 */
@Bean(name="sessionManager")
public DefaultWebSessionManager defaultWebSessionManager() {
    System.out.println("ShiroConfiguration.defaultWebSessionManager()");
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    

> sessionManager.setSessionDAO(sessionDAO());//此行代碼注釋項目即可正常

    sessionManager.setCacheManager(ehCacheManager());
    sessionManager.setSessionValidationInterval(3600000*12);
    sessionManager.setGlobalSessionTimeout(3600000*12);
    sessionManager.setDeleteInvalidSessions(true);
    sessionManager.setSessionValidationSchedulerEnabled(true);
    Cookie cookie = new SimpleCookie(ShiroHttpSession.DEFAULT_SESSION_ID_NAME);
    cookie.setHttpOnly(true);
    sessionManager.setSessionIdCookie(cookie);
    return sessionManager;
}
回答
編輯回答
六扇門

個人感覺問題出在Session更新這塊,登錄認(rèn)證成功后,shiro會對session進(jìn)行更新,session內(nèi)保存用戶的認(rèn)證信息,看你現(xiàn)在更新session,只是處理了最后登錄時間,建議嘗試將整個session進(jìn)行更新試下;直接把redisTemplate.hasKey(key)判斷注釋掉看看

2018年9月2日 03:51
編輯回答
骨殘心

移除sessionManager.setCacheManager(ehCacheManager());可解決
由于已經(jīng)啟用了sessionDao,CacheManager會影響到SessionDao

2017年4月4日 03:06