jwt token生成完成。

master
liuyuanqiang 3 years ago
parent 7c7dd6707a
commit 79c9511300

@ -3,10 +3,12 @@ package com.mashibing.apipassenger.service;
import com.mashibing.apipassenger.remote.ServicePassengerUserClient;
import com.mashibing.apipassenger.remote.ServiceVefificationcodeClient;
import com.mashibing.common.constant.CommonStatusEnum;
import com.mashibing.common.constant.IdentityConstant;
import com.mashibing.common.dto.ResponseResult;
import com.mashibing.common.request.VerificationCodeDTO;
import com.mashibing.common.response.NumberCodeResponse;
import com.mashibing.common.response.TokenResponse;
import com.mashibing.common.util.JwtUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -78,9 +80,11 @@ public class VerificationCodeService {
servicePassengerUserClient.loginOrRegister(verificationCodeDTO);
System.out.println("颁发token");
String token = JwtUtils.generateToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY);
TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setToken("my token");
tokenResponse.setToken(token);
return ResponseResult.success(tokenResponse);
}

@ -0,0 +1,6 @@
package com.mashibing.common.constant;
public class IdentityConstant {
public static final String PASSENGER_IDENTITY = "1";
public static final String DRIVER_IDENTITY = "2";
}

@ -0,0 +1,9 @@
package com.mashibing.common.dto;
import lombok.Data;
@Data
public class TokenResult {
private String phone;
private String identity;
}

@ -0,0 +1,58 @@
package com.mashibing.common.util;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.mashibing.common.dto.TokenResult;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class JwtUtils {
//加盐
private static final String SIGN = "CPFmsb!@#$$";
private static final String JWT_KEY_PHONE = "phone";
//乘客是1司机是2
private static final String JWT_KET_IDENTITY = "identity";
public static String generateToken(String phone, String identity){
Map<String,String> map = new HashMap<String, String>();
map.put(JwtUtils.JWT_KEY_PHONE,phone);
map.put(JwtUtils.JWT_KET_IDENTITY,identity);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE,1);
Date date = calendar.getTime();
JWTCreator.Builder builder = JWT.create();
map.forEach((k,v) -> {
builder.withClaim(k,v);
});
builder.withExpiresAt(date);
String token = builder.sign(Algorithm.HMAC256(SIGN));
return token;
}
public static TokenResult parseToken(String token){
DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
String phone = decodedJWT.getClaim(JwtUtils.JWT_KEY_PHONE).toString();
String identity = decodedJWT.getClaim(JwtUtils.JWT_KET_IDENTITY).toString();
TokenResult tokenResult = new TokenResult();
tokenResult.setIdentity(identity);
tokenResult.setPhone(phone);
return tokenResult;
}
public static void main(String[] args) {
String token = JwtUtils.generateToken("13751145166","1");
System.out.println("token:" + token);
TokenResult result = JwtUtils.parseToken(token);
System.out.println("phone parsed:" + result.toString());
}
}

@ -42,6 +42,11 @@
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.14.0</version>
</dependency>
<!-- nacos 依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>

Loading…
Cancel
Save