From 19fa8a7ddd2fd962afdc0062c6b0bf2a3bff27ca Mon Sep 17 00:00:00 2001 From: msb_89821 Date: Thu, 20 Oct 2022 17:54:07 +0800 Subject: [PATCH] =?UTF-8?q?add:=E5=AE=9E=E7=8E=B0=E5=8F=8Ctoken=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text/consonant/PassengerConsonant.java | 8 +- .../text/controller/PassengerController.java | 19 ++++- .../text/controller/TokenController.java | 81 +++++++++++++++++++ .../online/text/dto/TokenQequstVo.java | 23 ++++++ .../mashibing/online/text/dto/UserInfo.java | 4 +- .../text/intercept/InterceptorConfinger.java | 1 + .../online/text/intercept/JwtIntercept.java | 48 ++++------- .../text/utils/BussizParaTokenUtil.java | 43 ++++++++++ .../java/com/mashibing/dto/TokenResult.java | 3 + .../java/com/mashibing/util/JwtUtils.java | 15 ++-- .../com/mashibing/util/RediesPreKeyUtil.java | 21 +++++ 11 files changed, 223 insertions(+), 43 deletions(-) create mode 100644 oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/TokenController.java create mode 100644 oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/TokenQequstVo.java create mode 100644 oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/utils/BussizParaTokenUtil.java create mode 100644 oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/util/RediesPreKeyUtil.java diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/consonant/PassengerConsonant.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/consonant/PassengerConsonant.java index 41f030e..3c0db2c 100644 --- a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/consonant/PassengerConsonant.java +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/consonant/PassengerConsonant.java @@ -4,10 +4,16 @@ public class PassengerConsonant { public static final String phoneNumberCodePreKey = "smsCode_"; - public static final String phoneNumberTokenPreKey = "tokenPre_"; +// public static final String phoneNumberTokenPreKey = "tokenPre_"; +// +// public static final String phoneNumberReshTokenPreKey = "reshTokenPre_"; public static final String PassengerIdendity = "1"; public static final String DriverIdendity = "2"; + public static final String ACCESS_TOKEN_TYPE = "accessTokenType"; + + public static final String REFLSH_TOKEN_TYPE = "refshTokenType"; + } diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/PassengerController.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/PassengerController.java index 580b909..3db31eb 100644 --- a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/PassengerController.java +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/PassengerController.java @@ -6,6 +6,7 @@ import com.mashibing.dto.PassengerRequestDto; import com.mashibing.dto.ResponseResult; import com.mashibing.exception.BizExecption; import com.mashibing.util.JwtUtils; +import com.mashibing.util.RediesPreKeyUtil; import lombok.extern.slf4j.Slf4j; import mashibing.online.text.consonant.PassengerConsonant; import mashibing.online.text.dto.UserInfo; @@ -88,13 +89,23 @@ public class PassengerController { passengerUserService.reginsterUserbyPhone(passengerRequestDto); log.info("手机号码={} 完成注册!",phone); // 返回token - String token = JwtUtils.genaroteToken(phone, PassengerConsonant.PassengerIdendity); + String accessToken = JwtUtils.genaroteToken(phone, PassengerConsonant.PassengerIdendity,PassengerConsonant.ACCESS_TOKEN_TYPE); + String refshToken = JwtUtils.genaroteToken(phone, PassengerConsonant.PassengerIdendity,PassengerConsonant.REFLSH_TOKEN_TYPE); UserInfo userInfo =new UserInfo(); userInfo.setPhoneNumber(phone); - userInfo.setToken(token); - stringRedisTemplate.opsForValue().set(PassengerConsonant.phoneNumberTokenPreKey + phone+PassengerConsonant.PassengerIdendity, - token,2,TimeUnit.HOURS); + userInfo.setAccessToken(accessToken); + userInfo.setRefshToken(refshToken); + String accessTokenKey= RediesPreKeyUtil.generateKeyPre(PassengerConsonant.ACCESS_TOKEN_TYPE,phone,PassengerConsonant.PassengerIdendity); + String reshTokenKey= RediesPreKeyUtil.generateKeyPre(PassengerConsonant.REFLSH_TOKEN_TYPE,phone,PassengerConsonant.PassengerIdendity); + stringRedisTemplate.opsForValue().set(accessTokenKey, + accessToken,15,TimeUnit.SECONDS); + stringRedisTemplate.opsForValue().set(reshTokenKey, + refshToken,55,TimeUnit.SECONDS); + log.info("reshTokenKey ={},refshToken={}",reshTokenKey,refshToken); return ResponseResult.success(userInfo); } + + + } diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/TokenController.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/TokenController.java new file mode 100644 index 0000000..7b432ea --- /dev/null +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/TokenController.java @@ -0,0 +1,81 @@ +package mashibing.online.text.controller; + +import cn.hutool.core.util.ObjectUtil; +import com.mashibing.dto.ResponseResult; +import com.mashibing.dto.TokenResult; +import com.mashibing.util.JwtUtils; +import com.mashibing.util.RediesPreKeyUtil; +import lombok.extern.slf4j.Slf4j; +import mashibing.online.text.consonant.PassengerConsonant; +import mashibing.online.text.dto.TokenQequstVo; +import mashibing.online.text.dto.UserInfo; +import mashibing.online.text.utils.BussizParaTokenUtil; +import org.apache.commons.lang3.StringUtils; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.concurrent.TimeUnit; + +/** + * Copyright© 2020.10.20 by 博纳德集团有限公司.All rights reserved. + * 1.版权归博纳德集团公司所有; + * 2.未经原作者允许不得转载本代码内容,否则将视为侵权; + * 3.对于不遵守此声明或者其他违法使用本代码内容者,本公司依法保留追究权。. + * Description:
+ * + * @author nod + * ProjectName: onelinetextplublic + *
PackageName: mashibing.online.text.controller + *
ClassName: + *
Date: 2022/10/20 15:36 + */ +@Slf4j +@RestController +public class TokenController { + + @Resource + private StringRedisTemplate stringRedisTemplate; + + @PostMapping("/parsetReshToken") + public ResponseResult parsetReshToken(@RequestBody TokenQequstVo tokenQequstVo){ + if(ObjectUtil.isNull(tokenQequstVo) || ObjectUtil.isEmpty(tokenQequstVo.getReshToken())){ + return ResponseResult.fail(1002,"reshToken 不能为空"); + } + + TokenResult tokenResult = BussizParaTokenUtil.pareToken(tokenQequstVo.getReshToken()); + if(ObjectUtil.isNull(tokenResult)){ + log.error("解析 reshToken 异常"); + return ResponseResult.fail(1003,"解析 reshToken 异常"); + } + String tokenType=tokenResult.getTokenType(); + String phoneNum = tokenResult.getPhoneNum(); + String idendity = tokenResult.getIdendity(); + String reshTokenRedisKey= RediesPreKeyUtil.generateKeyPre(tokenType,phoneNum,idendity); + String redisToken = stringRedisTemplate.opsForValue().get(reshTokenRedisKey); + log.info("reshTokenKey ={},refshToken={}",reshTokenRedisKey,redisToken); + if(StringUtils.isBlank(redisToken) || !tokenQequstVo.getReshToken().equals(redisToken)){ + return ResponseResult.fail(1005,"reshToken 无效"); + } + + log.info("解析 reshToken 成功!"); + String accessToken = JwtUtils.genaroteToken(phoneNum, PassengerConsonant.PassengerIdendity,PassengerConsonant.ACCESS_TOKEN_TYPE); + String refshToken = JwtUtils.genaroteToken(phoneNum, PassengerConsonant.PassengerIdendity,PassengerConsonant.REFLSH_TOKEN_TYPE); + + + UserInfo userInfo =new UserInfo(); + userInfo.setPhoneNumber(phoneNum); + userInfo.setAccessToken(accessToken); + userInfo.setRefshToken(refshToken); + String accessTokenKey= RediesPreKeyUtil.generateKeyPre(PassengerConsonant.ACCESS_TOKEN_TYPE,phoneNum,PassengerConsonant.PassengerIdendity); + stringRedisTemplate.opsForValue().set(accessTokenKey, + accessToken,15, TimeUnit.SECONDS); + + stringRedisTemplate.opsForValue().set(reshTokenRedisKey, + refshToken,55,TimeUnit.SECONDS); + + return ResponseResult.success(userInfo); + } +} diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/TokenQequstVo.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/TokenQequstVo.java new file mode 100644 index 0000000..b3a817d --- /dev/null +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/TokenQequstVo.java @@ -0,0 +1,23 @@ +package mashibing.online.text.dto; + +import lombok.Data; + +/** + * Copyright© 2020.10.20 by 博纳德集团有限公司.All rights reserved. + * 1.版权归博纳德集团公司所有; + * 2.未经原作者允许不得转载本代码内容,否则将视为侵权; + * 3.对于不遵守此声明或者其他违法使用本代码内容者,本公司依法保留追究权。. + * Description:
+ * + * @author nod + * ProjectName: onelinetextplublic + *
PackageName: mashibing.online.text.dto + *
ClassName: + *
Date: 2022/10/20 15:40 + */ +@Data +public class TokenQequstVo { + + private String reshToken; + +} diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/UserInfo.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/UserInfo.java index 803a231..5372bd8 100644 --- a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/UserInfo.java +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/UserInfo.java @@ -7,5 +7,7 @@ public class UserInfo { private String phoneNumber; - private String token; + private String accessToken; + + private String refshToken; } diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/intercept/InterceptorConfinger.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/intercept/InterceptorConfinger.java index 1243d79..8c3f2ce 100644 --- a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/intercept/InterceptorConfinger.java +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/intercept/InterceptorConfinger.java @@ -32,6 +32,7 @@ public class InterceptorConfinger implements WebMvcConfigurer { registry.addInterceptor(jwtIntercept()).addPathPatterns("/**") .excludePathPatterns("/passenger/checkPhoneNumberCode") .excludePathPatterns("/passenger/getPhoneNumberCode") + .excludePathPatterns("/parsetReshToken") .excludePathPatterns("/test/noTokenTest"); } diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/intercept/JwtIntercept.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/intercept/JwtIntercept.java index b12e653..8998a92 100644 --- a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/intercept/JwtIntercept.java +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/intercept/JwtIntercept.java @@ -1,14 +1,11 @@ package mashibing.online.text.intercept; +import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; -import com.mashibing.constant.CommonStatusEnum; -import com.mashibing.dto.PassengerRequestDto; import com.mashibing.dto.ResponseResult; import com.mashibing.dto.TokenResult; -import com.mashibing.util.JwtUtils; -import javafx.print.Printer; import lombok.extern.slf4j.Slf4j; -import mashibing.online.text.consonant.PassengerConsonant; +import mashibing.online.text.utils.BussizParaTokenUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.servlet.HandlerInterceptor; @@ -26,43 +23,30 @@ public class JwtIntercept implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - + log.info(" 进入拦截器 JwtIntercept === preHandle 方法"); String token = request.getHeader("Authorization"); String message=null; - boolean result =true; - TokenResult tokenResult =null; - if(StringUtils.isBlank(token)){ - PrintWriter out = response.getWriter(); - out.println(JSON.toJSONString(ResponseResult.fail(99, "token is empty !"))); - return false; - } - try{ - tokenResult = JwtUtils.parseToken(token); - }catch (Exception e){ - log.error("解析token异常 e ={}",e.getCause()); - message="parse token exception"; - result =false; - } - if(result){ + boolean result =false; + TokenResult tokenResult = BussizParaTokenUtil.pareToken(token); + + if(ObjectUtil.isNotNull(tokenResult)){ String phone=tokenResult.getPhoneNum(); String idendity=tokenResult.getIdendity(); + String tokenType = tokenResult.getTokenType(); //根据手机号 和身份类型 查询 - String s = stringRedisTemplate.opsForValue().get(PassengerConsonant.phoneNumberTokenPreKey + phone + idendity); - if(StringUtils.isBlank(s)){ + String s = stringRedisTemplate.opsForValue().get(tokenType + phone + idendity); + if(StringUtils.isBlank(s) || !token.equals(s)){ //token 无效 - message="token not exist"; + message="token is inval"; result =false; - }else{ - // 比对token - if(!token.equals(s)){ - message="token is inval"; - result =false; - } + }else { + result = true; + } } - } + if(!result){ PrintWriter out = response.getWriter(); - out.println(JSON.toJSONString(ResponseResult.fail(99, message))); + out.println(JSON.toJSONString(ResponseResult.fail(99, "token is inval"))); } diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/utils/BussizParaTokenUtil.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/utils/BussizParaTokenUtil.java new file mode 100644 index 0000000..2d0f781 --- /dev/null +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/utils/BussizParaTokenUtil.java @@ -0,0 +1,43 @@ +package mashibing.online.text.utils; + +import com.alibaba.fastjson.JSON; +import com.mashibing.dto.ResponseResult; +import com.mashibing.dto.TokenResult; +import com.mashibing.util.JwtUtils; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +import java.io.PrintWriter; + +/** + * Copyright© 2020.10.20 by 博纳德集团有限公司.All rights reserved. + * 1.版权归博纳德集团公司所有; + * 2.未经原作者允许不得转载本代码内容,否则将视为侵权; + * 3.对于不遵守此声明或者其他违法使用本代码内容者,本公司依法保留追究权。. + * Description:
+ * + * @author nod + * ProjectName: onelinetextplublic + *
PackageName: mashibing.online.text.utils + *
ClassName: + *
Date: 2022/10/20 11:39 + */ +@Slf4j +public class BussizParaTokenUtil { + + + public static TokenResult pareToken(String token){ + TokenResult tokenResult =null; + if(StringUtils.isBlank(token)){ + log.error("token 为空"); + return tokenResult; + } + try{ + tokenResult = JwtUtils.parseToken(token); + }catch (Exception e){ + log.error("解析token异常 e ={}",e.getCause()); + tokenResult=null; + } + return tokenResult; + } +} diff --git a/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/dto/TokenResult.java b/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/dto/TokenResult.java index 11134d1..f6605e3 100644 --- a/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/dto/TokenResult.java +++ b/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/dto/TokenResult.java @@ -18,5 +18,8 @@ import lombok.Data; @Data public class TokenResult { private String phoneNum; + private String idendity; + + private String tokenType; } diff --git a/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/util/JwtUtils.java b/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/util/JwtUtils.java index aeb5424..4b07d8d 100644 --- a/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/util/JwtUtils.java +++ b/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/util/JwtUtils.java @@ -31,17 +31,20 @@ public class JwtUtils { private static final String JWT_KEY = "phoneNum"; private static final String IDENDITY = "idendity"; + private static final String TOKEN_TYPE = "tokenType"; + + private static final String TOKEN_TIME = "tokenTime"; + //生成token - public static String genaroteToken(String phoneNum,String idendity){ + public static String genaroteToken(String phoneNum,String idendity,String tokenType){ Map map =new HashMap<>(); map.put(JWT_KEY,phoneNum); map.put(IDENDITY,idendity); + map.put(TOKEN_TYPE,tokenType); -// Calendar calendar=Calendar.getInstance(); -// calendar.add(Calendar.DATE,1); -// Date date =calendar.getTime(); - + Calendar calendar=Calendar.getInstance(); + map.put(TOKEN_TIME,String.valueOf(calendar.getTimeInMillis())); JWTCreator.Builder builder = JWT.create(); //遍历map,把map 的逐个元素值放到 builder里面去 @@ -65,9 +68,11 @@ public class JwtUtils { DecodedJWT verify = JWT.require(Algorithm.HMAC256(SINGN)).build().verify(token); Claim phoneClaim = verify.getClaim(JWT_KEY); Claim idendityClaim = verify.getClaim(IDENDITY); + Claim tokenType = verify.getClaim(TOKEN_TYPE); TokenResult tokenResult = new TokenResult(); tokenResult.setIdendity(idendityClaim.asString()); tokenResult.setPhoneNum(phoneClaim.asString()); + tokenResult.setTokenType(tokenType.asString()); return tokenResult; } diff --git a/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/util/RediesPreKeyUtil.java b/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/util/RediesPreKeyUtil.java new file mode 100644 index 0000000..8640ade --- /dev/null +++ b/oneLineTxt/v1/onelinetextplublic/untitled4/src/main/java/com/mashibing/util/RediesPreKeyUtil.java @@ -0,0 +1,21 @@ +package com.mashibing.util; + +/** + * Copyright© 2020.10.20 by 博纳德集团有限公司.All rights reserved. + * 1.版权归博纳德集团公司所有; + * 2.未经原作者允许不得转载本代码内容,否则将视为侵权; + * 3.对于不遵守此声明或者其他违法使用本代码内容者,本公司依法保留追究权。. + * Description:
+ * + * @author nod + * ProjectName: onelinetextplublic + *
PackageName: com.mashibing.util + *
ClassName: + *
Date: 2022/10/20 10:29 + */ +public class RediesPreKeyUtil { + + public static String generateKeyPre(String tokenType,String phone,String idendityType){ + return tokenType+"_"+phone+"_"+idendityType; + } +}