提交 77d54a8b 编写于 作者: 天天's avatar 天天

refreshToken还原代码

上级 eb8c24da
......@@ -12,16 +12,11 @@ import cn.ibizlab.core.uaa.service.ISysRoleService;
import cn.ibizlab.core.uaa.service.ISysUserRoleService;
import cn.ibizlab.util.domain.Token;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.security.AuthTokenUtil;
import cn.ibizlab.util.security.AuthenticationUser;
import cn.ibizlab.util.service.AuthenticationUserService;
import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
......@@ -64,19 +59,10 @@ public class UAACoreService {
@Lazy
private ISysRoleService sysRoleService;
@Autowired
private AuthTokenUtil jwtTokenUtil;
@Autowired
private AuthenticationUserService userDetailsService;
@Autowired
@Lazy
private UserDingtalkRegisterService userDingtalkRegisterService;
@Value("${ibiz.jwt.expiration:7200000}")
private Long expiration;
@Autowired
@Lazy
private DingTalkTokenService dingTalkTokenService;
......@@ -342,40 +328,6 @@ public class UAACoreService {
return sign;
}
public String refreshToken(String oldToken){
String username = null;
String newToken = null;
try {
username = jwtTokenUtil.getUsernameFromToken(oldToken);
} catch (ExpiredJwtException e) {
log.error(e.getMessage());
}
if (!StringUtils.isEmpty(username)) {
AuthenticationUser user = userDetailsService.loadUserByUsername(username);
if (jwtTokenUtil.validateToken(oldToken, user)) {
// 将新token存入缓存,在固定周期内调用接口将返回同一token
Token tok = getToken(oldToken);
if (ObjectUtils.isEmpty(tok)) {
newToken = jwtTokenUtil.generateToken(user);
setToken(oldToken, newToken);
} else {
// 判断缓存中的token是否到期,到期将返回新token
if (isExpired(tok, expiration)) {
newToken = jwtTokenUtil.generateToken(user);
setToken(oldToken, newToken);
}else{
newToken = tok.getNewToken();
}
}
}
}
if (StringUtils.isEmpty(newToken)) {
throw new BadRequestAlertException("获取token失败", "", "refreshToken");
} else {
return newToken;
}
}
@CachePut(value = "ibzuaa_refreshtoken", key = "'token:'+#p0")
public Token setToken(String oldToken, String newToken) {
Token tok = new Token(newToken, oldToken, new Date());
......
......@@ -40,6 +40,9 @@ public class ClientAuthenticationResource
@Value("${ibiz.auth.cookie.domain:}")
private String cookiedomain;
@Value("${ibiz.jwt.expiration:7200000}")
private Long expiration;
@Autowired
private AuthTokenUtil jwtTokenUtil;
......@@ -78,9 +81,38 @@ public class ClientAuthenticationResource
* @return 新token
*/
@PostMapping(value = "uaa/refreshToken")
public ResponseEntity<String> refreshToken(@Validated @RequestBody @NotNull(message = "token不能为空") String oldToken) {
return ResponseEntity.ok().body(uaaCoreService.refreshToken(oldToken));
public String refreshToken(@Validated @RequestBody @NotNull(message = "token不能为空") String oldToken) {
String username = null;
String newToken = null;
try {
username = jwtTokenUtil.getUsernameFromToken(oldToken);
} catch (ExpiredJwtException e) {
log.error(e.getMessage());
}
if (!StringUtils.isEmpty(username)) {
AuthenticationUser user = userDetailsService.loadUserByUsername(username);
if (jwtTokenUtil.validateToken(oldToken, user)) {
// 将新token存入缓存,在固定周期内调用接口将返回同一token
Token tok = uaaCoreService.getToken(oldToken);
if (ObjectUtils.isEmpty(tok)) {
newToken = jwtTokenUtil.generateToken(user);
uaaCoreService.setToken(oldToken, newToken);
} else {
// 判断缓存中的token是否到期,到期将返回新token
if (uaaCoreService.isExpired(tok, expiration)) {
newToken = jwtTokenUtil.generateToken(user);
uaaCoreService.setToken(oldToken, newToken);
}else{
newToken = tok.getNewToken();
}
}
}
}
if (StringUtils.isEmpty(newToken)) {
throw new BadRequestAlertException("获取token失败", "", "refreshToken");
} else {
return newToken;
}
}
@PostMapping(value = "v7/changepwd")
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册