SView Help Center
English (US)

校验AccessToken

Author: sviewngts Last Updated: 2026-08-25 16:26
接口名 GET: /auth/token/check_token
入参:(Param) 是否必传 默认值 描述 备注
token 是 - accessToken accessToken值
回参示例:(application/json)

 

{
    "code": 0,
    "msg": null,
    "data": {
        "tokenValue": "950031f2-eca5-40b7-8d86-a01ae1f859e3",
        "issuedAt": "2026-05-18T08:49:48.142887956Z", // 该token的签发时间
        "expiresAt": "2026-05-18T20:49:48.142887956Z",//过期时间,超过这个时间后 Token 就会被判定为无效,需要重新获取
        "tokenType": {
            "value": "Bearer"
        },
        "scopes": [
            "server"
        ],
        "tenantStatus": "0"
    },
    "ok": true
}
package com.hoteam.sview.demo;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CheckTokenDemo {

    private static final String CHECK_TOKEN_URL = "http://your-host/auth/token/check_token";

    public static JSONObject checkAccessToken(String accessToken) {
        if (StrUtil.isBlank(accessToken)) {
            log.warn("accessToken 为空,跳过校验");
            return null;
        }

        HttpRequest request = HttpRequest.get(CHECK_TOKEN_URL)
                .form("token", accessToken)
                .timeout(10_000);

        // 追加公共 Header
        CommonHeaderUtil.withCommonHeaders(request);

        try (HttpResponse response = request.execute()) {
            int status = response.getStatus();
            String body = response.body();
            log.info("校验 AccessToken 响应:status={}, body={}", status, body);

            if (status == HttpStatus.HTTP_UNAUTHORIZED) {
                log.warn("AccessToken 校验失败(401),token 可能失效,body={}", body);
                return null;
            }
            if (!response.isOk() || StrUtil.isBlank(body)) {
                log.warn("AccessToken 校验失败:status={}, body={}", status, body);
                return null;
            }

            JSONObject json = JSONUtil.parseObj(body);
            Boolean ok   = json.getBool("ok");
            Integer code = json.getInt("code");
            if (!Boolean.TRUE.equals(ok) || (code != null && code != 0)) {
                log.warn("AccessToken 校验业务失败:code={}, msg={}", code, json.getStr("msg"));
                return null;
            }
            return json.getJSONObject("data");
        } catch (Exception e) {
            log.error("调用校验 AccessToken 接口异常", e);
            return null;
        }
    }
}