Shiro + SSM(框架) + Freemarker(jsp)讲解的权限控制Demo,还不赶快去下载?

Shiro + SSM(框架) + Freemarker(jsp)讲解的权限控制Demo,还不赶快去下载?

【转】http://www.sojson.com/blog/137.html

Shiro  我们通过重写AbstractSessionDAO ,来实现 Session  共享。再重写 Session  的时候(其实也不算重写),因为和HttpSession 没有任何实现或者继承关系。

首先 Shiro   Session  配置讲解。

Session  的每个回话的ID 生成器,我们用JavaUuidSessionIdGenerator (UUID 规则)。

 
  1. <!-- 会话Session ID生成器 -->
  2. <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>

Session  的创建、获取、删除

 
  1. <!-- session 创建、删除、查询 -->
  2. <bean id="jedisShiroSessionRepository" class="com.sojson.core.shiro.cache.JedisShiroSessionRepository" >
  3. <property name="jedisManager" ref="jedisManager"/>
  4. </bean>

Session  的监听生命周期

 
  1. <!-- custom shiro session listener -->
  2. <bean id="customShiroSessionDAO" class="com.sojson.core.shiro.CustomShiroSessionDAO">
  3. <property name="shiroSessionRepository" ref="jedisShiroSessionRepository"/>
  4. <property name="sessionIdGenerator" ref="sessionIdGenerator"/>
  5. </bean>

Session  定时管理器(有效期)

 
  1. <!-- 会话验证调度器 -->
  2. <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler">
  3. <property name="interval" value="${session.validate.timespan}"/><!--检测时间间距,默认是60分钟-->
  4. <property name="sessionManager" ref="sessionManager"/>
  5. </bean>

Session   cookie  模版配置

 
  1. <!-- 会话Cookie模板 -->
  2. <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
  3. <!--cookie的name,我故意取名叫xxxxbaidu -->
  4. <constructor-arg value="v_v-s-baidu"/>
  5. <property name="httpOnly" value="true"/>
  6. <!--cookie的有效时间 -->
  7. <property name="maxAge" value="-1"/>
  8. <!-- 配置存储Session Cookie的domain为 一级域名 -->
  9. <property name="domain" value=".itboy.net"/>
  10. </bean>

Session  Manager 配置

 
  1. <!-- Session Manager -->
  2. <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  3. <!-- 相隔多久检查一次session的有效性 -->
  4. <property name="sessionValidationInterval" value="1800000"/>
  5. <!-- session 有效时间为半小时 (毫秒单位)-->
  6. <property name="globalSessionTimeout" value="1800000"/>
  7. <property name="sessionDAO" ref="customShiroSessionDAO"/>
  8. <!-- session 监听,可以多个。 -->
  9. <property name="sessionListeners">
  10. <list>
  11. <ref bean="customSessionListener"/>
  12. </list>
  13. </property>
  14. <!-- 间隔多少时间检查,不配置是60分钟 -->
  15. <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
  16. <!-- 是否开启 检测,默认开启 -->
  17. <property name="sessionValidationSchedulerEnabled" value="true"/>
  18. <!-- 是否删除无效的,默认也是开启 -->
  19. <property name="deleteInvalidSessions" value="true"/>
  20. <!-- 会话Cookie模板 -->
  21. <property name="sessionIdCookie" ref="sessionIdCookie"/>
  22. </bean>

Session  的创建、删除、查询 ,ShiroSessionRepository 接口定义。

 
  1. package com.sojson.core.shiro.session;
  2. import org.apache.shiro.session.Session;
  3. import java.io.Serializable;
  4. import java.util.Collection;
  5. /**
  6. * custom shiro session manager interface
  7. *
  8. * @author zhoubaicheng
  9. */
  10. public interface ShiroSessionRepository {
  11. /**
  12. * 存储Session
  13. * @param session
  14. */
  15. void saveSession(Session session);
  16. /**
  17. * 删除session
  18. * @param sessionId
  19. */
  20. void deleteSession(Serializable sessionId);
  21. /**
  22. * 获取session
  23. * @param sessionId
  24. * @return
  25. */
  26. Session getSession(Serializable sessionId);
  27. /**
  28. * 获取所有sessoin
  29. * @return
  30. */
  31. Collection<Session> getAllSessions();
  32. }

Session  的创建、删除、查询实现。com.sojson.core.shiro.cache.JedisShiroSessionRepository 

 
  1. package com.sojson.core.shiro.cache;
  2. import java.io.Serializable;
  3. import java.util.Collection;
  4. import org.apache.log4j.Logger;
  5. import org.apache.shiro.session.Session;
  6. import com.sojson.common.utils.SerializeUtil;
  7. import com.sojson.core.shiro.session.ShiroSessionRepository;
  8. /**
  9. * Session 管理
  10. * @author sojson.com
  11. *
  12. */
  13. @SuppressWarnings("unchecked")
  14. public class JedisShiroSessionRepository implements ShiroSessionRepository {
  15. private static Logger logger = Logger.getLogger(JedisShiroSessionRepository.class);
  16. public static final String REDIS_SHIRO_SESSION = "sojson-shiro-session:";
  17. //这里有个小BUG,因为Redis使用序列化后,Key反序列化回来发现前面有一段乱码,解决的办法是存储缓存不序列化
  18. public static final String REDIS_SHIRO_ALL = "*sojson-shiro-session:*";
  19. private static final int SESSION_VAL_TIME_SPAN = 18000;
  20. private static final int DB_INDEX = 1;
  21. private JedisManager jedisManager;
  22. @Override
  23. public void saveSession(Session session) {
  24. if (session == null || session.getId() == null)
  25. throw new NullPointerException("session is empty");
  26. try {
  27. byte[] key = SerializeUtil.serialize(buildRedisSessionKey(session.getId()));
  28. byte[] value = SerializeUtil.serialize(session);
  29. long sessionTimeOut = session.getTimeout() / 1000;
  30. Long expireTime = sessionTimeOut + SESSION_VAL_TIME_SPAN + (5 * 60);
  31. getJedisManager().saveValueByKey(DB_INDEX, key, value, expireTime.intValue());
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. System.out.println("save session error");
  35. }
  36. }
  37. @Override
  38. public void deleteSession(Serializable id) {
  39. if (id == null) {
  40. throw new NullPointerException("session id is empty");
  41. }
  42. try {
  43. getJedisManager().deleteByKey(DB_INDEX,
  44. SerializeUtil.serialize(buildRedisSessionKey(id)));
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. System.out.println("delete session error");
  48. }
  49. }
  50. @Override
  51. public Session getSession(Serializable id) {
  52. if (id == null)
  53. throw new NullPointerException("session id is empty");
  54. Session session = null;
  55. try {
  56. byte[] value = getJedisManager().getValueByKey(DB_INDEX, SerializeUtil
  57. .serialize(buildRedisSessionKey(id)));
  58. session = SerializeUtil.deserialize(value, Session.class);
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. System.out.println("get session error");
  62. }
  63. return session;
  64. }
  65. @Override
  66. public Collection<Session> getAllSessions() {
  67. Collection<Session> sessions = null;
  68. try {
  69. sessions = getJedisManager().AllSession(DB_INDEX,REDIS_SHIRO_SESSION);
  70. } catch (Exception e) {
  71. logger.error("获取全部session异常");
  72. e.printStackTrace();
  73. }
  74. return sessions;
  75. }
  76. private String buildRedisSessionKey(Serializable sessionId) {
  77. return REDIS_SHIRO_SESSION + sessionId;
  78. }
  79. public JedisManager getJedisManager() {
  80. return jedisManager;
  81. }
  82. public void setJedisManager(JedisManager jedisManager) {
  83. this.jedisManager = jedisManager;
  84. }
  85. }

CustomShiroSessionDAO的继承实现

 
  1. package com.sojson.core.shiro;
  2. import java.io.Serializable;
  3. import java.util.Collection;
  4. import org.apache.log4j.Logger;
  5. import org.apache.shiro.session.Session;
  6. import org.apache.shiro.session.UnknownSessionException;
  7. import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
  8. import com.sojson.core.shiro.session.ShiroSessionRepository;
  9. public class CustomShiroSessionDAO extends AbstractSessionDAO{
  10. private static Logger logger = Logger.getLogger(CustomShiroSessionDAO.class);
  11. private ShiroSessionRepository shiroSessionRepository;
  12. public ShiroSessionRepository getShiroSessionRepository() {
  13. return shiroSessionRepository;
  14. }
  15. public void setShiroSessionRepository(
  16. ShiroSessionRepository shiroSessionRepository) {
  17. this.shiroSessionRepository = shiroSessionRepository;
  18. }
  19. @Override
  20. public void update(Session session) throws UnknownSessionException {
  21. getShiroSessionRepository().saveSession(session);
  22. }
  23. @Override
  24. public void delete(Session session) {
  25. if (session == null) {
  26. logger.error(
  27. "session can not be null,delete failed");
  28. return;
  29. }
  30. Serializable id = session.getId();
  31. if (id != null)
  32. getShiroSessionRepository().deleteSession(id);
  33. }
  34. @Override
  35. public Collection<Session> getActiveSessions() {
  36. return getShiroSessionRepository().getAllSessions();
  37. }
  38. @Override
  39. protected Serializable doCreate(Session session) {
  40. Serializable sessionId = this.generateSessionId(session);
  41. this.assignSessionId(session, sessionId);
  42. getShiroSessionRepository().saveSession(session);
  43. return sessionId;
  44. }
  45. @Override
  46. protected Session doReadSession(Serializable sessionId) {
  47. return getShiroSessionRepository().getSession(sessionId);
  48. } }

 

这样基本就OK了, Redis  配置请看前面的博客。因为我们是使用同一个 Redis  ,所以 Session  是共享的。

posted @ 2017-06-14 16:28  半也  阅读(1313)  评论(0编辑  收藏  举报