修复shiro sessionBUG

master
星期八 5 years ago
parent 602f117364
commit 0c4e8411f1
  1. 23
      src/main/java/com/lq/code/cache/RedisSessionDao.java
  2. 11
      src/main/java/com/lq/code/cache/RedisShiroCache.java
  3. 4
      src/main/java/com/lq/code/executor/package-info.java
  4. 2
      src/main/resources/dev/jdbc.properties
  5. 2
      src/main/resources/dev/redis.properties
  6. 2
      src/main/resources/spring/spring-redis.xml
  7. 1
      src/main/resources/spring/spring-shiro.xml
  8. 2
      src/main/webapp/WEB-INF/views/jsp/cms/login.jsp
  9. 6
      src/main/webapp/WEB-INF/views/jsp/cms/wechat/message/edit.jsp

@ -9,6 +9,7 @@ import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO; import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import java.io.IOException; import java.io.IOException;
@ -19,18 +20,19 @@ import java.util.concurrent.TimeUnit;
/** /**
* Created by qi_liang on 2018/9/11. * Created by qi_liang on 2018/9/11.
*/ */
public class RedisSessionDao extends EnterpriseCacheSessionDAO { public class RedisSessionDao<K,V> extends EnterpriseCacheSessionDAO {
private final static Logger LOGGER = LoggerFactory.getLogger(RedisSessionDao.class); private final static Logger LOGGER = LoggerFactory.getLogger(RedisSessionDao.class);
private RedisTemplate<String,Object> redisTemplate; @Autowired
private RedisTemplate<K,V> redisTemplate;
private int defaultExpireTime ; private int defaultExpireTime ;
private CacheManager cm = null; private CacheManager cm = null;
public RedisSessionDao(RedisTemplate<String,Object> redisTemplate,int defaultExpireTime){ public RedisSessionDao(int defaultExpireTime){
this.redisTemplate = redisTemplate;
this.defaultExpireTime = defaultExpireTime; this.defaultExpireTime = defaultExpireTime;
} }
@ -41,7 +43,7 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO {
//该方法交给父类去执行 //该方法交给父类去执行
// super.doUpdate(session); // super.doUpdate(session);
//更新reids中的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()); cm = new CacheManager(getCacheMangerConfigFileImputStream());
} }
Ehcache ehcache = cm.getCache("sessioncache"); Ehcache ehcache = cm.getCache("sessioncache");
redisTemplate.delete(sessionId.toString()); redisTemplate.delete((K) sessionId);
ehcache.remove(sessionId.toString()); ehcache.remove(sessionId.toString());
} }
@ -68,8 +70,8 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO {
Ehcache ehcache = cm.getCache("sessioncache"); Ehcache ehcache = cm.getCache("sessioncache");
assignSessionId(session,sessionId); assignSessionId(session,sessionId);
redisTemplate.opsForValue().set(sessionId.toString(),session); redisTemplate.opsForValue().set((K) sessionId,(V) session);
redisTemplate.expire(session.getId().toString(),this.defaultExpireTime, TimeUnit.SECONDS); redisTemplate.expire((K) sessionId,this.defaultExpireTime, TimeUnit.SECONDS);
ehcache.put(new Element(sessionId.toString(),session)); ehcache.put(new Element(sessionId.toString(),session));
LOGGER.info("create shiro sesisonId:"+sessionId.toString()); LOGGER.info("create shiro sesisonId:"+sessionId.toString());
return sessionId; return sessionId;
@ -79,7 +81,8 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO {
protected Session doReadSession(Serializable serializable) { protected Session doReadSession(Serializable serializable) {
LOGGER.info("Read shiro sessionID:"+serializable.toString()); 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(){ protected InputStream getCacheMangerConfigFileImputStream(){
@ -91,4 +94,6 @@ public class RedisSessionDao extends EnterpriseCacheSessionDAO {
throw new ConfigurationException("Unable to obtain input stram for cacheMangerConfigFile["+configFile+"]",e); throw new ConfigurationException("Unable to obtain input stram for cacheMangerConfigFile["+configFile+"]",e);
} }
} }
} }

@ -18,18 +18,11 @@ public class RedisShiroCache<K,V> implements Cache<K,V>{
@Autowired @Autowired
private RedisTemplate<K,V> redisTemplate; private RedisTemplate<K,V> redisTemplate;
public RedisTemplate<K, V> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override @Override
public V get(K k) throws CacheException { public V get(K k) throws CacheException {
V v = redisTemplate.opsForValue().get(k);
return redisTemplate.opsForValue().get(k); return v;
} }
@Override @Override

@ -1,4 +0,0 @@
/**
* Created by qi_liang on 2018/6/1.
*/
package com.lq.code.executor;

@ -1,6 +1,6 @@
#--------- jdbc 配置--------- #--------- jdbc 配置---------
jdbc.driver=com.mysql.jdbc.Driver 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.user=root
jdbc.password=mysql jdbc.password=mysql
#--------数据库连接池配置 ------- #--------数据库连接池配置 -------

@ -1,4 +1,4 @@
redis.host=192.168.31.19 redis.host=127.0.0.1
redis.port=6379 redis.port=6379
redis.pass= redis.pass=
redis.timeout=-1 redis.timeout=-1

@ -82,7 +82,7 @@
</property> </property>
<!--开启事务--> <!--开启事务-->
<property name="enableTransactionSupport" value="true"/> <property name="enableTransactionSupport" value="false"/>
</bean> </bean>

@ -19,7 +19,6 @@
<!-- 配置Session DAO的操作处理 --> <!-- 配置Session DAO的操作处理 -->
<bean id="sessionDAO" class="com.lq.code.cache.RedisSessionDao"> <bean id="sessionDAO" class="com.lq.code.cache.RedisSessionDao">
<constructor-arg ref="redisTempale"/>
<constructor-arg value="7200"/> <constructor-arg value="7200"/>
</bean> </bean>

@ -303,6 +303,8 @@
<!-- inline scripts related to this page --> <!-- inline scripts related to this page -->
<script type="text/javascript" src="${ctx}/resources/code/js/jquery.tips.js"></script> <script type="text/javascript" src="${ctx}/resources/code/js/jquery.tips.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var mediator = new Object();
function show_box(id) { function show_box(id) {
jQuery('.widget-box.visible').removeClass('visible'); jQuery('.widget-box.visible').removeClass('visible');
jQuery('#'+id).addClass('visible'); jQuery('#'+id).addClass('visible');

@ -108,16 +108,22 @@
function showMessageTypeInput(messageType) { function showMessageTypeInput(messageType) {
switch (messageType){ switch (messageType){
case '2': case '2':
$("#cropedBigImg").show();
$("#tr_cropedBigImg").show();
$("#tr_imageUrl").show(); $("#tr_imageUrl").show();
$("#tr_cropedBigImg").show(); $("#tr_cropedBigImg").show();
$("#tr_content").hide(); $("#tr_content").hide();
;break; ;break;
case '1': case '1':
$("#cropedBigImg").hide();
$("#tr_cropedBigImg").hide();
$("#tr_imageUrl").hide(); $("#tr_imageUrl").hide();
$("#tr_content").show(); $("#tr_content").show();
$("#tr_cropedBigImg").show(); $("#tr_cropedBigImg").show();
;break; ;break;
case '3': case '3':
$("#cropedBigImg").show();
$("#tr_cropedBigImg").show();
$("#tr_imageUrl").show(); $("#tr_imageUrl").show();
$("#tr_cropedBigImg").show(); $("#tr_cropedBigImg").show();
$("#tr_content").show(); $("#tr_content").show();

Loading…
Cancel
Save