From 23178477c62d2da9ba87bed38eb369d8eb277dc2 Mon Sep 17 00:00:00 2001 From: yh <1844516659@qq.com> Date: Sun, 17 Jul 2022 10:04:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=9E=E6=BB=B4=E5=87=BA=E8=A1=8C=E7=BD=91?= =?UTF-8?q?=E7=BA=A6=E8=BD=A62022-=E4=B9=98=E5=AE=A2=E6=9C=8D=E5=8A=A1=20?= =?UTF-8?q?=E5=8F=8Ctoken=E6=8E=A5=E5=8F=A3=E8=AE=BE=E8=AE=A1=E5=92=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/JwtInterceptor.java | 4 ++- .../service/VerificationCodeService.java | 24 ++++++++++------ .../constant/TokenConstants.java | 11 ++++++++ .../response/TokenResponse.java | 4 ++- .../internalcommon/util/JwtUtils.java | 17 ++--------- .../internalcommon/util/RedisPrefixUtils.java | 28 +++++++++++++++++++ 6 files changed, 64 insertions(+), 24 deletions(-) create mode 100644 internal-common/src/main/java/com/mashibing/internalcommon/constant/TokenConstants.java create mode 100644 internal-common/src/main/java/com/mashibing/internalcommon/util/RedisPrefixUtils.java diff --git a/api-passenger/src/main/java/com/mashibing/apipassenger/interceptor/JwtInterceptor.java b/api-passenger/src/main/java/com/mashibing/apipassenger/interceptor/JwtInterceptor.java index 676be08..c7a090f 100644 --- a/api-passenger/src/main/java/com/mashibing/apipassenger/interceptor/JwtInterceptor.java +++ b/api-passenger/src/main/java/com/mashibing/apipassenger/interceptor/JwtInterceptor.java @@ -3,9 +3,11 @@ package com.mashibing.apipassenger.interceptor; import com.auth0.jwt.exceptions.AlgorithmMismatchException; import com.auth0.jwt.exceptions.SignatureVerificationException; import com.auth0.jwt.exceptions.TokenExpiredException; +import com.mashibing.internalcommon.constant.TokenConstants; import com.mashibing.internalcommon.dto.ResponseResult; import com.mashibing.internalcommon.dto.TokenResult; import com.mashibing.internalcommon.util.JwtUtils; +import com.mashibing.internalcommon.util.RedisPrefixUtils; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -66,7 +68,7 @@ public class JwtInterceptor implements HandlerInterceptor { String phone = tokenResult.getPhone(); String indentiny = tokenResult.getIndentiny(); // 从redis中取出token - String rdisTokenKey = JwtUtils.getRdisTokenKey(phone, indentiny); + String rdisTokenKey = RedisPrefixUtils.getRdisTokenKey(phone, indentiny,TokenConstants.ACCESS_TOKEN_TYPE); String redisToken = redisTemplate.opsForValue().get(rdisTokenKey); // 判断 redis 中是否存在值 diff --git a/api-passenger/src/main/java/com/mashibing/apipassenger/service/VerificationCodeService.java b/api-passenger/src/main/java/com/mashibing/apipassenger/service/VerificationCodeService.java index 41ca7e0..2e1652b 100644 --- a/api-passenger/src/main/java/com/mashibing/apipassenger/service/VerificationCodeService.java +++ b/api-passenger/src/main/java/com/mashibing/apipassenger/service/VerificationCodeService.java @@ -4,11 +4,13 @@ import com.mashibing.apipassenger.remote.ServicePassengerUserCLient; import com.mashibing.apipassenger.remote.ServiceVerificationcodeClient; import com.mashibing.internalcommon.constant.CommonStatusEnum; import com.mashibing.internalcommon.constant.IndentinyConstant; +import com.mashibing.internalcommon.constant.TokenConstants; import com.mashibing.internalcommon.dto.ResponseResult; import com.mashibing.internalcommon.request.VerificationCodeDTO; import com.mashibing.internalcommon.response.NumberCodeResponse; import com.mashibing.internalcommon.response.TokenResponse; import com.mashibing.internalcommon.util.JwtUtils; +import com.mashibing.internalcommon.util.RedisPrefixUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; @@ -42,7 +44,7 @@ public class VerificationCodeService { String numberCode = numberCodeResponse.getData().getNumberCode(); // 存入redis - String redisKey = JwtUtils.getRdisVeriCodeKey(passengerPhone); + String redisKey = RedisPrefixUtils.getRdisVeriCodeKey(passengerPhone); redisTemplate.opsForValue().set(redisKey,numberCode,2, TimeUnit.MINUTES); // 返回处理结果 @@ -58,7 +60,7 @@ public class VerificationCodeService { public ResponseResult checkCode(String passengerPhone,String verificationCode){ // 根据 key前缀+号码 从redis取出对应 校验码比较 - String redisVeriCodeKey = JwtUtils.getRdisVeriCodeKey(passengerPhone); + String redisVeriCodeKey = RedisPrefixUtils.getRdisVeriCodeKey(passengerPhone); String codeRedis = redisTemplate.opsForValue().get(redisVeriCodeKey); // 校验 验证码 是否相同 @@ -73,14 +75,20 @@ public class VerificationCodeService { redisTemplate.opsForValue().set(redisVeriCodeKey,""); Boolean delete = redisTemplate.delete(redisVeriCodeKey);// 使用后删除key - // 颁布 token 令牌 - String token = JwtUtils.gerneratorToken(passengerPhone, IndentinyConstant.PASSENGER_IDENTITY); - // 将 token 存入 redis 中 - String redisTokenKey = JwtUtils.getRdisTokenKey(passengerPhone, IndentinyConstant.PASSENGER_IDENTITY); - redisTemplate.opsForValue().set(redisTokenKey,token,30,TimeUnit.DAYS); + // 颁布 accessToken & refreshToken 令牌 + String accessToken = JwtUtils.gerneratorToken(passengerPhone, IndentinyConstant.PASSENGER_IDENTITY,TokenConstants.ACCESS_TOKEN_TYPE); + String refreshToken = JwtUtils.gerneratorToken(passengerPhone, IndentinyConstant.PASSENGER_IDENTITY,TokenConstants.REFRESH_TOKEN_TYPE); + + // 将 accessToken & refreshToken 存入 redis 中 + String redisAccessTokenKey = RedisPrefixUtils.getRdisTokenKey(passengerPhone, IndentinyConstant.PASSENGER_IDENTITY,TokenConstants.ACCESS_TOKEN_TYPE); + String redisRefreshTokenKey = RedisPrefixUtils.getRdisTokenKey(passengerPhone, IndentinyConstant.PASSENGER_IDENTITY,TokenConstants.REFRESH_TOKEN_TYPE); + redisTemplate.opsForValue().set(redisAccessTokenKey,accessToken,30,TimeUnit.DAYS); + redisTemplate.opsForValue().set(redisRefreshTokenKey,refreshToken,31,TimeUnit.DAYS); + TokenResponse tokenResponse = new TokenResponse(); - tokenResponse.setToken(token); + tokenResponse.setAccessToken(accessToken); + tokenResponse.setRefreshToken(refreshToken); // 返回处理结果 return ResponseResult.success(tokenResponse); diff --git a/internal-common/src/main/java/com/mashibing/internalcommon/constant/TokenConstants.java b/internal-common/src/main/java/com/mashibing/internalcommon/constant/TokenConstants.java new file mode 100644 index 0000000..e4ad9a4 --- /dev/null +++ b/internal-common/src/main/java/com/mashibing/internalcommon/constant/TokenConstants.java @@ -0,0 +1,11 @@ +package com.mashibing.internalcommon.constant; + +/** + * token 类型常量类 + */ +public class TokenConstants { + + public static final String ACCESS_TOKEN_TYPE = "accessToken"; + public static final String REFRESH_TOKEN_TYPE = "refreshToken"; + +} diff --git a/internal-common/src/main/java/com/mashibing/internalcommon/response/TokenResponse.java b/internal-common/src/main/java/com/mashibing/internalcommon/response/TokenResponse.java index 3bb3e65..6e23aac 100644 --- a/internal-common/src/main/java/com/mashibing/internalcommon/response/TokenResponse.java +++ b/internal-common/src/main/java/com/mashibing/internalcommon/response/TokenResponse.java @@ -5,6 +5,8 @@ import lombok.Data; @Data public class TokenResponse { - private String token; + private String accessToken; + + private String refreshToken; } diff --git a/internal-common/src/main/java/com/mashibing/internalcommon/util/JwtUtils.java b/internal-common/src/main/java/com/mashibing/internalcommon/util/JwtUtils.java index 638ecf8..7c6d624 100644 --- a/internal-common/src/main/java/com/mashibing/internalcommon/util/JwtUtils.java +++ b/internal-common/src/main/java/com/mashibing/internalcommon/util/JwtUtils.java @@ -17,16 +17,13 @@ public class JwtUtils { private static final String JWT_KEY_PHONE = "phone"; - private static final String JWT_KEY_INDENTINY = "indentiny"; - - private static final String veriCodePrefix = "passenger-verification-code-"; - private static final String tokenPrefix = "passenger-verification-code-"; + private static final String JWT_TOKEN_TYPE = "tokenType"; /** * 获取 token字符串 */ - public static String gerneratorToken(String passengerPhone,String indentiny){ + public static String gerneratorToken(String passengerPhone,String indentiny,String tokenType){ // // 准备 token过期时间 Date 类型 -移交服务端进行控制 // Calendar calendar = Calendar.getInstance(); @@ -39,6 +36,7 @@ public class JwtUtils { // 将 有效数据部份 合成到 builder中 builder.withClaim(JWT_KEY_PHONE,passengerPhone); builder.withClaim(JWT_KEY_INDENTINY,indentiny); + builder.withClaim(JWT_TOKEN_TYPE,tokenType); // 设置 超时时间 -移交服务端进行控制 // builder.withExpiresAt(date); @@ -68,15 +66,6 @@ public class JwtUtils { return result; } - // 获取 验证码存入redis中的 key - public static String getRdisVeriCodeKey(String passengerPhone){ - return veriCodePrefix.trim() + passengerPhone.trim(); - } - // 获取 token 存入redis中的 key - public static String getRdisTokenKey(String phone,String indentiny){ - return tokenPrefix.trim() + phone.trim() + indentiny.trim(); - } - // public static void main (String [ ] args ) { // // String tmpPhone = "13912345678"; diff --git a/internal-common/src/main/java/com/mashibing/internalcommon/util/RedisPrefixUtils.java b/internal-common/src/main/java/com/mashibing/internalcommon/util/RedisPrefixUtils.java new file mode 100644 index 0000000..ace5c96 --- /dev/null +++ b/internal-common/src/main/java/com/mashibing/internalcommon/util/RedisPrefixUtils.java @@ -0,0 +1,28 @@ +package com.mashibing.internalcommon.util; + +public class RedisPrefixUtils { + + private static final String veriCodePrefix = "passenger-verification-code-"; + private static final String tokenPrefix = "token-"; + + /** + * 根据手机号 获取 验证码存入redis中的 key + * @param passengerPhone + * @return + */ + public static String getRdisVeriCodeKey(String passengerPhone){ + return veriCodePrefix.trim() + passengerPhone.trim(); + } + + /** + * 根据 有效信息 获取 token 存入redis中的 key + * @param phone + * @param indentiny + * @param tokeType + * @return + */ + public static String getRdisTokenKey(String phone,String indentiny,String tokeType){ + return tokenPrefix.trim() + phone.trim() +"-"+ indentiny.trim() +"-"+ tokeType.trim(); + } + +}