generated from msb_55675/Demo
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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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…
Reference in new issue