From 8bc6422edd638c19ed603aa69454c245ef92a15e Mon Sep 17 00:00:00 2001 From: dengwc Date: Fri, 22 Mar 2024 08:54:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E8=83=BD=E6=B4=BE=E6=8B=89=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E8=B0=83=E6=95=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/out/LoginController.java | 10 +- .../services/login/LocalLoginStrategy.java | 4 +- .../services/login/LoginStrategy.java | 10 +- .../login/strategy/ANFeiShuLoginStrategy.java | 118 ++++++++++++++++++ .../login/strategy/FeishuLoginStrategy.java | 14 +-- .../login/strategy/PailaLoginStrategy.java | 4 +- .../strategy/PailaMobileLoginStrategy.java | 4 +- src/test/java/com/chint/ExcelTest.java | 16 ++- .../java/com/chint/RouteApplicationTests.java | 2 +- 9 files changed, 153 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/chint/application/services/login/strategy/ANFeiShuLoginStrategy.java diff --git a/src/main/java/com/chint/application/out/LoginController.java b/src/main/java/com/chint/application/out/LoginController.java index 752e653b..d09f78bf 100644 --- a/src/main/java/com/chint/application/out/LoginController.java +++ b/src/main/java/com/chint/application/out/LoginController.java @@ -2,6 +2,7 @@ package com.chint.application.out; import com.chint.application.commands.OrderCreateCommand; +import com.chint.application.services.login.strategy.ANFeiShuLoginStrategy; import com.chint.application.services.login.strategy.FeishuLoginStrategy; import com.chint.application.services.login.strategy.PailaLoginStrategy; import com.chint.application.services.login.strategy.PailaMobileLoginStrategy; @@ -74,6 +75,9 @@ public class LoginController { @Autowired private RouteCacheService routeCacheService; + @Autowired + private ANFeiShuLoginStrategy anFeiShuLoginStrategy; + @ApiOperation("财务共享登录") @Transactional @GetMapping("/login") @@ -127,7 +131,7 @@ public class LoginController { @PostMapping("/login/sso") public Result loginSSO(@RequestBody UserLoginParam userLoginParam) { - String employeeNo = pailaLoginStrategy.login(userLoginParam.getCode(), FSSC); + String employeeNo = pailaLoginStrategy.login(userLoginParam.getCode()); userLoginParam.setSfno(employeeNo); UserLoginResult userLoginResult = authenticateService @@ -150,7 +154,7 @@ public class LoginController { @PostMapping("/login/sso/mobile") public Result loginSSOMobile(@RequestBody UserLoginParam userLoginParam) { - String employeeNo = feishuLoginStrategy.login(userLoginParam.getCode(), FSSC); + String employeeNo = feishuLoginStrategy.login(userLoginParam.getCode()); userLoginParam.setSfno(employeeNo); UserLoginResult userLoginResult = authenticateService @@ -174,7 +178,7 @@ public class LoginController { @Transactional @PostMapping("/AN/login/sso") public Result ANLoginSSO(@RequestBody UserLoginParam userLoginParam) { - String employeeNo = feishuLoginStrategy.login(userLoginParam.getCode(), AN_FSSC); + String employeeNo = anFeiShuLoginStrategy.login(userLoginParam.getCode()); userLoginParam.setSfno(employeeNo); UserLoginResult userLoginResult = authenticateService diff --git a/src/main/java/com/chint/application/services/login/LocalLoginStrategy.java b/src/main/java/com/chint/application/services/login/LocalLoginStrategy.java index fa584191..2ec2ea02 100644 --- a/src/main/java/com/chint/application/services/login/LocalLoginStrategy.java +++ b/src/main/java/com/chint/application/services/login/LocalLoginStrategy.java @@ -10,12 +10,12 @@ public class LocalLoginStrategy implements LoginStrategy { @Override - public Optional getAccessToken(String code, String tag) { + public Optional getAccessToken(String code) { return Optional.empty(); } @Override - public String getUserInfo(String accessToken,String tag) { + public String getUserInfo(String accessToken) { return null; } } diff --git a/src/main/java/com/chint/application/services/login/LoginStrategy.java b/src/main/java/com/chint/application/services/login/LoginStrategy.java index e341f296..d68afca6 100644 --- a/src/main/java/com/chint/application/services/login/LoginStrategy.java +++ b/src/main/java/com/chint/application/services/login/LoginStrategy.java @@ -20,14 +20,14 @@ import java.util.regex.Pattern; public interface LoginStrategy { Logger log = LoggerFactory.getLogger("UserService"); - default String login(String code, String tag) { - Optional accessToken = getAccessToken(code, tag); - return getUserInfo(accessToken.orElseThrow(() -> new SSOLoginException("Failed to obtain access token")), tag); + default String login(String code ) { + Optional accessToken = getAccessToken(code); + return getUserInfo(accessToken.orElseThrow(() -> new SSOLoginException("Failed to obtain access token"))); } - Optional getAccessToken(String code, String tag); + Optional getAccessToken(String code); - String getUserInfo(String accessToken, String tag); + String getUserInfo(String accessToken); static Optional getAccessTokenMethod(HttpRequestBase request, String tokenName) { String responseBody = null; diff --git a/src/main/java/com/chint/application/services/login/strategy/ANFeiShuLoginStrategy.java b/src/main/java/com/chint/application/services/login/strategy/ANFeiShuLoginStrategy.java new file mode 100644 index 00000000..066ec62b --- /dev/null +++ b/src/main/java/com/chint/application/services/login/strategy/ANFeiShuLoginStrategy.java @@ -0,0 +1,118 @@ +package com.chint.application.services.login.strategy; + +import com.alibaba.fastjson.JSON; +import com.chint.application.dtos.FeishuResponse; +import com.chint.application.services.login.LoginStrategy; +import com.chint.domain.exceptions.AuthException; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Optional; + +import static com.chint.infrastructure.constant.AuthMessageConstant.AUTHENTICATION_FAILED; +import static com.chint.infrastructure.constant.BPMConstant.AN_FSSC; + +@Component +public class ANFeiShuLoginStrategy implements LoginStrategy { + + @Value("${feishu.base-url}") + private String feishuBaseUrl; + + @Value("${feishu.ANAppId}") + private String ANAppId; + + @Value("${feishu.ANAppSecret}") + private String ANAppSecret; + + @Override + public Optional getAccessToken(String code) { + log.info("开始执行飞书登录"); + String appAccessTokenPath = "/open-apis/auth/v3/app_access_token/internal"; + String userAccessTokenPath = "/open-apis/authen/v1/access_token"; + + + // 获取 appAccessToken + String appAccessTokenUrl = null; + try { + appAccessTokenUrl = new URIBuilder(feishuBaseUrl).setPath(appAccessTokenPath).build().toString(); + } catch (URISyntaxException e) { + log.error(e.getMessage()); + } + + String entityForAppAccessToken = "{\"app_id\": \"" + ANAppId + "\",\"app_secret\": \"" + ANAppSecret + "\"}"; + HttpPost postForAppAccessToken = new HttpPost(appAccessTokenUrl); + postForAppAccessToken.setEntity(new StringEntity(entityForAppAccessToken, ContentType.APPLICATION_JSON)); + postForAppAccessToken.setHeader("Content-Type", "application/json"); + log.info("开始获取 AppAccessToken"); + + Optional appAccessToken = LoginStrategy.getAccessTokenMethod(postForAppAccessToken, "app_access_token"); + + // 获取 userAccessToken + String userAccessTokenUrl = null; + try { + userAccessTokenUrl = new URIBuilder(feishuBaseUrl).setPath(userAccessTokenPath).build().toString(); + } catch (URISyntaxException e) { + log.error(e.getMessage()); + } + HttpPost postForUserAccessToken = new HttpPost(userAccessTokenUrl); + postForUserAccessToken.setHeader("Content-Type", "application/json"); + postForUserAccessToken.setHeader("Authorization", "Bearer " + appAccessToken.orElseThrow( + () -> new AuthException("Failed to obtain access token") + )); + String entityForUserAccessToken = "{\"grant_type\": \"authorization_code\",\"code\": \"" + code + "\"}"; + postForUserAccessToken.setEntity(new StringEntity(entityForUserAccessToken, ContentType.APPLICATION_JSON)); + log.info("开始获取 UserAccessToken"); + return LoginStrategy.getAccessTokenMethod(postForUserAccessToken, "access_token"); + } + + @Override + public String getUserInfo(String accessToken) { + String userInfoPath = "/open-apis/authen/v1/user_info"; + + // 获取 userInfo + String userInfoUrl = null; + try { + userInfoUrl = new URIBuilder(feishuBaseUrl).setPath(userInfoPath).build().toString(); + } catch (URISyntaxException e) { + log.error(e.getMessage()); + } + HttpGet getForUserInfo = new HttpGet(userInfoUrl); + getForUserInfo.setHeader("Authorization", "Bearer " + accessToken); + log.info("开始获取 UserInfo"); + + String userInfoResBody = null; + HttpClient client = HttpClients.createDefault(); + + try { + HttpResponse userInfoRes = client.execute(getForUserInfo); + userInfoResBody = EntityUtils.toString(userInfoRes.getEntity(), "UTF-8"); + } catch (IOException e) { + log.error(e.getMessage()); + throw new AuthException(AUTHENTICATION_FAILED); + } + + FeishuResponse feishuResponse = JSON.parseObject(userInfoResBody, FeishuResponse.class); + + if (feishuResponse == null) { + throw new AuthException(AUTHENTICATION_FAILED); + } + + if (feishuResponse.getData() != null) { + return feishuResponse.getData().getEmployee_no(); + } else { + throw new AuthException(AUTHENTICATION_FAILED); + } + + } +} diff --git a/src/main/java/com/chint/application/services/login/strategy/FeishuLoginStrategy.java b/src/main/java/com/chint/application/services/login/strategy/FeishuLoginStrategy.java index 96097008..a2952d9b 100644 --- a/src/main/java/com/chint/application/services/login/strategy/FeishuLoginStrategy.java +++ b/src/main/java/com/chint/application/services/login/strategy/FeishuLoginStrategy.java @@ -43,7 +43,7 @@ public class FeishuLoginStrategy implements LoginStrategy { @Override - public Optional getAccessToken(String code, String tag) { + public Optional getAccessToken(String code) { log.info("开始执行飞书登录"); String appAccessTokenPath = "/open-apis/auth/v3/app_access_token/internal"; String userAccessTokenPath = "/open-apis/authen/v1/access_token"; @@ -57,15 +57,7 @@ public class FeishuLoginStrategy implements LoginStrategy { log.error(e.getMessage()); } - String id = appId; - String secret = appSecret; - //是否是安能的 - if (AN_FSSC.equals(tag)) { - id = ANAppId; - secret = ANAppSecret; - } - - String entityForAppAccessToken = "{\"app_id\": \"" + id + "\",\"app_secret\": \"" + secret + "\"}"; + String entityForAppAccessToken = "{\"app_id\": \"" + appId + "\",\"app_secret\": \"" + appSecret + "\"}"; HttpPost postForAppAccessToken = new HttpPost(appAccessTokenUrl); postForAppAccessToken.setEntity(new StringEntity(entityForAppAccessToken, ContentType.APPLICATION_JSON)); postForAppAccessToken.setHeader("Content-Type", "application/json"); @@ -92,7 +84,7 @@ public class FeishuLoginStrategy implements LoginStrategy { } @Override - public String getUserInfo(String accessToken, String tag) { + public String getUserInfo(String accessToken) { String userInfoPath = "/open-apis/authen/v1/user_info"; // 获取 userInfo diff --git a/src/main/java/com/chint/application/services/login/strategy/PailaLoginStrategy.java b/src/main/java/com/chint/application/services/login/strategy/PailaLoginStrategy.java index 7ec3381d..2b228be9 100644 --- a/src/main/java/com/chint/application/services/login/strategy/PailaLoginStrategy.java +++ b/src/main/java/com/chint/application/services/login/strategy/PailaLoginStrategy.java @@ -57,7 +57,7 @@ public class PailaLoginStrategy implements LoginStrategy { private UserFactory userFactory; @Override - public Optional getAccessToken(String code, String tag) { + public Optional getAccessToken(String code) { log.info("开始执行登录"); List parameters = Arrays.asList( @@ -73,7 +73,7 @@ public class PailaLoginStrategy implements LoginStrategy { } @Override - public String getUserInfo(String accessToken, String tag) { + public String getUserInfo(String accessToken) { List userInfoParams = Collections.singletonList( diff --git a/src/main/java/com/chint/application/services/login/strategy/PailaMobileLoginStrategy.java b/src/main/java/com/chint/application/services/login/strategy/PailaMobileLoginStrategy.java index 378a7426..c375938c 100644 --- a/src/main/java/com/chint/application/services/login/strategy/PailaMobileLoginStrategy.java +++ b/src/main/java/com/chint/application/services/login/strategy/PailaMobileLoginStrategy.java @@ -57,7 +57,7 @@ public class PailaMobileLoginStrategy implements LoginStrategy { private UserFactory userFactory; @Override - public Optional getAccessToken(String code, String tag) { + public Optional getAccessToken(String code) { log.info("开始执行登录"); List parameters = Arrays.asList( @@ -73,7 +73,7 @@ public class PailaMobileLoginStrategy implements LoginStrategy { } @Override - public String getUserInfo(String accessToken,String tag) { + public String getUserInfo(String accessToken) { List userInfoParams = Collections.singletonList( diff --git a/src/test/java/com/chint/ExcelTest.java b/src/test/java/com/chint/ExcelTest.java index faf2285f..d61c2481 100644 --- a/src/test/java/com/chint/ExcelTest.java +++ b/src/test/java/com/chint/ExcelTest.java @@ -10,6 +10,8 @@ import com.authine.cloudpivot.opensdk.config.CloudpivotEnvConfig; import com.authine.cloudpivot.opensdk.model.request.workflow.StartWorkflowRequest; import com.authine.cloudpivot.opensdk.model.response.workflow.StartWorkflowResponse; import com.chint.application.commands.RefundOrderGenerateCommand; +import com.chint.application.services.login.strategy.ANFeiShuLoginStrategy; +import com.chint.application.services.login.strategy.FeishuLoginStrategy; import com.chint.domain.aggregates.order.Leg; import com.chint.domain.aggregates.order.OrderDetail; import com.chint.domain.aggregates.standards.StaffRank; @@ -56,6 +58,7 @@ import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static com.chint.infrastructure.constant.BPMConstant.AN_FSSC; import static com.chint.infrastructure.constant.BPMConstant.YSBPM_TOKEN_URL; @Slf4j @@ -175,7 +178,7 @@ public class ExcelTest { processedLogCount = 0; } -//@Test + //@Test public void saveLog() { int count = 0; // 初始化计数器 while (count <= 10000) { //当计数器小于10000时执行循环 @@ -338,7 +341,7 @@ public class ExcelTest { @Autowired private PostRequest postRequest; -// @Test + // @Test public void outOfStandard() { String ANBPMUrl = "https://fssc.chintanneng.com/fssc"; String AN_Exceed_Standard = "/extapi/businessTravel/exceeding"; @@ -368,7 +371,7 @@ public class ExcelTest { @Autowired private PostRequest httpPostRequest; -// @Test + // @Test public void aabbcc() { @@ -450,5 +453,12 @@ public class ExcelTest { return XNClient; } + @Autowired + private ANFeiShuLoginStrategy anFeiShuLoginStrategy; +// @Test + void ssoLogin() { + String login = anFeiShuLoginStrategy.login("OC-5909-zRqrWjZGNThNXJiAV1kA7dPXTojGzVxK3nE"); + System.out.println(login); + } } diff --git a/src/test/java/com/chint/RouteApplicationTests.java b/src/test/java/com/chint/RouteApplicationTests.java index 5b982c96..22cc9850 100644 --- a/src/test/java/com/chint/RouteApplicationTests.java +++ b/src/test/java/com/chint/RouteApplicationTests.java @@ -314,7 +314,7 @@ class RouteApplicationTests { // @Test void ssoLogin() { - String login = pailaLoginStrategy.login("OC-5909-zRqrWjZGNThNXJiAV1kA7dPXTojGzVxK3nE",FSSC); + String login = pailaLoginStrategy.login("OC-5909-zRqrWjZGNThNXJiAV1kA7dPXTojGzVxK3nE"); System.out.println(login); }