添加互联网端,单点登录基础类

This commit is contained in:
sh
2025-11-16 18:32:13 +08:00
parent 2a866ce0e6
commit cc37ece461
12 changed files with 394 additions and 5 deletions

View File

@@ -56,6 +56,22 @@ public class HttpUtils {
return doPost(url, jsonParams, JSON_MEDIA_TYPE, null, connectTimeout, readTimeout, writeTimeout);
}
/**
* POST JSON请求默认超时支持自定义 headers
*/
public static String doPostJson(String url, Map<String, Object> params, Map<String, String> headers) throws TimeoutException, IOException {
return doPostJson(url, params, headers, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, DEFAULT_WRITE_TIMEOUT);
}
/**
* POST JSON请求自定义超时和 headers
*/
public static String doPostJson(String url, Map<String, Object> params, Map<String, String> headers,
int connectTimeout, int readTimeout, int writeTimeout) throws TimeoutException, IOException {
String jsonParams = CollectionUtils.isEmpty(params) ? "{}" : JSON.toJSONString(params);
return doPost(url, jsonParams, JSON_MEDIA_TYPE, headers, connectTimeout, readTimeout, writeTimeout);
}
/**
* POST 表单请求(默认超时)
*/

View File

@@ -7,6 +7,9 @@ import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.core.domain.entity.tymh.authority.QxUserRole;
import com.ruoyi.common.core.domain.entity.tymh.nwToken.NwTokenResult;
import com.ruoyi.common.core.domain.entity.tymh.nwToken.NwUserInfoResult;
import com.ruoyi.common.core.domain.entity.tymh.wwToken.WwTokenResult;
import com.ruoyi.common.core.domain.entity.tymh.wwToken.WwTyInfo;
import com.ruoyi.common.core.domain.entity.tymh.wwToken.WwUserLogin;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.crypto.CryptoUtil;
import org.springframework.beans.factory.annotation.Value;
@@ -38,6 +41,8 @@ public class OauthClient {
private String wwRegisterPostUrl;
@Value("${oauth.usptww.wwTokenPostUrl}")
private String wwTokenPostUrl;
@Value("${oauth.usptww.wwQueryWebUserInfo}")
private String wwQueryWebUserInfo;
@Value("${oauth.usptww.wwQueryWebPersonalInfoPostUrl}")
private String wwQueryWebPersonalInfoPostUrl;
@Value("${oauth.usptww.wwQueryWebEnterpriseInfoPostUrl}")
@@ -173,6 +178,31 @@ public class OauthClient {
return parseMapOrDataArray(responseJson, operationName, clazz);
}
/**
* 传递heades
* @param url
* @param params
* @param typeReference
* @param operationName
* @return
* @param <T>
* @throws IOException
* @throws TimeoutException
*/
private <T> T executePostRequestHeaders(String url, Map<String, Object> params,Map<String,String> headers,
TypeReference<Response<T>> typeReference, String operationName)
throws IOException, TimeoutException {
String responseJson = HttpUtils.doPostJson(
url,
params,
headers,
connectTimeout,
readTimeout,
writeTimeout
);
return validateResponse(responseJson, operationName, typeReference);
}
/**
* 简化后的响应校验方法(已移除网关返回码判断)
*/
@@ -277,6 +307,78 @@ public class OauthClient {
return (value instanceof JSONArray) ? (JSONArray) value : null;
}
/**
* 互联网-获取token
* @param wwUserLogin
* @return
* @throws Exception
*/
public WwTokenResult wwGetToken(WwUserLogin wwUserLogin) throws Exception {
if (wwUserLogin == null) {
log.error("获取互联网Token失败登录参数不能为空");
throw new IllegalArgumentException("登录参数不能为空");
}
if (StringUtils.isEmpty(wwUserLogin.getUsername())) {
log.error("获取互联网Token失败身份证号/统一社会信用代码不能为空");
throw new IllegalArgumentException("身份证号/统一社会信用代码不能为空");
}
if (StringUtils.isEmpty(wwUserLogin.getUsertype())) {
log.error("获取互联网Token失败用户类型usertype不能为空");
throw new IllegalArgumentException("用户类型usertype不能为空");
}
Map<String, Object> params = new HashMap<>(4);
params.put("username", wwUserLogin.getUsername());
params.put("usertype", wwUserLogin.getUsertype());
if (StringUtils.isNotEmpty(wwUserLogin.getLogontype())) {
params.put("logontype", wwUserLogin.getLogontype());
}
if (StringUtils.isNotEmpty(wwUserLogin.getLogonchannel())) {
params.put("logonchannel", wwUserLogin.getLogonchannel());
}
WwTokenResult tokenResult = executePostRequest(
wwTokenPostUrl,
params,
new TypeReference<OauthClient.Response<WwTokenResult>>() {},
"获取互联网Token"
);
if (tokenResult == null) {
log.error("获取互联网Token失败接口返回业务数据为空");
throw new Exception("获取互联网Token失败返回数据异常");
}
log.info("获取互联网Token成功 | username{} | accessToken{}", wwUserLogin.getUsername(), tokenResult);
return tokenResult;
}
/**
* 互联网-端根据token获取用户信息
* @param token
* @return
* @throws IOException
* @throws TimeoutException
*/
public WwTyInfo wwGetUserInfo(String token) throws IOException, TimeoutException{
if(StringUtils.isBlank(token)){
throw new IllegalArgumentException("token不能为空");
}
//headers
Map<String, String> headers = new HashMap<>();
headers.put("Access-Token", token);
headers.put("Content-Type", "application/json");
//parm
Map<String, Object> params = new HashMap<>(0);
return executePostRequestHeaders(
wwQueryWebUserInfo,
params,
headers,
new TypeReference<OauthClient.Response<WwTyInfo>>() {},
"获取互联网用户信息"
);
}
/**
* 静态返回解析
* @param <T>