From 79c9511300c493f6706e1654e6141b1b035164c9 Mon Sep 17 00:00:00 2001 From: liuyuanqiang <837052308@qq.com> Date: Thu, 27 Oct 2022 21:52:06 +0800 Subject: [PATCH] =?UTF-8?q?jwt=20token=E7=94=9F=E6=88=90=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/VerificationCodeService.java | 6 +- .../common/constant/IdentityConstant.java | 6 ++ .../com/mashibing/common/dto/TokenResult.java | 9 +++ .../com/mashibing/common/util/JwtUtils.java | 58 ++++++++++++++++++ .../mashibing/common/dto/TokenResult.class | Bin 0 -> 1907 bytes .../com/mashibing/common/util/JwtUtils.class | Bin 0 -> 4247 bytes pom.xml | 5 ++ 7 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 internal-common/src/main/java/com/mashibing/common/constant/IdentityConstant.java create mode 100644 internal-common/src/main/java/com/mashibing/common/dto/TokenResult.java create mode 100644 internal-common/src/main/java/com/mashibing/common/util/JwtUtils.java create mode 100644 internal-common/target/classes/com/mashibing/common/dto/TokenResult.class create mode 100644 internal-common/target/classes/com/mashibing/common/util/JwtUtils.class 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 229706f..a611125 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 @@ -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); } diff --git a/internal-common/src/main/java/com/mashibing/common/constant/IdentityConstant.java b/internal-common/src/main/java/com/mashibing/common/constant/IdentityConstant.java new file mode 100644 index 0000000..65e091f --- /dev/null +++ b/internal-common/src/main/java/com/mashibing/common/constant/IdentityConstant.java @@ -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"; +} diff --git a/internal-common/src/main/java/com/mashibing/common/dto/TokenResult.java b/internal-common/src/main/java/com/mashibing/common/dto/TokenResult.java new file mode 100644 index 0000000..eee7e4c --- /dev/null +++ b/internal-common/src/main/java/com/mashibing/common/dto/TokenResult.java @@ -0,0 +1,9 @@ +package com.mashibing.common.dto; + +import lombok.Data; + +@Data +public class TokenResult { + private String phone; + private String identity; +} diff --git a/internal-common/src/main/java/com/mashibing/common/util/JwtUtils.java b/internal-common/src/main/java/com/mashibing/common/util/JwtUtils.java new file mode 100644 index 0000000..4c7c41d --- /dev/null +++ b/internal-common/src/main/java/com/mashibing/common/util/JwtUtils.java @@ -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 map = new HashMap(); + 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()); + } +} + + diff --git a/internal-common/target/classes/com/mashibing/common/dto/TokenResult.class b/internal-common/target/classes/com/mashibing/common/dto/TokenResult.class new file mode 100644 index 0000000000000000000000000000000000000000..c378acc84fe00bd94f496b64d574deba60c0347e GIT binary patch literal 1907 zcma)7U2hvj6g^|lde`W$86z6p{e6039QRq{JZt1w4LJCy_chtOL;I4st3f|YTDWDy+Jl7V;ZauNS zu_|rL-LLHWUB}(82^db(c74bHRv@wJxQ@Rr5YHDL3&`7Elc&yYIj;S%*V(hXkF33R z5XWm+?Z;NvQR~Pk{g!hmaCxiYbt)a}u;uJAL4~W1=T@4&S9#<;wcQ>2u-Eog-hJEO z4tdSy3zL|o>9IK^XB-aw=kjOW72!{Bp$ACsLJx6Nay5p%pAci z=VTxHz{99&bTO)loThvd&yhXCR8Gq!22S#rx#Ae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