飞滴出行网约车2022-乘客服务 双token接口设计和代码改造

master
yh 3 years ago
parent c87c7bb993
commit 23178477c6

@ -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 中是否存在值

@ -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);

@ -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";
}

@ -5,6 +5,8 @@ import lombok.Data;
@Data
public class TokenResponse {
private String token;
private String accessToken;
private String refreshToken;
}

@ -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";

@ -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();
}
}
Loading…
Cancel
Save