|
@@ -0,0 +1,112 @@
|
|
|
+package com.diagbot.service.impl;
|
|
|
+
|
|
|
+import com.auth0.jwt.interfaces.Claim;
|
|
|
+import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
+import com.diagbot.service.TokenService;
|
|
|
+import com.diagbot.util.DateUtil;
|
|
|
+import com.diagbot.util.JwtUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.dao.DataAccessException;
|
|
|
+import org.springframework.data.redis.connection.RedisConnection;
|
|
|
+import org.springframework.data.redis.core.RedisCallback;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: Token验证类 实现
|
|
|
+ * @author: gaodm
|
|
|
+ * @time: 2018/10/29 13:34
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class TokenServiceImpl implements TokenService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("redisTemplateForToken")
|
|
|
+ RedisTemplate redisForToken;
|
|
|
+
|
|
|
+ private byte[] serializeKey(Object o) {
|
|
|
+ return redisForToken.getKeySerializer().serialize(o);
|
|
|
+ }
|
|
|
+
|
|
|
+ private byte[] serializeValue(Object o) {
|
|
|
+ return redisForToken.getValueSerializer().serialize(o);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object deserializeValue(byte[] b) {
|
|
|
+ return redisForToken.getValueSerializer().deserialize(b);
|
|
|
+ }
|
|
|
+
|
|
|
+ private byte[] getUserTokenKey(String userId) {
|
|
|
+ String userTokensFormat = "user_tokens_%s";
|
|
|
+ return serializeKey(String.format(userTokensFormat, userId));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建token
|
|
|
+ *
|
|
|
+ * @param token 用户token
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean createToken(String token) {
|
|
|
+ DecodedJWT jwt = JwtUtil.decodedJWT(token);
|
|
|
+ Map<String, Claim> claims = jwt.getClaims();
|
|
|
+ String userId = claims.get("user_id").asInt().toString();
|
|
|
+ Date expDate = claims.get("exp").asDate();
|
|
|
+ final byte[] redis_key = getUserTokenKey(userId);
|
|
|
+ redisForToken.execute(new RedisCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ //获取旧的
|
|
|
+ byte[] bytes = connection.get(redis_key);
|
|
|
+ //删除旧的
|
|
|
+ if (bytes != null) {
|
|
|
+ connection.del(bytes);
|
|
|
+ }
|
|
|
+ //设置新的
|
|
|
+ connection.setEx(
|
|
|
+ redis_key,
|
|
|
+ (expDate.getTime() - DateUtil.now().getTime()) / 1000,
|
|
|
+ serializeValue(token)
|
|
|
+ );
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证token是否有效
|
|
|
+ *
|
|
|
+ * @param token 待验证的token
|
|
|
+ * @return 返回token
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean verifyToken(String token) {
|
|
|
+ String userId = JwtUtil.getUserId(token);
|
|
|
+ //从redis中取出
|
|
|
+ final byte[] redis_key = getUserTokenKey(userId);
|
|
|
+ String tokenStore = (String) redisForToken.execute(new RedisCallback<String>() {
|
|
|
+ @Override
|
|
|
+ public String doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ byte[] bytes = connection.get(redis_key);
|
|
|
+ if (bytes == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return (String) deserializeValue(bytes);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (null != tokenStore && tokenStore.equals(token)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|