/211Project/to211-service/src/main/java/com/to211/service/impl/SessionServiceImpl.java
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
- package com.to211.service.impl;
-
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
-
- import javax.annotation.Resource;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.data.redis.support.collections.RedisMap;
- import org.springframework.stereotype.Service;
-
- import com.alibaba.fastjson.JSON;
- import com.fdhay.authcenter.proxy.domain.User;
- import com.fdhay.authcenter.proxy.service.UserService;
- import com.to211.common.Constants;
- import com.to211.service.SessionService;
- import com.to211.service.base.RedisService;
-
- @Service(value="sessionService")
- public class SessionServiceImpl implements SessionService {
- protected Log logger = LogFactory.getLog(getClass());
-
- @Resource private RedisService redisService;
- @Resource private UserService userService;
-
- private static final String REDIS_LOGIN_USER_KEY_PREFIX = Constants.cfg.getValue("redis.login.user.key.prefix", "redis.login.user.key.");
- private static final String REDIS_LOGIN_USER_TIMEOUT_MINUTES = Constants.cfg.getValue("redis.login.user.timeout.minutes", "5");
-
- private static final String REDIS_SESSION_KEY_PREFIX = Constants.cfg.getValue("redis.session.key.prefix", "redis.session.key.");
- private static final String REDIS_SESSION_TIMEOUT_MINUTES = Constants.cfg.getValue("redis.session.timeout.minutes", "30");
-
- @Override
- public User getLoginUser(String cookie){
- try{
- Map<String, String> map = redisService.getRedisMap(getRedisMapKeyForLoginUser(cookie));
- String userJson = map.get(cookie);
- // if(user == null){
- // //如果本地redis session中没有该用户,到authcenter认证中心去检测该cookie是否合法。
- // user = userService.getLoginUserIfCookieValid(cookie);
- // this.addLoginUser(cookie, user);
- // }
- return JSON.parseObject(userJson, User.class);
- }catch(Exception e){
- logger.error(e);
- }
- return null;
- }
-
- @Override
- public void addLoginUser(String cookie, User user){
- try{
- String redisMapKey = getRedisMapKeyForLoginUser(cookie);
- RedisMap<String, String> map = redisService.getRedisMap(redisMapKey);
- map.put(cookie, JSON.toJSONString(user));
- int timeout = Integer.parseInt(REDIS_LOGIN_USER_TIMEOUT_MINUTES);
- map.expire(timeout, TimeUnit.MINUTES);
- }catch(Exception e){
- logger.error(e);
- }
- }
-
- @Override
- public Boolean refreshLoginUserTimeoutMinutes(String cookie){
- try{
- String redisMapKey = getRedisMapKeyForLoginUser(cookie);
- RedisMap<String, String> map = redisService.getRedisMap(redisMapKey);
- int timeout = Integer.parseInt(REDIS_LOGIN_USER_TIMEOUT_MINUTES);
- return map.expire(timeout, TimeUnit.MINUTES);
- }catch(Exception e){
- logger.error(e);
- }
- return false;
- }
-
- private String getRedisMapKeyForLoginUser(String cookie){
- return REDIS_LOGIN_USER_KEY_PREFIX + cookie;
- }
-
-
- @Override
- public Map<String, Object> getSessionContext(String cookie){
- try{
- String redisMapKey = getRedisMapKeyForSession(cookie);
- RedisMap<String, Object> map = redisService.getRedisMap(redisMapKey);
- int timeout = Integer.parseInt(REDIS_SESSION_TIMEOUT_MINUTES);
- map.expire(timeout, TimeUnit.MINUTES);
- return map;
- }catch(Exception e){
- logger.error(e);
- }
- return new HashMap<String, Object>();
- }
-
- private String getRedisMapKeyForSession(String cookie){
- return REDIS_SESSION_KEY_PREFIX + cookie;
- }
- }