parent
5fbe1b6c8f
commit
4896454164
@ -0,0 +1,23 @@
|
||||
package com.taxi.apipassenger.controller;
|
||||
|
||||
import com.internal.dto.ResponseResult;
|
||||
import com.internal.response.TokenResponse;
|
||||
import com.taxi.apipassenger.service.TokenService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
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
|
||||
TokenService tokenService;
|
||||
|
||||
@PostMapping("/token-refresh")
|
||||
public ResponseResult refreshToken(@RequestBody TokenResponse tokenResponse){
|
||||
return tokenService.refreshToken(tokenResponse.getRefreshToken());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.taxi.apipassenger.service;
|
||||
|
||||
import com.internal.contant.CommonStatusEnum;
|
||||
import com.internal.contant.IdentityConstant;
|
||||
import com.internal.contant.TokenConstant;
|
||||
import com.internal.dto.ResponseResult;
|
||||
import com.internal.dto.TokenResult;
|
||||
import com.internal.response.TokenResponse;
|
||||
import com.internal.util.JwtUtils;
|
||||
import com.internal.util.RedisPrefixUtils;
|
||||
import com.taxi.apipassenger.util.TokenUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TokenService {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
public ResponseResult refreshToken(String refreshToken) {
|
||||
TokenResult tokenResult = JwtUtils.checkToken(refreshToken);
|
||||
TokenResponse tokenResponse;
|
||||
if (tokenResult == null) {
|
||||
return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR);
|
||||
}
|
||||
String passenegerPhone = tokenResult.getPassengerPhone();
|
||||
String refreshTokenKey = RedisPrefixUtils.getTokenPrefixKey(passenegerPhone,
|
||||
IdentityConstant.PASSENGER_IDENTITY, TokenConstant.REFRESH_TOKEN_TYPE);
|
||||
//从redis 缓存中获取refreshtoken
|
||||
String redisRefreshToken = stringRedisTemplate.opsForValue().get(refreshTokenKey);
|
||||
//判断refreshToken是否有效
|
||||
if (redisRefreshToken.equals(refreshToken)) {//生成双token
|
||||
tokenResponse = TokenUtil.proDoubleTokenAndSaveRedis(stringRedisTemplate,
|
||||
passenegerPhone);
|
||||
} else {
|
||||
return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR);
|
||||
}
|
||||
return ResponseResult.success(tokenResponse);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.taxi.apipassenger.util;
|
||||
|
||||
import com.internal.contant.IdentityConstant;
|
||||
import com.internal.contant.TokenConstant;
|
||||
import com.internal.response.TokenResponse;
|
||||
import com.internal.util.JwtUtils;
|
||||
import com.internal.util.RedisPrefixUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TokenUtil {
|
||||
|
||||
/**
|
||||
* 生成双token并且保存到redis
|
||||
* 动作一致
|
||||
*/
|
||||
public static TokenResponse proDoubleTokenAndSaveRedis(StringRedisTemplate stringRedisTemplate,
|
||||
String passenegerPhone){
|
||||
//四、颁发令牌
|
||||
String accessToken = JwtUtils.generatorToken(passenegerPhone,
|
||||
IdentityConstant.PASSENGER_IDENTITY, TokenConstant.ACCESS_TOKEN_TYPE);
|
||||
String refreshToken = JwtUtils.generatorToken(passenegerPhone,
|
||||
IdentityConstant.PASSENGER_IDENTITY,TokenConstant.REFRESH_TOKEN_TYPE);
|
||||
|
||||
//将accesstoken存入redis
|
||||
String accessTokenKey = RedisPrefixUtils.getTokenPrefixKey(passenegerPhone,
|
||||
IdentityConstant.PASSENGER_IDENTITY,TokenConstant.ACCESS_TOKEN_TYPE);
|
||||
stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30, TimeUnit.DAYS);
|
||||
//将refreshToken存入redis
|
||||
String refreshTokenKey = RedisPrefixUtils.getTokenPrefixKey(passenegerPhone,
|
||||
IdentityConstant.PASSENGER_IDENTITY,TokenConstant.REFRESH_TOKEN_TYPE);
|
||||
stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);
|
||||
|
||||
TokenResponse checkCodeResponse = new TokenResponse();
|
||||
checkCodeResponse.setAccessToken(accessToken);
|
||||
checkCodeResponse.setRefreshToken(refreshToken);
|
||||
return checkCodeResponse;
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in new issue