飞滴出行网约车2022-乘客服务 整个验证码获取校验返回token流程

master
yh 3 years ago
parent af9dda5f51
commit d68c0ce4df

@ -3,10 +3,12 @@ package com.mashibing.apipassenger.service;
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.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 net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -75,8 +77,9 @@ public class VerificationCodeService {
redisTemplate.delete(redisKey);// 使用后删除key
// 颁布 token 令牌
String token = JwtUtils.gerneratorToken(passengerPhone, IndentinyConstant.PASSENGER_IDENTITY);
TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setToken(null);
tokenResponse.setToken(token);
// 返回处理结果
return ResponseResult.success(tokenResponse);

@ -0,0 +1,10 @@
package com.mashibing.internalcommon.constant;
public class IndentinyConstant {
// 乘客身份-标识
public static final String PASSENGER_IDENTITY = "1";
// 司机身份-标识
public static final String DRIVER_IDENTITY = "2";
}

@ -0,0 +1,12 @@
package com.mashibing.internalcommon.dto;
import lombok.Data;
@Data
public class TokenResult {
private String phone;
private String indentiny;
}

@ -5,6 +5,7 @@ import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.mashibing.internalcommon.dto.TokenResult;
import java.util.Calendar;
import java.util.Date;
@ -15,10 +16,15 @@ public class JwtUtils {
private static final String SIGN = "CPFmsb%!@SSAE";
private static final String TOKEN_KEY = "passengerPhone";
private static final String JWT_KEY_PHONE = "phone";
// 提供 公共获取 token字符串
public static String gerneratorToken(String passengerPhone){
private static final String JWT_KEY_INDENTINY = "indentiny";
/**
* token
*/
public static String gerneratorToken(String passengerPhone,String indentiny){
// 准备 token过期时间 Date 类型
Calendar calendar = Calendar.getInstance();
@ -29,7 +35,9 @@ public class JwtUtils {
JWTCreator.Builder builder = JWT.create();
// 将 有效数据部份 合成到 builder中
builder.withClaim(TOKEN_KEY,passengerPhone);
builder.withClaim(JWT_KEY_PHONE,passengerPhone);
builder.withClaim(JWT_KEY_INDENTINY,indentiny);
// 设置 超时时间
builder.withExpiresAt(date);
@ -40,16 +48,22 @@ public class JwtUtils {
}
/**
* token
* token
* @param token
* @return
*/
public static String parseToken(String token){
public static TokenResult parseToken(String token){
// 通过 JWT 的 required 以某种加密算法校验后 进行比对
DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
// 获取有效数据内容
Claim claim = verify.getClaim(TOKEN_KEY);
return claim.toString();
String phone = verify.getClaim(JWT_KEY_PHONE).toString();
String indentiny = verify.getClaim(JWT_KEY_INDENTINY).toString();
TokenResult result = new TokenResult();
result.setPhone(phone);
result.setIndentiny(indentiny);
return result;
}
@ -57,11 +71,9 @@ public class JwtUtils {
// public static void main (String [ ] args ) {
//
// String tmpPhone = "13912345678";
// String token = gerneratorToken(tmpPhone);
// String token = gerneratorToken(tmpPhone,"1");
// System.out.println("号码 "+tmpPhone+"生成的token = " + token);
// String s = parseToken(token);
// System.out.println("根据生成token解析出的有效数据是 " + s);
//
// System.out.println("根据生成token解析出的有效数据是 " + parseToken(token));
// }
}

Loading…
Cancel
Save