From a9f11041786c4508157a060220e3c184ee9da087 Mon Sep 17 00:00:00 2001 From: guyuliang Date: Thu, 22 Aug 2024 13:54:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9B=E7=BB=99=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84token=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../booter/controller/TokenController.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/TokenController.java b/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/TokenController.java index 6bee59b..2c0ce15 100644 --- a/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/TokenController.java +++ b/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/TokenController.java @@ -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 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)) {