修改互联网单点登录
This commit is contained in:
@@ -16,5 +16,7 @@ public interface CompanyContactService {
|
||||
List<CompanyContact> getSelectList(CompanyContact companyContact);
|
||||
|
||||
int insertUpadteCompanyContact(List<CompanyContact> list);
|
||||
|
||||
int insertContact(CompanyContact contact);
|
||||
}
|
||||
|
||||
|
||||
@@ -39,4 +39,9 @@ public class CompanyContactServiceImpl extends ServiceImpl<CompanyContactMapper,
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertContact(CompanyContact contact) {
|
||||
return companyContactMapper.insert(contact);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.ruoyi.cms.util.oauth;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import lombok.Data;
|
||||
import okhttp3.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -9,6 +10,8 @@ import org.springframework.util.CollectionUtils;
|
||||
import java.io.IOException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
@@ -164,4 +167,49 @@ public class HttpUtils {
|
||||
headers.forEach(requestBuilder::header);
|
||||
}
|
||||
}
|
||||
|
||||
/**************************增加获取cookie开始******************************/
|
||||
@Data
|
||||
public static class ResponseWrapper {
|
||||
private String responseBody; // 响应体字符串
|
||||
private Map<String, List<String>> headers; // 响应头(key: 头名称,value: 多个值)
|
||||
}
|
||||
|
||||
// HttpUtils 中新增方法:发送 POST JSON 请求,返回响应体 + 响应头
|
||||
public static ResponseWrapper doPostJsonWithHeaders(String url, Map<String, Object> params,
|
||||
int connectTimeout, int readTimeout, int writeTimeout) throws IOException {
|
||||
String jsonParams = CollectionUtils.isEmpty(params) ? "{}" : JSON.toJSONString(params);
|
||||
MediaType mediaType = MediaType.parse("application/json");
|
||||
RequestBody requestBody = RequestBody.create(mediaType, jsonParams);
|
||||
|
||||
// 构建 OkHttpClient
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.connectTimeout(connectTimeout, TimeUnit.SECONDS)
|
||||
.readTimeout(readTimeout, TimeUnit.SECONDS)
|
||||
.writeTimeout(writeTimeout, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
// 构建请求
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
// 执行请求,获取响应体和响应头
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
String responseBody = response.body() != null ? response.body().string() : "";
|
||||
Map<String, List<String>> headers = new HashMap<>();
|
||||
for (String headerName : response.headers().names()) {
|
||||
headers.put(headerName, response.headers(headerName));
|
||||
}
|
||||
|
||||
// 封装并返回
|
||||
ResponseWrapper wrapper = new ResponseWrapper();
|
||||
wrapper.setResponseBody(responseBody);
|
||||
wrapper.setHeaders(headers);
|
||||
return wrapper;
|
||||
}
|
||||
}
|
||||
|
||||
/**************************增加获取cookie结束******************************/
|
||||
}
|
||||
@@ -307,6 +307,37 @@ public class OauthClient {
|
||||
return (value instanceof JSONArray) ? (JSONArray) value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从响应头中提取Cookie(工具方法)
|
||||
*/
|
||||
private String extractCookieFromHeaders(Map<String, List<String>> headers) {
|
||||
if (headers == null || headers.isEmpty()) {
|
||||
log.warn("响应头为空,无法提取Cookie");
|
||||
return "";
|
||||
}
|
||||
|
||||
List<String> cookies = headers.get("Set-Cookie");
|
||||
if (cookies == null || cookies.isEmpty()) {
|
||||
log.warn("响应头中未包含Set-Cookie信息");
|
||||
return "";
|
||||
}
|
||||
|
||||
// 拼接Cookie(只保留name=value部分,忽略路径、过期时间等附加信息)
|
||||
StringBuilder cookieSb = new StringBuilder();
|
||||
for (String cookie : cookies) {
|
||||
if (cookieSb.length() > 0) {
|
||||
cookieSb.append("; ");
|
||||
}
|
||||
int semicolonIndex = cookie.indexOf(';');
|
||||
if (semicolonIndex > 0) {
|
||||
cookieSb.append(cookie.substring(0, semicolonIndex));
|
||||
} else {
|
||||
cookieSb.append(cookie);
|
||||
}
|
||||
}
|
||||
return cookieSb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 互联网-获取token
|
||||
* @param wwUserLogin
|
||||
@@ -337,36 +368,51 @@ public class OauthClient {
|
||||
params.put("logonchannel", wwUserLogin.getLogonchannel());
|
||||
}
|
||||
|
||||
WwTokenResult tokenResult = executePostRequest(
|
||||
// 发送请求并获取响应体+响应头(包含Cookie)
|
||||
HttpUtils.ResponseWrapper responseWrapper = HttpUtils.doPostJsonWithHeaders(
|
||||
wwTokenPostUrl,
|
||||
params,
|
||||
new TypeReference<OauthClient.Response<WwTokenResult>>() {},
|
||||
"获取互联网Token"
|
||||
connectTimeout,
|
||||
readTimeout,
|
||||
writeTimeout
|
||||
);
|
||||
|
||||
String responseJson = responseWrapper.getResponseBody();
|
||||
WwTokenResult tokenResult = validateResponse(
|
||||
responseJson,
|
||||
"获取互联网Token",
|
||||
new TypeReference<Response<WwTokenResult>>() {}
|
||||
);
|
||||
|
||||
if (tokenResult == null) {
|
||||
log.error("获取互联网Token失败:接口返回业务数据为空");
|
||||
throw new Exception("获取互联网Token失败:返回数据异常");
|
||||
}
|
||||
log.info("获取互联网Token成功 | username:{} | accessToken:{}", wwUserLogin.getUsername(), tokenResult);
|
||||
|
||||
// 提取Cookie并设置到tokenResult中
|
||||
String cookie = extractCookieFromHeaders(responseWrapper.getHeaders());
|
||||
tokenResult.setSessionCookie(cookie);
|
||||
|
||||
log.info("获取互联网Token及Cookie成功 | username:{} | accessToken:{} | cookie:{}",
|
||||
wwUserLogin.getUsername(), tokenResult.getAccessToken(), cookie);
|
||||
|
||||
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不能为空");
|
||||
public WwTyInfo wwGetUserInfo(WwTokenResult wwTokenResult) throws IOException, TimeoutException{
|
||||
if(wwTokenResult==null){
|
||||
throw new IllegalArgumentException("wwTokenResult不能为空");
|
||||
}
|
||||
//headers
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Access-Token", token);
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Access-Token", wwTokenResult.getAccessToken());
|
||||
headers.put("Cookie", wwTokenResult.getSessionCookie());
|
||||
|
||||
//parm
|
||||
Map<String, Object> params = new HashMap<>(0);
|
||||
|
||||
Reference in New Issue
Block a user