token存储到服务端

main
topsun 2 years ago
parent 34ec151277
commit ac9cf00500

@ -1,6 +1,5 @@
package com.taxi.apipassenger.interceptor; package com.taxi.apipassenger.interceptor;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -12,7 +11,9 @@ public class InterceptorConfig implements WebMvcConfigurer {
// WebMvcConfigurer.super.addInterceptors(registry); // WebMvcConfigurer.super.addInterceptors(registry);
registry.addInterceptor(new JwtInterceptor()) registry.addInterceptor(new JwtInterceptor())
.addPathPatterns("/**")//拦截的路径 .addPathPatterns("/**")//拦截的路径
.excludePathPatterns("/noauthTest");//不拦截的路径 .excludePathPatterns("/noauthTest")
.excludePathPatterns("/verification-code-check")
.excludePathPatterns("/verification-code");//不拦截的路径
} }
} }

@ -14,8 +14,10 @@ import com.taxi.apipassenger.remote.ServiceVerificatoncodeClient;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Indexed;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.lang.invoke.ConstantCallSite;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Service @Service
@ -72,6 +74,12 @@ public class VerificationCodeService {
//四、颁发令牌 //四、颁发令牌
String token = JwtUtils.generatorToken(passenegerPhone, String token = JwtUtils.generatorToken(passenegerPhone,
IdentityConstant.PASSENGER_IDENTITY); IdentityConstant.PASSENGER_IDENTITY);
//将token存入redis
String tokenKey = Utils.getTokenPrefixKey(passenegerPhone,
IdentityConstant.PASSENGER_IDENTITY);
stringRedisTemplate.opsForValue().set(tokenKey,token,30,TimeUnit.DAYS);
CheckCodeResponse checkCodeResponse = new CheckCodeResponse(); CheckCodeResponse checkCodeResponse = new CheckCodeResponse();
checkCodeResponse.setToken(token); checkCodeResponse.setToken(token);
return ResponseResult.success(checkCodeResponse); return ResponseResult.success(checkCodeResponse);

@ -1,7 +1,13 @@
package com.internal.contant; package com.internal.contant;
/**
*
*/
public class IdentityConstant { public class IdentityConstant {
/**
*
*/
public static final String PASSENGER_IDENTITY = "1"; public static final String PASSENGER_IDENTITY = "1";
public static final String DRIVER_IDENTITY = "2"; public static final String DRIVER_IDENTITY = "2";

@ -3,4 +3,5 @@ package com.internal.util;
public class ApiPassengerConstant { public class ApiPassengerConstant {
//乘客验证码的前缀 //乘客验证码的前缀
public final static String verificationCodePrefix = "passenger-verificatioin-code-"; public final static String verificationCodePrefix = "passenger-verificatioin-code-";
public final static String TOKEN_PREFIX = "token-";
} }

@ -28,7 +28,7 @@ public class JwtUtils {
//token过期时间 //token过期时间
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.forEach( map.forEach(
@ -37,7 +37,7 @@ public class JwtUtils {
} }
); );
//整合过期时间 //整合过期时间
builder.withExpiresAt(date); // builder.withExpiresAt(date);//token保持到redis已经设置有效期时间
//生成token //生成token
String sign = builder.sign(Algorithm.HMAC256(SIGN)); String sign = builder.sign(Algorithm.HMAC256(SIGN));
return sign; return sign;

@ -5,4 +5,16 @@ public class Utils {
public static String getVerificationCodePrefixKey(String passenegerPhone) { public static String getVerificationCodePrefixKey(String passenegerPhone) {
return ApiPassengerConstant.verificationCodePrefix + passenegerPhone; return ApiPassengerConstant.verificationCodePrefix + passenegerPhone;
} }
/**
* token
* @param passenegerPhone
* @param identity
* @return
*/
public static String getTokenPrefixKey(String passenegerPhone, String identity) {
return ApiPassengerConstant.TOKEN_PREFIX + passenegerPhone + "-" + identity;
}
} }

Loading…
Cancel
Save