获取code参数代码修改
This commit is contained in:
parent
08b693c16d
commit
9c919f6896
|
@ -2,6 +2,7 @@ package shkd.sys.sys.eoss;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import kd.bos.config.client.util.StringUtils;
|
||||||
import kd.bos.logging.Log;
|
import kd.bos.logging.Log;
|
||||||
import kd.bos.logging.LogFactory;
|
import kd.bos.logging.LogFactory;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
|
@ -9,13 +10,12 @@ import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @program: laxv-cosmic
|
|
||||||
* @author: Lang
|
* @author: Lang
|
||||||
* @create: 2024-11-08 10:50
|
|
||||||
* @description: 认证服务
|
* @description: 认证服务
|
||||||
**/
|
**/
|
||||||
public class AuthService {
|
public class AuthService {
|
||||||
|
@ -26,28 +26,28 @@ public class AuthService {
|
||||||
* @param code
|
* @param code
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static String accessToken(String code,String eoss_ip,String client_id,String client_secret) {
|
public static String accessToken(String code,String eoss_ip,String client_id,String client_secret) {
|
||||||
HttpHeaders api_headers = new HttpHeaders();//请求头
|
HttpHeaders api_headers = new HttpHeaders();//请求头
|
||||||
api_headers.add("Content-Type", "application/json; charset=utf-8");
|
api_headers.add("Content-Type", "application/json; charset=utf-8");
|
||||||
HashMap<String, Object> requestBody = new HashMap<>();
|
HttpEntity<String> api_requestEntity = new HttpEntity<>(api_headers);
|
||||||
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);
|
|
||||||
|
|
||||||
String url = String.format("%s/sso2/authCenter/accessToken", eoss_ip);
|
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)));
|
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());
|
JSONObject jsonObject = JSON.parseObject(response.getBody());
|
||||||
logger.info(String.format("accessToken→返回结果:%s",jsonObject));
|
logger.info(String.format("accessToken→返回结果:%s",jsonObject));
|
||||||
if ("0".equals(jsonObject.get("code").toString())){
|
String access_token = jsonObject.getString("access_token");
|
||||||
String data = jsonObject.getString("data");
|
if (!StringUtils.isEmpty(access_token)){
|
||||||
return data;
|
return access_token;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -61,21 +61,25 @@ public class AuthService {
|
||||||
logger.info("getUserInfo→请求token:%s", token);
|
logger.info("getUserInfo→请求token:%s", token);
|
||||||
HttpHeaders api_headers = new HttpHeaders();//请求头
|
HttpHeaders api_headers = new HttpHeaders();//请求头
|
||||||
api_headers.add("Content-Type", "application/json; charset=utf-8");
|
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);
|
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)));
|
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());
|
JSONObject jsonObject = JSON.parseObject(response.getBody());
|
||||||
logger.info(String.format("getUserInfo→返回结果:%s",jsonObject));
|
logger.info(String.format("getUserInfo→返回结果:%s",jsonObject));
|
||||||
if ("0".equals(jsonObject.get("code").toString())){
|
JSONObject obj = jsonObject.getJSONObject("data");
|
||||||
String data = jsonObject.getString("data");
|
if (obj!=null){
|
||||||
return data;
|
return obj.getString("loginName");
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,13 @@ import kd.bos.logging.Log;
|
||||||
import kd.bos.logging.LogFactory;
|
import kd.bos.logging.LogFactory;
|
||||||
import kd.bos.login.thirdauth.ThirdSSOAuthHandler;
|
import kd.bos.login.thirdauth.ThirdSSOAuthHandler;
|
||||||
import kd.bos.login.thirdauth.UserAuthResult;
|
import kd.bos.login.thirdauth.UserAuthResult;
|
||||||
|
import kd.bos.login.thirdauth.UserProperType;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import shkd.sys.sys.eoss.AuthService;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static kd.bos.login.thirdauth.UserProperType.UserName;
|
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_ID = System.getProperty("clientKey-scict");
|
||||||
private final String CLIENT_SECRET = System.getProperty("clientSecret-scict");
|
private final String CLIENT_SECRET = System.getProperty("clientSecret-scict");
|
||||||
private final String EOSS_IP = System.getProperty("ip-port");
|
private final String EOSS_IP = System.getProperty("ip-port");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 方法实现用户没有登录的时候跳转认证中心的登录地址
|
* 方法实现用户没有登录的时候跳转认证中心的登录地址
|
||||||
* @param httpServletRequest
|
* @param httpServletRequest
|
||||||
|
@ -58,35 +57,28 @@ public class SSOPluginLogin implements ThirdSSOAuthHandler {
|
||||||
@Override
|
@Override
|
||||||
public UserAuthResult getTrdSSOAuth(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
|
public UserAuthResult getTrdSSOAuth(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
|
||||||
logger.info(String.format("getTrdSSOAuth→httpServletRequest:%s",httpServletRequest));
|
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();
|
UserAuthResult result=new UserAuthResult();
|
||||||
|
//获取返回的授权码
|
||||||
|
String code = httpServletRequest.getParameter("code");
|
||||||
|
logger.info(String.format("getTrdSSOAuth→code:%s",code));
|
||||||
if (StringUtils.isEmpty(code)){
|
if (StringUtils.isEmpty(code)){
|
||||||
logger.error("getTrdSSOAuth→统一认证授权码为空");
|
logger.error("getTrdSSOAuth→code为空");
|
||||||
result.setSucess(false);
|
result.setSucess(false);
|
||||||
}else {
|
}else {
|
||||||
try {
|
try {
|
||||||
//TODO:调用accessToken方法
|
//TODO:调用accessToken方法
|
||||||
String data = AuthService.accessToken(code, EOSS_IP, CLIENT_ID, CLIENT_SECRET);
|
String access_token = AuthService.accessToken(code, EOSS_IP, CLIENT_ID, CLIENT_SECRET);
|
||||||
logger.info(String.format("accessToken返回数据:%s", data));
|
logger.info(String.format("accessToken返回数据:%s", access_token));
|
||||||
|
|
||||||
//TODO:调用getUserInfo方法
|
//TODO:调用getUserInfo方法
|
||||||
String access_token = JSON.parseObject(data).getString("access_token");
|
|
||||||
String user = AuthService.getUserInfo(access_token, EOSS_IP, CLIENT_ID, CLIENT_SECRET);
|
String user = AuthService.getUserInfo(access_token, EOSS_IP, CLIENT_ID, CLIENT_SECRET);
|
||||||
logger.info(String.format("getUserInfo返回数据:%s", user));
|
logger.info(String.format("getUserInfo返回数据:%s", user));
|
||||||
|
|
||||||
JSONObject jsonObject = JSON.parseObject(user);
|
if (user != null) {
|
||||||
JSONObject loginUser = jsonObject.getJSONObject("data");
|
|
||||||
String loginName = loginUser.getString("loginName");//登录账户
|
|
||||||
String userName = loginUser.getString("Nm");//用户姓名
|
|
||||||
if (loginName != null) {
|
|
||||||
//当前返回类型手机,用户名,email,工号
|
//当前返回类型手机,用户名,email,工号
|
||||||
result.setUserType(UserName);
|
result.setUserType(UserProperType.UserName);
|
||||||
result.setUser(loginName);
|
result.setUser(user);
|
||||||
result.setSucess(true);
|
result.setSucess(true);
|
||||||
logger.info("SSO用户登录成功,进入苍穹系统");
|
logger.info("SSO用户登录成功,进入苍穹系统");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue