diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/AuthService.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/AuthService.java index 45ac4c8..42ca4e2 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/AuthService.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/AuthService.java @@ -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 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 api_requestEntity = new HttpEntity<>(api_jsonBody, api_headers); - + HttpEntity 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 response = restTemplate.postForEntity(url, api_requestEntity, String.class); + ResponseEntity 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 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 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 api_requestEntity = new HttpEntity<>(api_headers); + logger.info(String.format("getUserInfo→请求地址:%s,请求参数:%s",url,JSON.toJSONString(api_requestEntity))); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, api_requestEntity, String.class, requestBody); + ResponseEntity 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; } diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/SSOPluginLogin.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/SSOPluginLogin.java index a2011ca..6c3714c 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/SSOPluginLogin.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/eoss/SSOPluginLogin.java @@ -6,12 +6,13 @@ 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 shkd.sys.sys.eoss.AuthService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; + import static kd.bos.login.thirdauth.UserProperType.UserName; /** @@ -23,8 +24,6 @@ public class SSOPluginLogin implements ThirdSSOAuthHandler { private final String CLIENT_ID = System.getProperty("clientKey-scict"); private final String CLIENT_SECRET = System.getProperty("clientSecret-scict"); private final String EOSS_IP = System.getProperty("ip-port"); - - /** * 方法实现用户没有登录的时候跳转认证中心的登录地址 * @param httpServletRequest @@ -58,35 +57,28 @@ public class SSOPluginLogin implements ThirdSSOAuthHandler { @Override public UserAuthResult getTrdSSOAuth(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { logger.info(String.format("getTrdSSOAuth→httpServletRequest:%s",httpServletRequest)); - //获取返回的授权码 - String queryString = httpServletRequest.getQueryString(); - JSONObject queryBody = JSON.parseObject(queryString); - logger.info(String.format("getTrdSSOAuth→queryString:%s,授权码:%s",queryString, httpServletRequest.getParameter("code"))); - String code = queryBody.getString("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用户登录成功,进入苍穹系统"); }