PageRenderTime 669ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/211AuthcenterProject/authcenter-service/src/main/java/com/fdhay/authcenter/service/impl/SessionServiceImpl.java

http://java-hiking.googlecode.com/
Java | 100 lines | 87 code | 13 blank | 0 comment | 0 complexity | 668ca9c0c9b9e1f578553737a43955dd MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. package com.fdhay.authcenter.service.impl;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.concurrent.TimeUnit;
  5. import javax.annotation.Resource;
  6. import org.apache.commons.logging.Log;
  7. import org.apache.commons.logging.LogFactory;
  8. import org.springframework.data.redis.support.collections.RedisMap;
  9. import org.springframework.stereotype.Service;
  10. import com.alibaba.fastjson.JSON;
  11. import com.fdhay.authcenter.common.Constants;
  12. import com.fdhay.authcenter.domain.User;
  13. import com.fdhay.authcenter.service.SessionService;
  14. import com.fdhay.authcenter.service.base.RedisService;
  15. @Service(value="sessionService")
  16. public class SessionServiceImpl implements SessionService {
  17. protected Log logger = LogFactory.getLog(getClass());
  18. @Resource private RedisService redisService;
  19. private static final String REDIS_LOGIN_USER_KEY_PREFIX = Constants.cfg.getValue("redis.login.user.key.prefix", "redis.login.user.key.");
  20. private static final String REDIS_SESSION_KEY_PREFIX = Constants.cfg.getValue("redis.session.key.prefix", "redis.session.key.");
  21. private static final String REDIS_LOGIN_USER_TIMEOUT_MINUTES = Constants.cfg.getValue("redis.login.user.timeout.minutes", "5");
  22. private static final String REDIS_SESSION_TIMEOUT_MINUTES = Constants.cfg.getValue("redis.session.timeout.minutes", "30");
  23. @Override
  24. public User getLoginUser(String cookie){
  25. try{
  26. Map<String, String> map = redisService.getRedisMap(getRedisMapKeyForLoginUser(cookie));
  27. User user = JSON.parseObject(map.get(cookie), User.class);
  28. return user;
  29. }catch(Exception e){
  30. logger.error(e);
  31. }
  32. return null;
  33. }
  34. @Override
  35. public void addLoginUser(String cookie, User user){
  36. try{
  37. String redisMapKey = getRedisMapKeyForLoginUser(cookie);
  38. RedisMap<String, String> map = redisService.getRedisMap(redisMapKey);
  39. map.put(cookie, JSON.toJSONString(user));
  40. int timeout = Integer.parseInt(REDIS_LOGIN_USER_TIMEOUT_MINUTES);
  41. map.expire(timeout, TimeUnit.MINUTES);
  42. }catch(Exception e){
  43. logger.error(e);
  44. }
  45. }
  46. @Override
  47. public void invalidLoginUser(String cookie){
  48. try{
  49. String redisMapKey = getRedisMapKeyForLoginUser(cookie);
  50. RedisMap<String, String> map = redisService.getRedisMap(redisMapKey);
  51. map.expire(0, TimeUnit.MINUTES);
  52. }catch(Exception e){
  53. logger.error(e);
  54. }
  55. }
  56. @Override
  57. public Boolean refreshLoginUserTimeoutMinutes(String cookie){
  58. try{
  59. String redisMapKey = getRedisMapKeyForLoginUser(cookie);
  60. RedisMap<String, String> map = redisService.getRedisMap(redisMapKey);
  61. int timeout = Integer.parseInt(REDIS_LOGIN_USER_TIMEOUT_MINUTES);
  62. return map.expire(timeout, TimeUnit.MINUTES);
  63. }catch(Exception e){
  64. logger.error(e);
  65. }
  66. return false;
  67. }
  68. private String getRedisMapKeyForLoginUser(String cookie){
  69. return REDIS_LOGIN_USER_KEY_PREFIX + cookie;
  70. }
  71. @Override
  72. public Map<String, Object> getSessionContext(String cookie){
  73. try{
  74. String redisMapKey = getRedisMapKeyForSession(cookie);
  75. RedisMap<String, Object> map = redisService.getRedisMap(redisMapKey);
  76. int timeout = Integer.parseInt(REDIS_SESSION_TIMEOUT_MINUTES);
  77. map.expire(timeout, TimeUnit.MINUTES);
  78. return map;
  79. }catch(Exception e){
  80. logger.error(e);
  81. }
  82. return new HashMap<String, Object>();
  83. }
  84. private String getRedisMapKeyForSession(String cookie){
  85. return REDIS_SESSION_KEY_PREFIX + cookie;
  86. }
  87. }