提供给外部使用的token验证

develop
guyuliang 2024-08-22 13:54:07 +08:00
parent a592b9443d
commit a9f1104178
1 changed files with 31 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package com.bocloud.sms.booter.controller;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.Claim;
import com.bocloud.sms.interfaces.TenantService;
import com.bocloud.sms.interfaces.UserService;
@ -15,6 +16,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
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;
import java.util.Map;
@ -70,6 +72,35 @@ public class TokenController {
}
}
//提供给外部验证token的接口
@PostMapping("/v1/token/check")
@Operation(summary = "单点登录验证token")
public GeneralResult<String> checkTokenOut(@RequestBody JSONObject jsonObject) {
String token = jsonObject.getString("token");
if (!StringUtils.hasText(token)) {
return new GeneralResult<>(false, "token校验失败");
}
String tokenKey = "";
if (StringUtils.hasText(token)) {
Claim claim = Tokens.parse(token);
if (claim == null) {
return new GeneralResult<>(false, "token校验失败");
}
String catalog = claim.asMap().get("catalog").toString();
long uuid = Long.parseLong(claim.asMap().get(Common.UUID).toString());
tokenKey = Common.TOKEN + "_" + catalog + "_" + uuid;
}
//校验token
boolean checkResult = check(token, tokenKey);
if (checkResult) {
String newTokenCache = (String) this.redisTemplate.opsForHash().get(tokenKey + "_n", Common.TOKEN);
return new GeneralResult<>(true, newTokenCache, "校验token成功");
} else {
return new GeneralResult<>(false, "token校验失败");
}
}
private boolean check(String token, String tokenKey) {
String newTokenCache = (String) this.redisTemplate.opsForHash().get(tokenKey + "_n", Common.TOKEN);
if (token.equals(newTokenCache)) {