SessionManager.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
  3. */
  4. package com.jeeplus.common.security.shiro.session;
  5. import java.io.Serializable;
  6. import java.util.Collection;
  7. import java.util.Date;
  8. import javax.servlet.ServletRequest;
  9. import javax.servlet.ServletResponse;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import org.apache.shiro.session.InvalidSessionException;
  13. import org.apache.shiro.session.Session;
  14. import org.apache.shiro.session.UnknownSessionException;
  15. import org.apache.shiro.session.mgt.SessionContext;
  16. import org.apache.shiro.session.mgt.SessionKey;
  17. import org.apache.shiro.session.mgt.SimpleSession;
  18. import org.apache.shiro.web.servlet.Cookie;
  19. import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
  20. import org.apache.shiro.web.servlet.SimpleCookie;
  21. import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
  22. import org.apache.shiro.web.util.WebUtils;
  23. import com.jeeplus.common.utils.StringUtils;
  24. /**
  25. * 自定义WEB会话管理类
  26. * @author jeeplus
  27. * @version 2014-7-20
  28. */
  29. public class SessionManager extends DefaultWebSessionManager {
  30. public SessionManager() {
  31. super();
  32. }
  33. @Override
  34. protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
  35. // 如果参数中包含“__sid”参数,则使用此sid会话。 例如:http://localhost/project?__sid=xxx&__cookie=true
  36. String sid = request.getParameter("__sid");
  37. if (StringUtils.isNotBlank(sid)) {
  38. // 是否将sid保存到cookie,浏览器模式下使用此参数。
  39. if (WebUtils.isTrue(request, "__cookie")){
  40. HttpServletRequest rq = (HttpServletRequest)request;
  41. HttpServletResponse rs = (HttpServletResponse)response;
  42. Cookie template = getSessionIdCookie();
  43. Cookie cookie = new SimpleCookie(template);
  44. cookie.setValue(sid); cookie.saveTo(rq, rs);
  45. }
  46. // 设置当前session状态
  47. request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE,
  48. ShiroHttpServletRequest.URL_SESSION_ID_SOURCE); // session来源与url
  49. request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, sid);
  50. request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
  51. return sid;
  52. }else{
  53. return super.getSessionId(request, response);
  54. }
  55. }
  56. @Override
  57. public void validateSessions() {
  58. super.validateSessions();
  59. }
  60. protected Session retrieveSession(SessionKey sessionKey) {
  61. try{
  62. return super.retrieveSession(sessionKey);
  63. }catch (UnknownSessionException e) {
  64. // 获取不到SESSION不抛出异常
  65. return null;
  66. }
  67. }
  68. public Date getStartTimestamp(SessionKey key) {
  69. try{
  70. return super.getStartTimestamp(key);
  71. }catch (InvalidSessionException e) {
  72. // 获取不到SESSION不抛出异常
  73. return null;
  74. }
  75. }
  76. public Date getLastAccessTime(SessionKey key) {
  77. try{
  78. return super.getLastAccessTime(key);
  79. }catch (InvalidSessionException e) {
  80. // 获取不到SESSION不抛出异常
  81. return null;
  82. }
  83. }
  84. public long getTimeout(SessionKey key){
  85. try{
  86. return super.getTimeout(key);
  87. }catch (InvalidSessionException e) {
  88. // 获取不到SESSION不抛出异常
  89. return 0;
  90. }
  91. }
  92. public void setTimeout(SessionKey key, long maxIdleTimeInMillis) {
  93. try{
  94. super.setTimeout(key, maxIdleTimeInMillis);
  95. }catch (InvalidSessionException e) {
  96. // 获取不到SESSION不抛出异常
  97. }
  98. }
  99. public void touch(SessionKey key) {
  100. try{
  101. super.touch(key);
  102. }catch (InvalidSessionException e) {
  103. // 获取不到SESSION不抛出异常
  104. }
  105. }
  106. public String getHost(SessionKey key) {
  107. try{
  108. return super.getHost(key);
  109. }catch (InvalidSessionException e) {
  110. // 获取不到SESSION不抛出异常
  111. return null;
  112. }
  113. }
  114. public Collection<Object> getAttributeKeys(SessionKey key) {
  115. try{
  116. return super.getAttributeKeys(key);
  117. }catch (InvalidSessionException e) {
  118. // 获取不到SESSION不抛出异常
  119. return null;
  120. }
  121. }
  122. public Object getAttribute(SessionKey sessionKey, Object attributeKey) {
  123. try{
  124. return super.getAttribute(sessionKey, attributeKey);
  125. }catch (InvalidSessionException e) {
  126. // 获取不到SESSION不抛出异常
  127. return null;
  128. }
  129. }
  130. public void setAttribute(SessionKey sessionKey, Object attributeKey, Object value) {
  131. try{
  132. super.setAttribute(sessionKey, attributeKey, value);
  133. }catch (InvalidSessionException e) {
  134. // 获取不到SESSION不抛出异常
  135. }
  136. }
  137. public Object removeAttribute(SessionKey sessionKey, Object attributeKey) {
  138. try{
  139. return super.removeAttribute(sessionKey, attributeKey);
  140. }catch (InvalidSessionException e) {
  141. // 获取不到SESSION不抛出异常
  142. return null;
  143. }
  144. }
  145. public void stop(SessionKey key) {
  146. try{
  147. super.stop(key);
  148. }catch (InvalidSessionException e) {
  149. // 获取不到SESSION不抛出异常
  150. }
  151. }
  152. public void checkValid(SessionKey key) {
  153. try{
  154. super.checkValid(key);
  155. }catch (InvalidSessionException e) {
  156. // 获取不到SESSION不抛出异常
  157. }
  158. }
  159. @Override
  160. protected Session doCreateSession(SessionContext context) {
  161. try{
  162. return super.doCreateSession(context);
  163. }catch (IllegalStateException e) {
  164. return null;
  165. }
  166. }
  167. @Override
  168. protected Session newSessionInstance(SessionContext context) {
  169. Session session = super.newSessionInstance(context);
  170. session.setTimeout(getGlobalSessionTimeout());
  171. return session;
  172. }
  173. @Override
  174. public Session start(SessionContext context) {
  175. try{
  176. return super.start(context);
  177. }catch (NullPointerException e) {
  178. SimpleSession session = new SimpleSession();
  179. session.setId(0);
  180. return session;
  181. }
  182. }
  183. }