SessionAuthorization.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.yingying.tourist.request;
  2. import org.springframework.beans.factory.InitializingBean;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.data.redis.core.ValueOperations;
  6. import org.springframework.stereotype.Component;
  7. import java.util.concurrent.TimeUnit;
  8. @Component
  9. public class SessionAuthorization implements InitializingBean {
  10. public static final String SESSION_KEY_COOKIE_NAME = "jituan";
  11. public static final int DEFAULT_SESSION_COOKIE_EXPIRE = 604800; // 604800 604800
  12. public static final String SESSION_KEY_PREFIX = "WeChatToken";
  13. @Autowired
  14. private RedisTemplate<Object, Object> redisTemplate;
  15. private static RedisTemplate<Object, Object> baseRedisTemplate;
  16. public static void setSession(String token) {
  17. ValueOperations<Object, Object> ops = baseRedisTemplate.opsForValue();
  18. ops.set(SESSION_KEY_PREFIX + ":" + RequestSessionKey.getSessionKey(), token, DEFAULT_SESSION_COOKIE_EXPIRE, TimeUnit.SECONDS);
  19. }
  20. public static Integer getSession() {
  21. ValueOperations<Object, Object> ops = baseRedisTemplate.opsForValue();
  22. Integer sessionValue = (Integer) ops.get(SESSION_KEY_PREFIX + ":" + RequestSessionKey.getSessionKey());
  23. return sessionValue;
  24. }
  25. public static void removeSession() {
  26. baseRedisTemplate.delete(SESSION_KEY_PREFIX + ":" + RequestSessionKey.getSessionKey());
  27. }
  28. @Override
  29. public void afterPropertiesSet() throws Exception {
  30. baseRedisTemplate = this.redisTemplate;
  31. }
  32. }