From 0c4e8411f174951159fa286aeb8e092e63e0c0bc Mon Sep 17 00:00:00 2001 From: qi_liang Date: Fri, 28 Jun 2019 17:06:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dshiro=20sessionBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/lq/code/cache/RedisSessionDao.java | 23 +++++++++++-------- .../com/lq/code/cache/RedisShiroCache.java | 11 ++------- .../com/lq/code/executor/package-info.java | 4 ---- src/main/resources/dev/jdbc.properties | 2 +- src/main/resources/dev/redis.properties | 2 +- src/main/resources/spring/spring-redis.xml | 2 +- src/main/resources/spring/spring-shiro.xml | 1 - .../webapp/WEB-INF/views/jsp/cms/login.jsp | 2 ++ .../views/jsp/cms/wechat/message/edit.jsp | 6 +++++ 9 files changed, 27 insertions(+), 26 deletions(-) delete mode 100755 src/main/java/com/lq/code/executor/package-info.java diff --git a/src/main/java/com/lq/code/cache/RedisSessionDao.java b/src/main/java/com/lq/code/cache/RedisSessionDao.java index 7b4be02..b3ee036 100755 --- a/src/main/java/com/lq/code/cache/RedisSessionDao.java +++ b/src/main/java/com/lq/code/cache/RedisSessionDao.java @@ -9,6 +9,7 @@ import org.apache.shiro.session.Session; import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import java.io.IOException; @@ -19,18 +20,19 @@ import java.util.concurrent.TimeUnit; /** * Created by qi_liang on 2018/9/11. */ -public class RedisSessionDao extends EnterpriseCacheSessionDAO { +public class RedisSessionDao extends EnterpriseCacheSessionDAO { + private final static Logger LOGGER = LoggerFactory.getLogger(RedisSessionDao.class); - private RedisTemplate redisTemplate; + @Autowired + private RedisTemplate redisTemplate; private int defaultExpireTime ; private CacheManager cm = null; - public RedisSessionDao(RedisTemplate redisTemplate,int defaultExpireTime){ - this.redisTemplate = redisTemplate; + public RedisSessionDao(int defaultExpireTime){ this.defaultExpireTime = defaultExpireTime; } @@ -41,7 +43,7 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO { //该方法交给父类去执行 // super.doUpdate(session); //更新reids中的session时间 - redisTemplate.expire(session.getId().toString(),this.defaultExpireTime, TimeUnit.SECONDS); + redisTemplate.expire((K)session.getId(),this.defaultExpireTime, TimeUnit.SECONDS); } @@ -54,7 +56,7 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO { cm = new CacheManager(getCacheMangerConfigFileImputStream()); } Ehcache ehcache = cm.getCache("sessioncache"); - redisTemplate.delete(sessionId.toString()); + redisTemplate.delete((K) sessionId); ehcache.remove(sessionId.toString()); } @@ -68,8 +70,8 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO { Ehcache ehcache = cm.getCache("sessioncache"); assignSessionId(session,sessionId); - redisTemplate.opsForValue().set(sessionId.toString(),session); - redisTemplate.expire(session.getId().toString(),this.defaultExpireTime, TimeUnit.SECONDS); + redisTemplate.opsForValue().set((K) sessionId,(V) session); + redisTemplate.expire((K) sessionId,this.defaultExpireTime, TimeUnit.SECONDS); ehcache.put(new Element(sessionId.toString(),session)); LOGGER.info("create shiro sesisonId:"+sessionId.toString()); return sessionId; @@ -79,7 +81,8 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO { protected Session doReadSession(Serializable serializable) { LOGGER.info("Read shiro sessionID:"+serializable.toString()); //此方法不会执行,不用管 - return (Session)redisTemplate.opsForValue().get(serializable.toString()); + Session session = (Session)redisTemplate.opsForValue().get((K) serializable); + return session; } protected InputStream getCacheMangerConfigFileImputStream(){ @@ -91,4 +94,6 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO { throw new ConfigurationException("Unable to obtain input stram for cacheMangerConfigFile["+configFile+"]",e); } } + + } diff --git a/src/main/java/com/lq/code/cache/RedisShiroCache.java b/src/main/java/com/lq/code/cache/RedisShiroCache.java index 95155db..ac3d14f 100644 --- a/src/main/java/com/lq/code/cache/RedisShiroCache.java +++ b/src/main/java/com/lq/code/cache/RedisShiroCache.java @@ -18,18 +18,11 @@ public class RedisShiroCache implements Cache{ @Autowired private RedisTemplate redisTemplate; - public RedisTemplate getRedisTemplate() { - return redisTemplate; - } - - public void setRedisTemplate(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } @Override public V get(K k) throws CacheException { - - return redisTemplate.opsForValue().get(k); + V v = redisTemplate.opsForValue().get(k); + return v; } @Override diff --git a/src/main/java/com/lq/code/executor/package-info.java b/src/main/java/com/lq/code/executor/package-info.java deleted file mode 100755 index 5b07d6c..0000000 --- a/src/main/java/com/lq/code/executor/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Created by qi_liang on 2018/6/1. - */ -package com.lq.code.executor; \ No newline at end of file diff --git a/src/main/resources/dev/jdbc.properties b/src/main/resources/dev/jdbc.properties index 2087517..cf92f01 100644 --- a/src/main/resources/dev/jdbc.properties +++ b/src/main/resources/dev/jdbc.properties @@ -1,6 +1,6 @@ #--------- jdbc 配置--------- jdbc.driver=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://192.168.31.110:3306/ssm?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8 +jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8 jdbc.user=root jdbc.password=mysql #--------数据库连接池配置 ------- diff --git a/src/main/resources/dev/redis.properties b/src/main/resources/dev/redis.properties index b61b6af..80be888 100644 --- a/src/main/resources/dev/redis.properties +++ b/src/main/resources/dev/redis.properties @@ -1,4 +1,4 @@ -redis.host=192.168.31.19 +redis.host=127.0.0.1 redis.port=6379 redis.pass= redis.timeout=-1 diff --git a/src/main/resources/spring/spring-redis.xml b/src/main/resources/spring/spring-redis.xml index d7df648..cfe5d88 100644 --- a/src/main/resources/spring/spring-redis.xml +++ b/src/main/resources/spring/spring-redis.xml @@ -82,7 +82,7 @@ - + diff --git a/src/main/resources/spring/spring-shiro.xml b/src/main/resources/spring/spring-shiro.xml index 39fb94d..43a7aa1 100644 --- a/src/main/resources/spring/spring-shiro.xml +++ b/src/main/resources/spring/spring-shiro.xml @@ -19,7 +19,6 @@ - diff --git a/src/main/webapp/WEB-INF/views/jsp/cms/login.jsp b/src/main/webapp/WEB-INF/views/jsp/cms/login.jsp index 0357d4f..8282f39 100755 --- a/src/main/webapp/WEB-INF/views/jsp/cms/login.jsp +++ b/src/main/webapp/WEB-INF/views/jsp/cms/login.jsp @@ -303,6 +303,8 @@