/211AuthcenterProject/authcenter-service/src/main/java/com/fdhay/authcenter/service/impl/SessionServiceImpl.java
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
- package com.fdhay.authcenter.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.common.Constants;
- import com.fdhay.authcenter.domain.User;
- import com.fdhay.authcenter.service.SessionService;
- import com.fdhay.authcenter.service.base.RedisService;
-
- @Service(value="sessionService")
- public class SessionServiceImpl implements SessionService {
- protected Log logger = LogFactory.getLog(getClass());
-
- @Resource private RedisService redisService;
- 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_SESSION_KEY_PREFIX = Constants.cfg.getValue("redis.session.key.prefix", "redis.session.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_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));
- User user = JSON.parseObject(map.get(cookie), User.class);
- return user;
- }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 void invalidLoginUser(String cookie){
- try{
- String redisMapKey = getRedisMapKeyForLoginUser(cookie);
- RedisMap<String, String> map = redisService.getRedisMap(redisMapKey);
- map.expire(0, 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;
- }
- }