双token完成验证。

master
liuyuanqiang 2 years ago
parent 2e07341226
commit 13b1585eaf

@ -0,0 +1,23 @@
package com.mashibing.apipassenger.controller;
import com.mashibing.apipassenger.service.TokenService;
import com.mashibing.common.dto.ResponseResult;
import com.mashibing.common.response.TokenResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TokenController {
@Autowired
private TokenService tokenService;
@PostMapping("/token-refresh")
public ResponseResult refreshToken(@RequestBody TokenResponse tokenResponse){
String refreshToken = tokenResponse.getRefreshToken();
System.out.println("接收的refreshToken:" + refreshToken);
return tokenService.refreshToken(refreshToken);
}
}

@ -18,8 +18,9 @@ public class InterceptorConfig implements WebMvcConfigurer {
// registry.addInterceptor(new JwtInterceptor()) 这个会导致拦截器初始化时没有初始化redisTemplate,所以要提前new
registry.addInterceptor(jwtInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/token-refresh")
.excludePathPatterns("/noAuthTest")
.excludePathPatterns("/verification-code")
.excludePathPatterns("/verification-code-check");
.excludePathPatterns("/verification-code-check");//这里注意post还是get
}
}

@ -30,15 +30,10 @@ public class JwtInterceptor implements HandlerInterceptor {
String token = request.getHeader("Authorization");
// 解析token
TokenResult tokenResult = null;
try {
tokenResult = JwtUtils.parseToken(token);
}catch (Exception e){
tokenResult = null;
}
TokenResult tokenResult = JwtUtils.checkToken(token);
if (tokenResult == null){
resutltString = "access token invalid";
resutltString = "access token invalid interceptor.";
result = false;
}else{
// 拼接key

@ -0,0 +1,54 @@
package com.mashibing.apipassenger.service;
import com.mashibing.common.constant.CommonStatusEnum;
import com.mashibing.common.constant.TokenConstants;
import com.mashibing.common.dto.ResponseResult;
import com.mashibing.common.dto.TokenResult;
import com.mashibing.common.response.TokenResponse;
import com.mashibing.common.util.JwtUtils;
import com.mashibing.common.util.RedisPrefixUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class TokenService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public ResponseResult refreshToken(String refreshToken){
//解析refreshToken
TokenResult tokenResult = JwtUtils.checkToken(refreshToken);
if(tokenResult == null){
return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR.getCode(),CommonStatusEnum.TOKEN_ERROR.getMsg());
}
//读取Redis Token
String phone = tokenResult.getPhone();
String identity = tokenResult.getIdentity();
String tokenKey = RedisPrefixUtils.generateTokenKey(phone,identity, TokenConstants.REFRESH_TOKEN_TYPE);
String redisAccessToken = stringRedisTemplate.opsForValue().get(tokenKey);
//验证refreshToken
if(StringUtils.isBlank(redisAccessToken) || !redisAccessToken.trim().equals(refreshToken.trim())){
String message = "refreshtoken is invalid!";
return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR.getCode(), CommonStatusEnum.TOKEN_ERROR.getMsg());
}
//重新生成双token
String newAccessToken = JwtUtils.generateToken(phone, identity, TokenConstants.ACCESS_TOKEN_TYPE);
String newRefreshToken = JwtUtils.generateToken(phone, identity, TokenConstants.ACCESS_TOKEN_TYPE);
String accessTokenKey = RedisPrefixUtils.generateTokenKey(phone,identity,TokenConstants.ACCESS_TOKEN_TYPE);
stringRedisTemplate.opsForValue().set(accessTokenKey,newAccessToken,10, TimeUnit.SECONDS);
stringRedisTemplate.opsForValue().set(tokenKey,newRefreshToken,300,TimeUnit.SECONDS);
TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setRefreshToken(newRefreshToken);
tokenResponse.setAccessToken(newAccessToken);
return ResponseResult.success(tokenResponse);
}
}

@ -87,8 +87,8 @@ public class VerificationCodeService {
//token存入Redis
String accessTokenRedisKey = RedisPrefixUtils.generateTokenKey(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
String refreshTokenRedisKey = RedisPrefixUtils.generateTokenKey(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);
stringRedisTemplate.opsForValue().set(accessTokenRedisKey, accessToken, 30,TimeUnit.DAYS);
stringRedisTemplate.opsForValue().set(refreshTokenRedisKey, refreshToken, 31,TimeUnit.DAYS);
stringRedisTemplate.opsForValue().set(accessTokenRedisKey, accessToken, 10,TimeUnit.SECONDS);
stringRedisTemplate.opsForValue().set(refreshTokenRedisKey, refreshToken, 301,TimeUnit.SECONDS);
TokenResponse tokenResponse = new TokenResponse();
tokenResponse.setAccessToken(accessToken);

@ -9,6 +9,12 @@ public enum CommonStatusEnum {
*
*/
VERIFICATION_CODE_FAIL(1001,"验证码错误!"),
/**
* token1100 ~ 1199
*/
TOKEN_ERROR(1100,"token错误"),
/**
*
*/

@ -19,6 +19,7 @@ public class JwtUtils {
//乘客是1司机是2
private static final String JWT_KET_IDENTITY = "identity";
private static final String JWT_KET_TOKEN_TYPE = "tokenType";
private static final String JWT_KET_TOKEN_TIME = "tokenTime";
public static String generateToken(String phone, String identity, String tokenType){
Map<String,String> map = new HashMap<String, String>();
@ -29,6 +30,7 @@ public class JwtUtils {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE,1);
Date date = calendar.getTime();
map.put(JwtUtils.JWT_KET_TOKEN_TIME,date.toString());
JWTCreator.Builder builder = JWT.create();
map.forEach((k,v) -> {
@ -52,6 +54,21 @@ public class JwtUtils {
tokenResult.setPhone(phone);
return tokenResult;
}
/**
* token
* @param token
* @return TokenResult
*/
public static TokenResult checkToken(String token){
TokenResult tokenResult = null;
try {
tokenResult = JwtUtils.parseToken(token);
}catch (Exception e){
tokenResult = null;
}
return tokenResult;
}
public static void main(String[] args) {
String token = JwtUtils.generateToken("13751145166","1", TokenConstants.ACCESS_TOKEN_TYPE);
System.out.println("token:" + token);

@ -6,4 +6,5 @@ D:\Works\workspaces\online-taxi-public\internal-common\src\main\java\com\mashibi
D:\Works\workspaces\online-taxi-public\internal-common\src\main\java\com\mashibing\common\request\VerificationCodeDTO.java
D:\Works\workspaces\online-taxi-public\internal-common\src\main\java\com\mashibing\common\response\TokenResponse.java
D:\Works\workspaces\online-taxi-public\internal-common\src\main\java\com\mashibing\common\util\JwtUtils.java
D:\Works\workspaces\online-taxi-public\internal-common\src\main\java\com\mashibing\common\util\RedisPrefixUtils.java
D:\Works\workspaces\online-taxi-public\internal-common\src\main\java\com\mashibing\common\dto\TokenResult.java

@ -0,0 +1,6 @@
D:\Works\workspaces\online-taxi-public\service-passenger-user\src\main\java\com\mashibing\serviceDriverUser\controller\UserController.java
D:\Works\workspaces\online-taxi-public\service-passenger-user\src\main\java\com\mashibing\serviceDriverUser\ServicePassengerUserApplication.java
D:\Works\workspaces\online-taxi-public\service-passenger-user\src\main\java\com\mashibing\serviceDriverUser\controller\TestController.java
D:\Works\workspaces\online-taxi-public\service-passenger-user\src\main\java\com\mashibing\serviceDriverUser\mapper\PassengerUserMapper.java
D:\Works\workspaces\online-taxi-public\service-passenger-user\src\main\java\com\mashibing\serviceDriverUser\dto\PassengerUser.java
D:\Works\workspaces\online-taxi-public\service-passenger-user\src\main\java\com\mashibing\serviceDriverUser\service\UserService.java

@ -0,0 +1,3 @@
D:\Works\workspaces\online-taxi-public\service-verificationcode\src\main\java\com\mashibing\service\controller\TestController.java
D:\Works\workspaces\online-taxi-public\service-verificationcode\src\main\java\com\mashibing\service\ServiceVerificationcodeApplication.java
D:\Works\workspaces\online-taxi-public\service-verificationcode\src\main\java\com\mashibing\service\controller\NumberCodeController.java
Loading…
Cancel
Save