/trunk/src/com/mycms/core/manager/impl/AuthenticationMngImpl.java

https://gitlab.com/BGCX262/zyzweb-svn-to-git · Java · 174 lines · 124 code · 23 blank · 27 comment · 10 complexity · 3a555bd34f954de6ef9588ebdd7b2a8e MD5 · raw file

  1. package com.mycms.core.manager.impl;
  2. import java.sql.Timestamp;
  3. import java.util.Date;
  4. import java.util.UUID;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.apache.commons.lang.StringUtils;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import com.mycms.common.page.Pagination;
  14. import com.mycms.common.security.BadCredentialsException;
  15. import com.mycms.common.security.UsernameNotFoundException;
  16. import com.mycms.common.web.session.SessionProvider;
  17. import com.mycms.core.dao.AuthenticationDao;
  18. import com.mycms.core.entity.Authentication;
  19. import com.mycms.core.entity.UnifiedUser;
  20. import com.mycms.core.manager.AuthenticationMng;
  21. import com.mycms.core.manager.UnifiedUserMng;
  22. @Service
  23. @Transactional
  24. public class AuthenticationMngImpl implements AuthenticationMng {
  25. private Logger log = LoggerFactory.getLogger(AuthenticationMngImpl.class);
  26. public Authentication login(String username, String password, String ip,
  27. HttpServletRequest request, HttpServletResponse response,
  28. SessionProvider session) throws UsernameNotFoundException,
  29. BadCredentialsException {
  30. UnifiedUser user = unifiedUserMng.login(username, password, ip);
  31. Authentication auth = new Authentication();
  32. auth.setUid(user.getId());
  33. auth.setUsername(user.getUsername());
  34. auth.setEmail(user.getEmail());
  35. auth.setLoginIp(ip);
  36. save(auth);
  37. session.setAttribute(request, response, AUTH_KEY, auth.getId());
  38. return auth;
  39. }
  40. public Authentication retrieve(String authId) {
  41. long current = System.currentTimeMillis();
  42. // 是否刷新数据库
  43. if (refreshTime < current) {
  44. refreshTime = getNextRefreshTime(current, interval);
  45. int count = dao.deleteExpire(new Date(current - timeout));
  46. log.info("refresh Authentication, delete count: {}", count);
  47. }
  48. Authentication auth = findById(authId);
  49. if (auth != null && auth.getUpdateTime().getTime() + timeout > current) {
  50. auth.setUpdateTime(new Timestamp(current));
  51. return auth;
  52. } else {
  53. return null;
  54. }
  55. }
  56. public Integer retrieveUserIdFromSession(SessionProvider session,
  57. HttpServletRequest request) {
  58. String authId = (String) session.getAttribute(request, AUTH_KEY);
  59. if (authId == null) {
  60. return null;
  61. }
  62. Authentication auth = retrieve(authId);
  63. if (auth == null) {
  64. return null;
  65. }
  66. return auth.getUid();
  67. }
  68. public void storeAuthIdToSession(SessionProvider session,
  69. HttpServletRequest request, HttpServletResponse response,
  70. String authId) {
  71. session.setAttribute(request, response, AUTH_KEY, authId);
  72. }
  73. @Transactional(readOnly = true)
  74. public Pagination getPage(int pageNo, int pageSize) {
  75. Pagination page = dao.getPage(pageNo, pageSize);
  76. return page;
  77. }
  78. @Transactional(readOnly = true)
  79. public Authentication findById(String id) {
  80. Authentication entity = dao.findById(id);
  81. return entity;
  82. }
  83. public Authentication save(Authentication bean) {
  84. bean.setId(StringUtils.remove(UUID.randomUUID().toString(), '-'));
  85. bean.init();
  86. dao.save(bean);
  87. return bean;
  88. }
  89. public Authentication deleteById(String id) {
  90. Authentication bean = dao.deleteById(id);
  91. return bean;
  92. }
  93. public Authentication[] deleteByIds(String[] ids) {
  94. Authentication[] beans = new Authentication[ids.length];
  95. for (int i = 0, len = ids.length; i < len; i++) {
  96. beans[i] = deleteById(ids[i]);
  97. }
  98. return beans;
  99. }
  100. // 过期时间
  101. private int timeout = 30 * 60 * 1000; // 30分钟
  102. // 间隔时间
  103. private int interval = 4 * 60 * 60 * 1000; // 4小时
  104. // 刷新时间。
  105. private long refreshTime = getNextRefreshTime(System.currentTimeMillis(),
  106. this.interval);
  107. private UnifiedUserMng unifiedUserMng;
  108. private AuthenticationDao dao;
  109. @Autowired
  110. public void setDao(AuthenticationDao dao) {
  111. this.dao = dao;
  112. }
  113. @Autowired
  114. public void setUserMng(UnifiedUserMng unifiedUserMng) {
  115. this.unifiedUserMng = unifiedUserMng;
  116. }
  117. /**
  118. * 设置认证过期时间。默认30分钟。
  119. *
  120. * @param timeout
  121. * 单位分钟
  122. */
  123. public void setTimeout(int timeout) {
  124. this.timeout = timeout * 60 * 1000;
  125. }
  126. /**
  127. * 设置刷新数据库时间。默认4小时。
  128. *
  129. * @param interval
  130. * 单位分钟
  131. */
  132. public void setInterval(int interval) {
  133. this.interval = interval * 60 * 1000;
  134. this.refreshTime = getNextRefreshTime(System.currentTimeMillis(),
  135. this.interval);
  136. }
  137. /**
  138. * 获得下一个刷新时间。
  139. *
  140. *
  141. *
  142. * @param current
  143. * @param interval
  144. * @return 随机间隔时间
  145. */
  146. private long getNextRefreshTime(long current, int interval) {
  147. return current + interval;
  148. // 为了防止多个应用同时刷新,间隔时间=interval+RandomUtils.nextInt(interval/4);
  149. // return current + interval + RandomUtils.nextInt(interval / 4);
  150. }
  151. }