PageRenderTime 2777ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/211Project/to211-service/src/main/java/com/to211/service/impl/SessionServiceImpl.java

http://java-hiking.googlecode.com/
Java | 99 lines | 79 code | 15 blank | 5 comment | 0 complexity | fac1b4e52328520915cda326332f896e MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. package com.to211.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.proxy.domain.User;
  12. import com.fdhay.authcenter.proxy.service.UserService;
  13. import com.to211.common.Constants;
  14. import com.to211.service.SessionService;
  15. import com.to211.service.base.RedisService;
  16. @Service(value="sessionService")
  17. public class SessionServiceImpl implements SessionService {
  18. protected Log logger = LogFactory.getLog(getClass());
  19. @Resource private RedisService redisService;
  20. @Resource private UserService userService;
  21. private static final String REDIS_LOGIN_USER_KEY_PREFIX = Constants.cfg.getValue("redis.login.user.key.prefix", "redis.login.user.key.");
  22. private static final String REDIS_LOGIN_USER_TIMEOUT_MINUTES = Constants.cfg.getValue("redis.login.user.timeout.minutes", "5");
  23. private static final String REDIS_SESSION_KEY_PREFIX = Constants.cfg.getValue("redis.session.key.prefix", "redis.session.key.");
  24. private static final String REDIS_SESSION_TIMEOUT_MINUTES = Constants.cfg.getValue("redis.session.timeout.minutes", "30");
  25. @Override
  26. public User getLoginUser(String cookie){
  27. try{
  28. Map<String, String> map = redisService.getRedisMap(getRedisMapKeyForLoginUser(cookie));
  29. String userJson = map.get(cookie);
  30. // if(user == null){
  31. // //如果本地redis session中没有该用户,到authcenter认证中心去检测该cookie是否合法。
  32. // user = userService.getLoginUserIfCookieValid(cookie);
  33. // this.addLoginUser(cookie, user);
  34. // }
  35. return JSON.parseObject(userJson, User.class);
  36. }catch(Exception e){
  37. logger.error(e);
  38. }
  39. return null;
  40. }
  41. @Override
  42. public void addLoginUser(String cookie, User user){
  43. try{
  44. String redisMapKey = getRedisMapKeyForLoginUser(cookie);
  45. RedisMap<String, String> map = redisService.getRedisMap(redisMapKey);
  46. map.put(cookie, JSON.toJSONString(user));
  47. int timeout = Integer.parseInt(REDIS_LOGIN_USER_TIMEOUT_MINUTES);
  48. map.expire(timeout, TimeUnit.MINUTES);
  49. }catch(Exception e){
  50. logger.error(e);
  51. }
  52. }
  53. @Override
  54. public Boolean refreshLoginUserTimeoutMinutes(String cookie){
  55. try{
  56. String redisMapKey = getRedisMapKeyForLoginUser(cookie);
  57. RedisMap<String, String> map = redisService.getRedisMap(redisMapKey);
  58. int timeout = Integer.parseInt(REDIS_LOGIN_USER_TIMEOUT_MINUTES);
  59. return map.expire(timeout, TimeUnit.MINUTES);
  60. }catch(Exception e){
  61. logger.error(e);
  62. }
  63. return false;
  64. }
  65. private String getRedisMapKeyForLoginUser(String cookie){
  66. return REDIS_LOGIN_USER_KEY_PREFIX + cookie;
  67. }
  68. @Override
  69. public Map<String, Object> getSessionContext(String cookie){
  70. try{
  71. String redisMapKey = getRedisMapKeyForSession(cookie);
  72. RedisMap<String, Object> map = redisService.getRedisMap(redisMapKey);
  73. int timeout = Integer.parseInt(REDIS_SESSION_TIMEOUT_MINUTES);
  74. map.expire(timeout, TimeUnit.MINUTES);
  75. return map;
  76. }catch(Exception e){
  77. logger.error(e);
  78. }
  79. return new HashMap<String, Object>();
  80. }
  81. private String getRedisMapKeyForSession(String cookie){
  82. return REDIS_SESSION_KEY_PREFIX + cookie;
  83. }
  84. }