All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.easyopen.session.RedisSessionManager Maven / Gradle / Ivy

package com.gitee.easyopen.session;

import com.gitee.easyopen.ApiContext;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.Objects;
import java.util.UUID;

/**
 * SessionManager的redis实现,使用redis管理session
 * 
 * @author tanghc
 *
 */
public class RedisSessionManager implements SessionManager {

    private RedisTemplate redisTemplate;

    /** 过期时间,30分钟 */
    private int sessionTimeout = 30;

    /** 存入redis中key的前缀 */
    private String keyPrefix = "session:";


    public RedisSessionManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
        Objects.requireNonNull(redisTemplate, "RedisSessionManager中的redisTemplate不能为null");
        this.redisTemplate = redisTemplate;
    }

    @Override
    public HttpSession getSession(String sessionId) {
        return this.getSession(sessionId, this.keyPrefix);
    }
    
    public HttpSession getSession(String sessionId, String keyPrefix) {
        if (this.hasKey(sessionId)) {
            return RedisHttpSession.createExistSession(sessionId, getServletContext(), redisTemplate, keyPrefix);
        } else {
            sessionId = this.buildSessionId(sessionId);
            return RedisHttpSession.createNewSession(getServletContext(), sessionId, this.getSessionTimeout(),
                    redisTemplate, keyPrefix);
        }
    }
    
    /**
     * 构建sessionId
     * @param id 指定的sessionId,如果没有指定,则生成一个新的
     * @return 返回sessionid
     */
    public String buildSessionId(String id) {
        return (id != null ? id : UUID.randomUUID().toString().replace("-", "").toUpperCase());
    }

    public boolean hasKey(String sessionId) {
        if (sessionId == null) {
            return false;
        } else {
            String key = RedisHttpSession.buildKey(this.keyPrefix, sessionId);
            return redisTemplate.hasKey(key);
        }
    }

    public ServletContext getServletContext() {
        return ApiContext.getServletContext();
    }

    public int getSessionTimeout() {
        return sessionTimeout;
    }

    /**
     * 设置session过期时间,单位分钟
     * @param sessionTimeout 超时时间
     */
    public void setSessionTimeout(int sessionTimeout) {
        this.sessionTimeout = sessionTimeout;
    }

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public String getKeyPrefix() {
        return keyPrefix;
    }

    /**
     * 设置存入redis中key的前缀,默认为"session:"
     * @param keyPrefix 前缀
     */
    public void setKeyPrefix(String keyPrefix) {
        this.keyPrefix = keyPrefix;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy