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)) {