| | |
| | | package com.dg.core.manager; |
| | | |
| | | import com.dg.core.Constant; |
| | | import io.jsonwebtoken.Claims; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.core.script.DigestUtils; |
| | | import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.UUID; |
| | | import java.util.concurrent.TimeUnit; |
| | |
| | | * 通过Redis存储和验证token的实现类 |
| | | */ |
| | | @Component |
| | | public class RedisTokenManager implements TokenManager { |
| | | |
| | | public class RedisTokenManager implements TokenManager |
| | | { |
| | | private RedisTemplate<String, String> redis; |
| | | |
| | | @Qualifier("redisTemplate") |
| | |
| | | return token; |
| | | } |
| | | |
| | | |
| | | public boolean checkToken(String token) { |
| | | if (token == null) { |
| | | if (StringUtils.isEmpty(token)) { |
| | | return false; |
| | | } |
| | | String userId = redis.boundValueOps(token).get(); |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 验证花城token |
| | | * @param token |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean checkHCToken(String token) { |
| | | if (StringUtils.isEmpty(token)) { |
| | | return false; |
| | | } |
| | | // token解析 |
| | | Claims claims = JWTTokenUtil.getClaimsFromToken(token); |
| | | if (ObjectUtils.isEmpty(claims)) { |
| | | return false; |
| | | } |
| | | String userId = claims.getSubject(); |
| | | if (ObjectUtils.isEmpty(userId)) |
| | | { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public long getUserId(String token) { |
| | | if (token == null) { |
| | |
| | | return Long.parseLong(userId); |
| | | } |
| | | |
| | | /** |
| | | * 获取花城e+的 UserId |
| | | * @param token |
| | | * @return |
| | | */ |
| | | @Override |
| | | public long getHCUserId(String token) { |
| | | if (token == null) { |
| | | return -1; |
| | | } |
| | | // token解析 |
| | | Claims claims = JWTTokenUtil.getClaimsFromToken(token); |
| | | if (ObjectUtils.isEmpty(claims)) { |
| | | return -1; |
| | | } |
| | | String userId = claims.getSubject(); |
| | | if (ObjectUtils.isEmpty(userId)) |
| | | { |
| | | return -1; |
| | | } |
| | | return Long.parseLong(userId); |
| | | } |
| | | |
| | | public void deleteToken(String token) { |
| | | redis.delete(token); |
| | | } |