工业互联网平台2.0版本后端代码
houzhongjian
2025-03-07 c0b68c59704f48d39aabc02d7009464c6fc36784
iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java
@@ -1,8 +1,11 @@
package com.iailab.sdk.auth.client;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iailab.sdk.auth.client.dto.TokenDTO;
import com.iailab.sdk.auth.config.AuthProperties;
import com.iailab.sdk.auth.constants.ErrorCodeConstants;
import com.iailab.sdk.auth.constants.SdkErrorCodeConstants;
import com.iailab.sdk.util.http.IailabHttpUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
@@ -38,6 +41,8 @@
    private static final String SCOPE = "user.read user.write";
    private static final String CHARSET = "utf-8";
    // 鉴权token
    public static String accessToken;
    // 刷新token
@@ -72,18 +77,18 @@
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        Map<String, Object> authMap = exchange.getBody();
        Object code = authMap.get("code");
        if(ErrorCodeConstants.AUTH_BAD_CREDENTIALS.getCode().equals(code)) {
            throw exception(ErrorCodeConstants.AUTH_BAD_CREDENTIALS);
        } else if(ErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS.getCode().equals(code)) {
            throw exception(ErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS);
        } else if(ErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH.getCode().equals(code)) {
            throw exception(ErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH);
        } else if(ErrorCodeConstants.OAUTH2_CLIENT_CLIENT_SECRET_ERROR.getCode().equals(code)) {
            throw exception(ErrorCodeConstants.OAUTH2_CLIENT_CLIENT_SECRET_ERROR);
        } else if(ErrorCodeConstants.OAUTH2_CLIENT_NOT_EXISTS.getCode().equals(code)) {
            throw exception(ErrorCodeConstants.OAUTH2_CLIENT_NOT_EXISTS);
        } else if(ErrorCodeConstants.OAUTH2_CLIENT_DISABLE.getCode().equals(code)) {
            throw exception(ErrorCodeConstants.OAUTH2_CLIENT_DISABLE);
        if(SdkErrorCodeConstants.AUTH_BAD_CREDENTIALS.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.AUTH_BAD_CREDENTIALS);
        } else if(SdkErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS);
        } else if(SdkErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH);
        } else if(SdkErrorCodeConstants.OAUTH2_CLIENT_CLIENT_SECRET_ERROR.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_CLIENT_SECRET_ERROR);
        } else if(SdkErrorCodeConstants.OAUTH2_CLIENT_NOT_EXISTS.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_NOT_EXISTS);
        } else if(SdkErrorCodeConstants.OAUTH2_CLIENT_DISABLE.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_DISABLE);
        }
        accessToken = authMap.get("access_token").toString();
        refreshToken = authMap.get("refresh_token").toString();
@@ -113,7 +118,7 @@
            if (code == 401) {
                authenticate();
            } else {
                throw exception(ErrorCodeConstants.AUTH_REFRESH_TOKEN_ERROR);
                throw exception(SdkErrorCodeConstants.AUTH_REFRESH_TOKEN_ERROR);
            }
        } else {
            accessToken = authMap.get("access_token").toString();
@@ -122,6 +127,26 @@
        return handleResponse(exchange);
    }
    /**
     * 平台http请求封装
     * @param method
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String doHttp(String method, String url, Map<String, Object> params) throws Exception {
        String response = null;
        String upperMethod = method.toUpperCase();
        if("GET".equals(upperMethod)) {
            response = IailabHttpUtils.doGet(url, params, CHARSET);
        } else if("POST".equals(upperMethod)) {
            ObjectMapper objectMapper = new ObjectMapper();
            response = IailabHttpUtils.doPost(url, objectMapper.writeValueAsString(params), CHARSET);
        }
        return response;
    }
    private static void addClientHeader(HttpHeaders headers) {
        // client 拼接,需要 BASE64 编码
        String client = authProperties.getClientId() + ":" + authProperties.getClientSecret();