shiro版本

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.7.0</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.


自定义RedisSessionDAO

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class RedisSessionDAO extends CachingSessionDAO {

    private final StringRedisTemplate stringRedisTemplate;

    public RedisSessionDAO(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
    
    /**
     * SESSION 超时时间
     */
    public static final int SESSION_TIME_OUT = 24 * 60 * 60;
    /**
     * SESSION会话
     */
    public final static String CACHE_SESSION = "CACHE:SESSION:";

    @Override
    protected void doUpdate(Session session) {
        stringRedisTemplate.opsForValue().set(sessionKeyGenerator(session.getId().toString()),
                serialize(session), SESSION_TIME_OUT, TimeUnit.SECONDS);
    }

    @Override
    protected void doDelete(Session session) {
        stringRedisTemplate.delete(sessionKeyGenerator(session.getId().toString()));
    }

    @Override
    protected Serializable doCreate(Session session) {
        Serializable sessionId = generateSessionId(session);
        assignSessionId(session, sessionId);
        stringRedisTemplate.opsForValue().set(sessionKeyGenerator(session.getId().toString()),
                serialize(session), SESSION_TIME_OUT, TimeUnit.SECONDS);
        return sessionId;
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        String sessionKey = sessionKeyGenerator(sessionId.toString());
        String value = stringRedisTemplate.opsForValue().get(sessionKey);
        if (StringUtils.hasText(value)) {
            return deserialize(value);
        }
        return null;
    }

    public String sessionKeyGenerator(String sessionId) {
        return CACHE_SESSION + sessionId;
    }

    private String serialize(Session session) {
        if (session instanceof SimpleSession) {
            SimpleSession simpleSession = (SimpleSession) session;
            JSONObject object = new JSONObject();
            object.put("id", simpleSession.getId());
            object.put("startTimestamp", simpleSession.getStartTimestamp());
            object.put("stopTimestamp", simpleSession.getStopTimestamp());
            object.put("lastAccessTime", simpleSession.getLastAccessTime());
            object.put("timeout", simpleSession.getTimeout());
            object.put("expired", simpleSession.isExpired());
            object.put("host", simpleSession.getHost());
            object.put("attributes", simpleSession.getAttributes());
            return object.toJSONString();
        }
        return null;
    }

    private Session deserialize(String value) {
        JSONObject object = JSON.parseObject(value);
        SimpleSession simpleSession = new SimpleSession();
        simpleSession.setId(object.getObject("id", Serializable.class));
        simpleSession.setStartTimestamp(object.getDate("startTimestamp"));
        simpleSession.setStopTimestamp(object.getDate("stopTimestamp"));
        simpleSession.setLastAccessTime(object.getDate("lastAccessTime"));
        simpleSession.setTimeout(object.getLongValue("timeout"));
        simpleSession.setExpired(object.getBooleanValue("expired"));
        simpleSession.setHost(object.getString("host"));
        Map<Object, Object> attributes = object.getObject("attributes", Map.class);
        simpleSession.setAttributes(attributes);
        return simpleSession;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.


在sessionManager中使用

@Bean
public SessionManager sessionManager() {
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    sessionManager.setGlobalSessionTimeout(SESSION_TIME_OUT * 1000L);
    sessionManager.setSessionDAO(new RedisSessionDAO(stringRedisTemplate));
    return sessionManager;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.