安能派拉登录调整。

This commit is contained in:
dengwc 2024-03-22 08:54:34 +08:00
parent 58069e3c6c
commit 8bc6422edd
9 changed files with 153 additions and 29 deletions

View File

@ -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<UserLoginResult> 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<UserLoginResult> 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<UserLoginResult> ANLoginSSO(@RequestBody UserLoginParam userLoginParam) {
String employeeNo = feishuLoginStrategy.login(userLoginParam.getCode(), AN_FSSC);
String employeeNo = anFeiShuLoginStrategy.login(userLoginParam.getCode());
userLoginParam.setSfno(employeeNo);
UserLoginResult userLoginResult = authenticateService

View File

@ -10,12 +10,12 @@ public class LocalLoginStrategy implements LoginStrategy {
@Override
public Optional<String> getAccessToken(String code, String tag) {
public Optional<String> getAccessToken(String code) {
return Optional.empty();
}
@Override
public String getUserInfo(String accessToken,String tag) {
public String getUserInfo(String accessToken) {
return null;
}
}

View File

@ -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<String> accessToken = getAccessToken(code, tag);
return getUserInfo(accessToken.orElseThrow(() -> new SSOLoginException("Failed to obtain access token")), tag);
default String login(String code ) {
Optional<String> accessToken = getAccessToken(code);
return getUserInfo(accessToken.orElseThrow(() -> new SSOLoginException("Failed to obtain access token")));
}
Optional<String> getAccessToken(String code, String tag);
Optional<String> getAccessToken(String code);
String getUserInfo(String accessToken, String tag);
String getUserInfo(String accessToken);
static Optional<String> getAccessTokenMethod(HttpRequestBase request, String tokenName) {
String responseBody = null;

View File

@ -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<String> 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<String> 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);
}
}
}

View File

@ -43,7 +43,7 @@ public class FeishuLoginStrategy implements LoginStrategy {
@Override
public Optional<String> getAccessToken(String code, String tag) {
public Optional<String> 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

View File

@ -57,7 +57,7 @@ public class PailaLoginStrategy implements LoginStrategy {
private UserFactory userFactory;
@Override
public Optional<String> getAccessToken(String code, String tag) {
public Optional<String> getAccessToken(String code) {
log.info("开始执行登录");
List<NameValuePair> 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<NameValuePair> userInfoParams = Collections.singletonList(

View File

@ -57,7 +57,7 @@ public class PailaMobileLoginStrategy implements LoginStrategy {
private UserFactory userFactory;
@Override
public Optional<String> getAccessToken(String code, String tag) {
public Optional<String> getAccessToken(String code) {
log.info("开始执行登录");
List<NameValuePair> 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<NameValuePair> userInfoParams = Collections.singletonList(

View File

@ -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
@ -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);
}
}

View File

@ -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);
}