From 5949b9c88c5899246930b55471a240bdcd6f21e1 Mon Sep 17 00:00:00 2001 From: b2894lxlx <517289602@qq.com> Date: Tue, 14 Jul 2026 11:54:56 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=96=B0=E5=A2=9EIAM=E5=AF=B9?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/dto/IamSsoProfileResponse.java | 65 +++++ .../auth/dto/IamSsoTokenResponse.java | 51 ++++ .../auth/granter/IamSsoTokenGranter.java | 244 ++++++++++++++++++ .../auth/props/IamSsoProperties.java | 75 ++++++ 4 files changed, 435 insertions(+) create mode 100644 blade-auth/src/main/java/org/springblade/auth/dto/IamSsoProfileResponse.java create mode 100644 blade-auth/src/main/java/org/springblade/auth/dto/IamSsoTokenResponse.java create mode 100644 blade-auth/src/main/java/org/springblade/auth/granter/IamSsoTokenGranter.java create mode 100644 blade-auth/src/main/java/org/springblade/auth/props/IamSsoProperties.java diff --git a/blade-auth/src/main/java/org/springblade/auth/dto/IamSsoProfileResponse.java b/blade-auth/src/main/java/org/springblade/auth/dto/IamSsoProfileResponse.java new file mode 100644 index 0000000..d51f7f8 --- /dev/null +++ b/blade-auth/src/main/java/org/springblade/auth/dto/IamSsoProfileResponse.java @@ -0,0 +1,65 @@ +/** + * BladeX Commercial License Agreement + * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved. + *
+ * Use of this software is governed by the Commercial License Agreement + * obtained after purchasing a license from BladeX. + *
+ * 1. This software is for development use only under a valid license + * from BladeX. + *
+ * 2. Redistribution of this software's source code to any third party + * without a commercial license is strictly prohibited. + *
+ * 3. Licensees may copyright their own code but cannot use segments + * from this software for such purposes. Copyright of this software + * remains with BladeX. + *
+ * Using this software signifies agreement to this License, and the software + * must not be used for illegal purposes. + *
+ * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is + * not liable for any claims arising from secondary or illegal development. + *
+ * Author: Chill Zhuang (bladejava@qq.com)
+ */
+package org.springblade.auth.dto;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+
+import java.util.Map;
+
+/**
+ * IAM统一身份认证用户信息响应
+ *
+ * @author Chill
+ */
+@Data
+public class IamSsoProfileResponse {
+
+ /**
+ * IAM用户主账号
+ */
+ private String id;
+
+ /**
+ * IAM用户扩展属性
+ */
+ private Map
+ * Use of this software is governed by the Commercial License Agreement
+ * obtained after purchasing a license from BladeX.
+ *
+ * 1. This software is for development use only under a valid license
+ * from BladeX.
+ *
+ * 2. Redistribution of this software's source code to any third party
+ * without a commercial license is strictly prohibited.
+ *
+ * 3. Licensees may copyright their own code but cannot use segments
+ * from this software for such purposes. Copyright of this software
+ * remains with BladeX.
+ *
+ * Using this software signifies agreement to this License, and the software
+ * must not be used for illegal purposes.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
+ * not liable for any claims arising from secondary or illegal development.
+ *
+ * Author: Chill Zhuang (bladejava@qq.com)
+ */
+package org.springblade.auth.dto;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+
+/**
+ * IAM统一身份认证Token响应
+ *
+ * @author Chill
+ */
+@Data
+public class IamSsoTokenResponse {
+
+ /**
+ * IAM访问令牌
+ */
+ @JsonProperty("access_token")
+ private String accessToken;
+
+ /**
+ * IAM刷新令牌
+ */
+ @JsonProperty("refresh_token")
+ private String refreshToken;
+
+}
diff --git a/blade-auth/src/main/java/org/springblade/auth/granter/IamSsoTokenGranter.java b/blade-auth/src/main/java/org/springblade/auth/granter/IamSsoTokenGranter.java
new file mode 100644
index 0000000..52a24aa
--- /dev/null
+++ b/blade-auth/src/main/java/org/springblade/auth/granter/IamSsoTokenGranter.java
@@ -0,0 +1,244 @@
+/**
+ * BladeX Commercial License Agreement
+ * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
+ *
+ * Use of this software is governed by the Commercial License Agreement
+ * obtained after purchasing a license from BladeX.
+ *
+ * 1. This software is for development use only under a valid license
+ * from BladeX.
+ *
+ * 2. Redistribution of this software's source code to any third party
+ * without a commercial license is strictly prohibited.
+ *
+ * 3. Licensees may copyright their own code but cannot use segments
+ * from this software for such purposes. Copyright of this software
+ * remains with BladeX.
+ *
+ * Using this software signifies agreement to this License, and the software
+ * must not be used for illegal purposes.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
+ * not liable for any claims arising from secondary or illegal development.
+ *
+ * Author: Chill Zhuang (bladejava@qq.com)
+ */
+package org.springblade.auth.granter;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.extern.slf4j.Slf4j;
+import org.springblade.auth.dto.IamSsoProfileResponse;
+import org.springblade.auth.dto.IamSsoTokenResponse;
+import org.springblade.auth.props.IamSsoProperties;
+import org.springblade.auth.utils.TokenUtil;
+import org.springblade.core.oauth2.constant.OAuth2TokenConstant;
+import org.springblade.core.oauth2.exception.UserInvalidException;
+import org.springblade.core.oauth2.granter.AuthorizationCodeGranter;
+import org.springblade.core.oauth2.handler.PasswordHandler;
+import org.springblade.core.oauth2.provider.OAuth2Request;
+import org.springblade.core.oauth2.service.OAuth2ClientService;
+import org.springblade.core.oauth2.service.OAuth2User;
+import org.springblade.core.oauth2.service.OAuth2UserService;
+import org.springblade.core.redis.cache.BladeRedis;
+import org.springblade.core.tool.api.R;
+import org.springblade.core.tool.utils.StringUtil;
+import org.springblade.system.feign.IUserClient;
+import org.springblade.system.pojo.entity.UserInfo;
+import org.springframework.core.Ordered;
+import org.springframework.core.annotation.Order;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URLEncoder;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.util.Base64;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * IAM统一身份认证授权器
+ *
+ * @author Chill
+ */
+@Slf4j
+@Component
+@Order(Ordered.HIGHEST_PRECEDENCE)
+public class IamSsoTokenGranter extends AuthorizationCodeGranter {
+
+ private static final String IAM_GRANT_TYPE = "authorization_code";
+ private static final String BEARER_PREFIX = "Bearer ";
+
+ private final IUserClient userClient;
+ private final IamSsoProperties properties;
+ private final ObjectMapper objectMapper;
+ private final HttpClient httpClient;
+
+ public IamSsoTokenGranter(OAuth2ClientService clientService,
+ OAuth2UserService userService,
+ PasswordHandler passwordHandler,
+ BladeRedis bladeRedis,
+ IUserClient userClient,
+ IamSsoProperties properties,
+ ObjectMapper objectMapper) {
+ super(clientService, userService, passwordHandler, bladeRedis);
+ this.userClient = userClient;
+ this.properties = properties;
+ this.objectMapper = objectMapper;
+ this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
+ }
+
+ @Override
+ public OAuth2User user(OAuth2Request request) {
+ if (!isIamRequest(request)) {
+ return super.user(request);
+ }
+ validateRequest(request);
+ // 先校验本系统客户端,IAM的redirect_uri由后续远端接口校验。
+ client(request);
+
+ IamSsoTokenResponse tokenResponse = requestIamToken(request);
+ String accessToken = tokenResponse.getAccessToken();
+ if (StringUtil.isBlank(accessToken)) {
+ throw new UserInvalidException(OAuth2TokenConstant.TOKEN_NOT_CORRECT);
+ }
+
+ IamSsoProfileResponse profileResponse = requestIamProfile(accessToken);
+ String accountNo = profileResponse.getAccountNo();
+ if (StringUtil.isBlank(accountNo)) {
+ log.warn("IAM统一身份认证用户信息缺少account_no,iamId={}", profileResponse.getId());
+ throw new UserInvalidException(OAuth2TokenConstant.USER_NOT_FOUND);
+ }
+
+ R
+ * Use of this software is governed by the Commercial License Agreement
+ * obtained after purchasing a license from BladeX.
+ *
+ * 1. This software is for development use only under a valid license
+ * from BladeX.
+ *
+ * 2. Redistribution of this software's source code to any third party
+ * without a commercial license is strictly prohibited.
+ *
+ * 3. Licensees may copyright their own code but cannot use segments
+ * from this software for such purposes. Copyright of this software
+ * remains with BladeX.
+ *
+ * Using this software signifies agreement to this License, and the software
+ * must not be used for illegal purposes.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
+ * not liable for any claims arising from secondary or illegal development.
+ *
+ * Author: Chill Zhuang (bladejava@qq.com)
+ */
+package org.springblade.auth.props;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * IAM统一身份认证配置
+ *
+ * @author Chill
+ */
+@Data
+@ConfigurationProperties(prefix = "iam.sso")
+public class IamSsoProperties {
+
+ /**
+ * IAM换取Token接口地址
+ */
+ private String tokenUrl;
+
+ /**
+ * IAM获取用户信息接口地址
+ */
+ private String profileUrl;
+
+ /**
+ * IAM客户端ID
+ */
+ private String clientId;
+
+ /**
+ * IAM客户端密钥
+ */
+ private String clientSecret;
+
+ /**
+ * IAM回调地址
+ */
+ private String redirectUri;
+
+ /**
+ * IAM认证头。为空时默认使用 Basic Base64(clientId:clientSecret)
+ */
+ private String authorization;
+
+ /**
+ * IAM用户信息认证头。为空时默认使用 Bearer accessToken
+ */
+ private String profileAuthorization;
+
+}