Compare commits

...

4 Commits

Author SHA1 Message Date
owan 0b924c9cd7 Merge remote-tracking branch 'origin/main' 2024-11-11 18:44:03 +08:00
owan 9c919f6896 获取code参数代码修改 2024-11-11 18:05:11 +08:00
owan 08b693c16d 获取code参数代码修改 2024-11-11 14:44:10 +08:00
owan d94c8b442b 获取code参数代码修改 2024-11-11 13:28:07 +08:00
2 changed files with 41 additions and 42 deletions

View File

@ -2,6 +2,7 @@ package shkd.sys.sys.eoss;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kd.bos.config.client.util.StringUtils;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import org.springframework.http.HttpEntity;
@ -9,13 +10,12 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.HashMap;
/**
* @program: laxv-cosmic
* @author: Lang
* @create: 2024-11-08 10:50
* @description: 认证服务
**/
public class AuthService {
@ -26,28 +26,28 @@ public class AuthService {
* @param code
* @return
*/
public static String accessToken(String code,String eoss_ip,String client_id,String client_secret) {
HttpHeaders api_headers = new HttpHeaders();//请求头
api_headers.add("Content-Type", "application/json; charset=utf-8");
HashMap<String, Object> requestBody = new HashMap<>();
requestBody.put("client_id", client_id);
requestBody.put("client_secret", client_secret);
requestBody.put("grant_type", "authorization_code");
requestBody.put("code", code);
requestBody.put("authType", 0);
String api_jsonBody = JSON.toJSONString(requestBody);//请求体
HttpEntity<String> api_requestEntity = new HttpEntity<>(api_jsonBody, api_headers);
HttpEntity<String> api_requestEntity = new HttpEntity<>(api_headers);
String url = String.format("%s/sso2/authCenter/accessToken", eoss_ip);
// 构建 URL添加查询参数
String urlWithParams = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("client_id", client_id)
.queryParam("client_secret", client_secret)
.queryParam("grant_type", "authorization_code")
.queryParam("authType", "0")
.queryParam("code", code)
.toUriString(); // 生成带参数的完整 URL
logger.info(String.format("accessToken→请求地址%s,请求参数:%s",url,JSON.toJSONString(api_requestEntity)));
ResponseEntity<String> response = restTemplate.postForEntity(url, api_requestEntity, String.class);
ResponseEntity<String> response = restTemplate.postForEntity(urlWithParams, api_requestEntity, String.class);
JSONObject jsonObject = JSON.parseObject(response.getBody());
logger.info(String.format("accessToken→返回结果:%s",jsonObject));
if ("0".equals(jsonObject.get("code").toString())){
String data = jsonObject.getString("data");
return data;
String access_token = jsonObject.getString("access_token");
if (!StringUtils.isEmpty(access_token)){
return access_token;
}
return null;
}
@ -61,21 +61,25 @@ public class AuthService {
logger.info("getUserInfo→请求token%s", token);
HttpHeaders api_headers = new HttpHeaders();//请求头
api_headers.add("Content-Type", "application/json; charset=utf-8");
HashMap<String, Object> requestBody = new HashMap<>();
requestBody.put("access_token", token);
requestBody.put("client_id", client_id);
requestBody.put("client_secret", client_secret);
requestBody.put("authType", 0);
HttpEntity<String> api_requestEntity = new HttpEntity<>(null, api_headers);
String url = String.format("%s/sso2/authCenter/myAccount", eoss_ip);
// 构建 URL添加查询参数
String urlWithParams = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("access_token", token)
.queryParam("client_id", client_id)
.queryParam("client_secret", client_secret)
.queryParam("authType", "0")
.toUriString(); // 生成带参数的完整 URL
HttpEntity<String> api_requestEntity = new HttpEntity<>(api_headers);
logger.info(String.format("getUserInfo→请求地址%s,请求参数:%s",url,JSON.toJSONString(api_requestEntity)));
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, api_requestEntity, String.class, requestBody);
ResponseEntity<String> response = restTemplate.exchange(urlWithParams, HttpMethod.GET, api_requestEntity, String.class);
JSONObject jsonObject = JSON.parseObject(response.getBody());
logger.info(String.format("getUserInfo→返回结果:%s",jsonObject));
if ("0".equals(jsonObject.get("code").toString())){
String data = jsonObject.getString("data");
return data;
JSONObject obj = jsonObject.getJSONObject("data");
if (obj!=null){
return obj.getString("loginName");
}
return null;
}

View File

@ -6,6 +6,7 @@ import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.login.thirdauth.ThirdSSOAuthHandler;
import kd.bos.login.thirdauth.UserAuthResult;
import kd.bos.login.thirdauth.UserProperType;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
@ -15,9 +16,7 @@ import java.io.IOException;
import static kd.bos.login.thirdauth.UserProperType.UserName;
/**
* @program: laxv-cosmic
* @author: Lang
* @create: 2024-09-27 15:48
* @description: 单点登录-川投统一认证
**/
public class SSOPluginLogin implements ThirdSSOAuthHandler {
@ -33,7 +32,6 @@ public class SSOPluginLogin implements ThirdSSOAuthHandler {
*/
@Override
public void callTrdSSOLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) {
s = "http://10.1.7.85:8022/ierp/auth/callbackTrdSSO.do?tenantNo=ctsk-dev&sandboxNo=2063179586270661632";
//认证中心的登录地址
logger.info(String.format("eoss_ip:%s,client_id:%s,client_secret:%s,重定向地址:%s",EOSS_IP,CLIENT_ID,CLIENT_SECRET,s));
try {
@ -42,7 +40,7 @@ public class SSOPluginLogin implements ThirdSSOAuthHandler {
* 获取授权码
*/
String ssourl=String.format("%s/sso2/authCenter/authorize?client_id=%s&response_type=code&authType=0&redirect_uri=%s",
EOSS_IP,CLIENT_ID,EOSS_IP);
EOSS_IP,CLIENT_ID,s);
logger.info(String.format("重定向地址→sendRedirect%s",ssourl));
httpServletResponse.sendRedirect(ssourl);
} catch (IOException e) {
@ -59,31 +57,28 @@ public class SSOPluginLogin implements ThirdSSOAuthHandler {
@Override
public UserAuthResult getTrdSSOAuth(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
logger.info(String.format("getTrdSSOAuth→httpServletRequest%s",httpServletRequest));
String code = httpServletRequest.getParameter("code");
//返回的认证结果
UserAuthResult result=new UserAuthResult();
//获取返回的授权码
String code = httpServletRequest.getParameter("code");
logger.info(String.format("getTrdSSOAuth→code%s",code));
if (StringUtils.isEmpty(code)){
logger.error("getTrdSSOAuth→统一认证授权码为空");
logger.error("getTrdSSOAuth→code为空");
result.setSucess(false);
}else {
try {
//TODO:调用accessToken方法
String data = AuthService.accessToken(code, EOSS_IP, CLIENT_ID, CLIENT_SECRET);
logger.info(String.format("accessToken返回数据%s", data));
String access_token = AuthService.accessToken(code, EOSS_IP, CLIENT_ID, CLIENT_SECRET);
logger.info(String.format("accessToken返回数据%s", access_token));
//TODO:调用getUserInfo方法
String access_token = JSON.parseObject(data).getString("access_token");
String user = AuthService.getUserInfo(access_token, EOSS_IP, CLIENT_ID, CLIENT_SECRET);
logger.info(String.format("getUserInfo返回数据%s", user));
JSONObject jsonObject = JSON.parseObject(user);
JSONObject loginUser = jsonObject.getJSONObject("data");
String loginName = loginUser.getString("loginName");//登录账户
String userName = loginUser.getString("Nm");//用户姓名
if (loginName != null) {
if (user != null) {
//当前返回类型手机用户名email工号
result.setUserType(UserName);
result.setUser(loginName);
result.setUserType(UserProperType.UserName);
result.setUser(user);
result.setSucess(true);
logger.info("SSO用户登录成功进入苍穹系统");
}