From 3da3515b71e43b2715cc177e60ffb79fbedfdc5e Mon Sep 17 00:00:00 2001 From: smallbun <2689170096@qq.com> Date: Mon, 28 Aug 2023 22:12:11 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E5=AE=9D=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alipay/client/AlipayClient.java | 73 ++--------------- .../AlipaySystemUserInfoShareResponse.java | 79 ------------------- .../AlipayLoginAuthenticationFilter.java | 9 +-- 3 files changed, 6 insertions(+), 155 deletions(-) delete mode 100644 eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/client/AlipaySystemUserInfoShareResponse.java diff --git a/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/client/AlipayClient.java b/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/client/AlipayClient.java index c78c602e..46aaaa33 100644 --- a/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/client/AlipayClient.java +++ b/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/client/AlipayClient.java @@ -56,14 +56,18 @@ public class AlipayClient { } retryTimes = retryTimes + 1; try { + //@formatter:off java.util.Map systemParams = TeaConverter.buildMap( new TeaPair("method", "alipay.system.oauth.token"), new TeaPair("app_id", kernel.getConfig("appId")), - new TeaPair("timestamp", kernel.getTimestamp()), new TeaPair("format", "json"), + new TeaPair("timestamp", kernel.getTimestamp()), + new TeaPair("format", "json"), + new TeaPair("version", "1.0"), new TeaPair("charset", "UTF-8"), new TeaPair("sign_type", kernel.getConfig("signType")), new TeaPair("app_cert_sn", kernel.getMerchantCertSN()), new TeaPair("alipay_root_cert_sn", kernel.getAlipayRootCertSN())); + //@formatter:no java.util.Map bizParams = new java.util.HashMap<>(); java.util.Map textParams = TeaConverter.buildMap( new TeaPair("grant_type", "authorization_code"), new TeaPair("code", code)); @@ -100,73 +104,6 @@ public class AlipayClient { throw new TeaUnretryableException(request); } - /** - * 获取用户信息 - * - * @param authToken {@link String} - * @return {@link AlipaySystemOauthTokenResponse} - * @throws Exception Exception - */ - public AlipaySystemUserInfoShareResponse getUserInfo(String authToken) throws Exception { - java.util.Map runtime = getRuntime(); - - TeaRequest request = null; - long now = System.currentTimeMillis(); - int retryTimes = 0; - while (Tea.allowRetry((java.util.Map) runtime.get("retry"), retryTimes, - now)) { - if (retryTimes > 0) { - int backoffTime = Tea.getBackoffTime(runtime.get("backoff"), retryTimes); - if (backoffTime > 0) { - Tea.sleep(backoffTime); - } - } - retryTimes = retryTimes + 1; - try { - java.util.Map systemParams = TeaConverter.buildMap( - new TeaPair("method", "alipay.user.info.share"), - new TeaPair("app_id", kernel.getConfig("appId")), - new TeaPair("timestamp", kernel.getTimestamp()), new TeaPair("format", "json"), - new TeaPair("charset", "UTF-8"), - new TeaPair("sign_type", kernel.getConfig("signType")), - new TeaPair("app_cert_sn", kernel.getMerchantCertSN()), - new TeaPair("alipay_root_cert_sn", kernel.getAlipayRootCertSN())); - java.util.Map bizParams = new java.util.HashMap<>(); - java.util.Map textParams = TeaConverter - .buildMap(new TeaPair("auth_token", authToken)); - request = getRequest(systemParams, bizParams, textParams); - TeaResponse response = Tea.doAction(request, runtime); - - java.util.Map respMap = kernel.readAsJson(response, - "alipay.user.info.share"); - if (kernel.isCertMode()) { - if (kernel.verify(respMap, - kernel.extractAlipayPublicKey(kernel.getAlipayCertSN(respMap)))) { - return TeaModel.toModel(kernel.toRespModel(respMap), - new AlipaySystemUserInfoShareResponse()); - } - - } else { - if (kernel.verify(respMap, kernel.getConfig("alipayPublicKey"))) { - return TeaModel.toModel(kernel.toRespModel(respMap), - new AlipaySystemUserInfoShareResponse()); - } - - } - - throw new TeaException( - TeaConverter.buildMap(new TeaPair("message", "验签失败,请检查支付宝公钥设置是否正确。"))); - } catch (Exception e) { - if (Tea.isRetryable(e)) { - continue; - } - throw new RuntimeException(e); - } - } - - throw new TeaUnretryableException(request); - } - private TeaRequest getRequest(Map systemParams, Map bizParams, Map textParams) throws Exception { TeaRequest request = new TeaRequest(); diff --git a/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/client/AlipaySystemUserInfoShareResponse.java b/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/client/AlipaySystemUserInfoShareResponse.java deleted file mode 100644 index afd83932..00000000 --- a/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/client/AlipaySystemUserInfoShareResponse.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * eiam-authentication-alipay - Employee Identity and Access Management - * Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package cn.topiam.employee.authentication.alipay.client; - -import com.aliyun.tea.NameInMap; -import com.aliyun.tea.TeaModel; -import com.aliyun.tea.Validation; - -import lombok.Getter; -import lombok.Setter; - -/** - * - * @author TopIAM - * Created by support@topiam.cn on 2023/8/25 22:26 - */ -@Getter -@Setter -public class AlipaySystemUserInfoShareResponse extends TeaModel { - @NameInMap("http_body") - @Validation(required = true) - public String httpBody; - - @NameInMap("code") - @Validation(required = true) - public String code; - - @NameInMap("msg") - @Validation(required = true) - public String msg; - - @NameInMap("sub_code") - @Validation(required = true) - public String subCode; - - @NameInMap("sub_msg") - @Validation(required = true) - public String subMsg; - - @NameInMap("user_id") - @Validation(required = true) - public String userId; - - @NameInMap("avatar") - @Validation(required = true) - public String avatar; - - @NameInMap("city") - @Validation(required = true) - public Long city; - - @NameInMap("nick_name") - @Validation(required = true) - public String nickName; - - @NameInMap("province") - @Validation(required = true) - public Long province; - - @NameInMap("gender") - @Validation(required = true) - public Long gender; - -} diff --git a/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/filter/AlipayLoginAuthenticationFilter.java b/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/filter/AlipayLoginAuthenticationFilter.java index 1210e0e3..3d950d3c 100644 --- a/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/filter/AlipayLoginAuthenticationFilter.java +++ b/eiam-authentication/eiam-authentication-alipay/src/main/java/cn/topiam/employee/authentication/alipay/filter/AlipayLoginAuthenticationFilter.java @@ -41,7 +41,6 @@ import com.alipay.easysdk.kernel.Context; import cn.topiam.employee.authentication.alipay.AlipayIdpOAuth2Config; import cn.topiam.employee.authentication.alipay.client.AlipayClient; import cn.topiam.employee.authentication.alipay.client.AlipaySystemOauthTokenResponse; -import cn.topiam.employee.authentication.alipay.client.AlipaySystemUserInfoShareResponse; import cn.topiam.employee.authentication.common.authentication.IdpUserDetails; import cn.topiam.employee.authentication.common.filter.AbstractIdpAuthenticationProcessingFilter; import cn.topiam.employee.authentication.common.service.UserIdpService; @@ -136,16 +135,10 @@ public class AlipayLoginAuthenticationFilter extends AbstractIdpAuthenticationPr logger.error("支付宝认证获取 access_token 失败: [" + token.getHttpBody() + "]"); throw new TopIamException(token.getSubMsg()); } - String accessToken = token.getAccessToken(); - AlipaySystemUserInfoShareResponse userInfo = client.getUserInfo(accessToken); - if (!StringUtils.isBlank(userInfo.getCode())) { - logger.error("支付宝认证获取用户信息失败: [" + userInfo.getHttpBody() + "]"); - throw new TopIamException(userInfo.getSubMsg()); - } //执行逻辑 IdpUserDetails idpUserDetails = IdpUserDetails.builder().openId(token.getOpenId()) .providerType(ALIPAY_OAUTH).providerCode(providerCode).providerId(providerId) - .avatarUrl(userInfo.getAvatar()).nickName(userInfo.getNickName()).build(); + .build(); return attemptAuthentication(request, response, idpUserDetails); } catch (Exception e) { throw new RuntimeException(e);