token存储到服务端

main
topsun 2 years ago
parent 34ec151277
commit ac9cf00500

@ -1,6 +1,5 @@
package com.taxi.apipassenger.interceptor;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -12,7 +11,9 @@ public class InterceptorConfig implements WebMvcConfigurer {
// WebMvcConfigurer.super.addInterceptors(registry);
registry.addInterceptor(new JwtInterceptor())
.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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Indexed;
import org.springframework.stereotype.Service;
import java.lang.invoke.ConstantCallSite;
import java.util.concurrent.TimeUnit;
@Service
@ -72,6 +74,12 @@ public class VerificationCodeService {
//四、颁发令牌
String token = JwtUtils.generatorToken(passenegerPhone,
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.setToken(token);
return ResponseResult.success(checkCodeResponse);

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

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

@ -28,7 +28,7 @@ public class JwtUtils {
//token过期时间
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
Date date = calendar.getTime();
// Date date = calendar.getTime();
JWTCreator.Builder builder = JWT.create();
map.forEach(
@ -37,7 +37,7 @@ public class JwtUtils {
}
);
//整合过期时间
builder.withExpiresAt(date);
// builder.withExpiresAt(date);//token保持到redis已经设置有效期时间
//生成token
String sign = builder.sign(Algorithm.HMAC256(SIGN));
return sign;

@ -2,7 +2,19 @@ package com.internal.util;
public class Utils {
public static String getVerificationCodePrefixKey(String passenegerPhone){
return ApiPassengerConstant.verificationCodePrefix + passenegerPhone;
public static String getVerificationCodePrefixKey(String 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