add: 实现功能1

master
msb_89821 2 years ago
parent c31aa02aff
commit fb3e5ab194

@ -4,6 +4,8 @@ public class PassengerConsonant {
public static final String phoneNumberCodePreKey = "smsCode_"; public static final String phoneNumberCodePreKey = "smsCode_";
public static final String phoneNumberTokenPreKey = "tokenPre_";
public static final String PassengerIdendity = "1"; public static final String PassengerIdendity = "1";
public static final String DriverIdendity = "2"; public static final String DriverIdendity = "2";

@ -80,6 +80,9 @@ public class PassengerController {
UserInfo userInfo =new UserInfo(); UserInfo userInfo =new UserInfo();
userInfo.setPhoneNumber(phone); userInfo.setPhoneNumber(phone);
userInfo.setToken(token); userInfo.setToken(token);
stringRedisTemplate.opsForValue().set(PassengerConsonant.phoneNumberTokenPreKey + phone+PassengerConsonant.PassengerIdendity,
token,30,TimeUnit.MINUTES);
return ResponseResult.success(userInfo); return ResponseResult.success(userInfo);
} }
} }

@ -1,5 +1,11 @@
package mashibing.online.text.intercept; 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.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
@ -7,6 +13,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@Slf4j
public class JwtIntercept implements HandlerInterceptor { public class JwtIntercept implements HandlerInterceptor {
@Resource @Resource
@ -15,7 +22,28 @@ public class JwtIntercept implements HandlerInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("Authorization"); 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;
} }
} }

@ -38,10 +38,11 @@ public class JwtUtils {
map.put(JWT_KEY,phoneNum); map.put(JWT_KEY,phoneNum);
map.put(IDENDITY,idendity); map.put(IDENDITY,idendity);
Calendar calendar=Calendar.getInstance(); // Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE,1); // calendar.add(Calendar.DATE,1);
Date date =calendar.getTime(); // Date date =calendar.getTime();
JWTCreator.Builder builder = JWT.create();
JWTCreator.Builder builder = JWT.create();
//遍历map,把map 的逐个元素值放到 builder里面去 //遍历map,把map 的逐个元素值放到 builder里面去
map.forEach( map.forEach(
@ -50,7 +51,7 @@ public class JwtUtils {
} }
); );
//把过期时间放到builder //把过期时间放到builder
builder.withExpiresAt(date); // builder.withExpiresAt(date);
//最后生成token //最后生成token
return builder.sign(Algorithm.HMAC256(SINGN)); return builder.sign(Algorithm.HMAC256(SINGN));
@ -65,8 +66,8 @@ public class JwtUtils {
Claim phoneClaim = verify.getClaim(JWT_KEY); Claim phoneClaim = verify.getClaim(JWT_KEY);
Claim idendityClaim = verify.getClaim(IDENDITY); Claim idendityClaim = verify.getClaim(IDENDITY);
TokenResult tokenResult = new TokenResult(); TokenResult tokenResult = new TokenResult();
tokenResult.setIdendity(idendityClaim.toString()); tokenResult.setIdendity(idendityClaim.asString());
tokenResult.setPhoneNum(phoneClaim.toString()); tokenResult.setPhoneNum(phoneClaim.asString());
return tokenResult; return tokenResult;
} }

Loading…
Cancel
Save