From fb3e5ab1945ac3c826833bb824912c9062391755 Mon Sep 17 00:00:00 2001 From: msb_89821 Date: Tue, 18 Oct 2022 23:04:35 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E5=AE=9E=E7=8E=B0=E5=8A=9F=E8=83=BD1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text/consonant/PassengerConsonant.java | 2 ++ .../text/controller/PassengerController.java | 3 ++ .../online/text/intercept/JwtIntercept.java | 30 ++++++++++++++++++- .../java/com/mashibing/util/JwtUtils.java | 15 +++++----- 4 files changed, 42 insertions(+), 8 deletions(-) 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 8a35810..41f030e 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,6 +4,8 @@ public class PassengerConsonant { public static final String phoneNumberCodePreKey = "smsCode_"; + public static final String phoneNumberTokenPreKey = "tokenPre_"; + public static final String PassengerIdendity = "1"; public static final String DriverIdendity = "2"; 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 76d09c4..dc6cd60 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 @@ -80,6 +80,9 @@ public class PassengerController { UserInfo userInfo =new UserInfo(); userInfo.setPhoneNumber(phone); userInfo.setToken(token); + stringRedisTemplate.opsForValue().set(PassengerConsonant.phoneNumberTokenPreKey + phone+PassengerConsonant.PassengerIdendity, + token,30,TimeUnit.MINUTES); + return ResponseResult.success(userInfo); } } 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 cdf7448..bf457ff 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,5 +1,11 @@ package mashibing.online.text.intercept; +import com.mashibing.dto.PassengerRequestDto; +import com.mashibing.dto.TokenResult; +import com.mashibing.util.JwtUtils; +import lombok.extern.slf4j.Slf4j; +import mashibing.online.text.consonant.PassengerConsonant; +import org.apache.commons.lang3.StringUtils; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.servlet.HandlerInterceptor; @@ -7,6 +13,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +@Slf4j public class JwtIntercept implements HandlerInterceptor { @Resource @@ -15,7 +22,28 @@ public class JwtIntercept implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String token = request.getHeader("Authorization"); + boolean result =true; + TokenResult tokenResult =null; + try{ + tokenResult = JwtUtils.parseToken(token); + }catch (Exception e){ + log.error("解析token异常 e",e.getCause()); + } + if(!result){ - return HandlerInterceptor.super.preHandle(request, response, handler); + }else { + String phone=tokenResult.getPhoneNum(); + String idendity=tokenResult.getIdendity(); + //根据手机号 和身份类型 查询 + String s = stringRedisTemplate.opsForValue().get(PassengerConsonant.phoneNumberTokenPreKey + phone + idendity); + if(StringUtils.isNotBlank(s)){ + //token 有效 + } + + + } + + + return result; } } 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 6870f64..aeb5424 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 @@ -38,10 +38,11 @@ public class JwtUtils { map.put(JWT_KEY,phoneNum); map.put(IDENDITY,idendity); - Calendar calendar=Calendar.getInstance(); - calendar.add(Calendar.DATE,1); - Date date =calendar.getTime(); - JWTCreator.Builder builder = JWT.create(); +// Calendar calendar=Calendar.getInstance(); +// calendar.add(Calendar.DATE,1); +// Date date =calendar.getTime(); + + JWTCreator.Builder builder = JWT.create(); //遍历map,把map 的逐个元素值放到 builder里面去 map.forEach( @@ -50,7 +51,7 @@ public class JwtUtils { } ); //把过期时间放到builder - builder.withExpiresAt(date); + // builder.withExpiresAt(date); //最后生成token return builder.sign(Algorithm.HMAC256(SINGN)); @@ -65,8 +66,8 @@ public class JwtUtils { Claim phoneClaim = verify.getClaim(JWT_KEY); Claim idendityClaim = verify.getClaim(IDENDITY); TokenResult tokenResult = new TokenResult(); - tokenResult.setIdendity(idendityClaim.toString()); - tokenResult.setPhoneNum(phoneClaim.toString()); + tokenResult.setIdendity(idendityClaim.asString()); + tokenResult.setPhoneNum(phoneClaim.asString()); return tokenResult; }