You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
211 lines
8.1 KiB
211 lines
8.1 KiB
package com.fr.plugin.third.party.jsdidig; |
|
|
|
import com.fanruan.api.log.LogKit; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fr.plugin.third.party.jsdidig.config.CustomDataConfig; |
|
import com.fr.third.org.apache.http.HttpEntity; |
|
import com.fr.third.org.apache.http.HttpStatus; |
|
import com.fr.third.org.apache.http.client.config.RequestConfig; |
|
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse; |
|
import com.fr.third.org.apache.http.client.methods.HttpGet; |
|
import com.fr.third.org.apache.http.client.methods.HttpPost; |
|
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier; |
|
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
|
import com.fr.third.org.apache.http.entity.StringEntity; |
|
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; |
|
import com.fr.third.org.apache.http.impl.client.HttpClients; |
|
import com.fr.third.org.apache.http.ssl.SSLContextBuilder; |
|
import com.fr.third.org.apache.http.ssl.TrustStrategy; |
|
import com.fr.third.org.apache.http.util.EntityUtils; |
|
import com.fr.third.springframework.web.util.UriUtils; |
|
|
|
import javax.net.ssl.HostnameVerifier; |
|
import javax.net.ssl.SSLContext; |
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.io.IOException; |
|
import java.io.UnsupportedEncodingException; |
|
import java.security.cert.CertificateException; |
|
import java.security.cert.X509Certificate; |
|
import java.util.UUID; |
|
|
|
public class Utils { |
|
public static String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"; |
|
public static RequestConfig REQUEST_CONFIG = RequestConfig.custom() |
|
.setConnectionRequestTimeout(30000) |
|
.setSocketTimeout(30000) // 服务端相应超时 |
|
.setConnectTimeout(30000) // 建立socket链接超时时间 |
|
.build(); |
|
|
|
public static CloseableHttpClient createSSLClientDefault() { |
|
try { |
|
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { |
|
|
|
@Override |
|
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
|
return true; |
|
} |
|
}).build(); |
|
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; |
|
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); |
|
return HttpClients.custom().setSSLSocketFactory(sslsf).build(); |
|
} catch (Exception e) { |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
return HttpClients.createDefault(); |
|
} |
|
|
|
public static synchronized CloseableHttpClient createHttpClient(String url) { |
|
CloseableHttpClient httpClient = null; |
|
if (StringKit.isEmpty(url)) { |
|
httpClient = HttpClients.createDefault(); |
|
return httpClient; |
|
} |
|
|
|
if (url.startsWith("https://")) { |
|
httpClient = createSSLClientDefault(); |
|
return httpClient; |
|
} |
|
httpClient = HttpClients.createDefault(); |
|
return httpClient; |
|
} |
|
|
|
public static synchronized String createHttpGetContent(CloseableHttpClient httpClient, String url, String basicAuth) throws IOException { |
|
if ((httpClient == null) || (StringKit.isEmpty(url))) { |
|
return ""; |
|
} |
|
|
|
HttpGet httpGet = new HttpGet(url); |
|
httpGet.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT); |
|
if (StringKit.isNotEmpty(basicAuth)) { |
|
httpGet.addHeader("Authorization", basicAuth); |
|
} |
|
|
|
httpGet.setConfig(Utils.REQUEST_CONFIG); |
|
CloseableHttpResponse response = httpClient.execute(httpGet); |
|
int statusCode = response.getStatusLine().getStatusCode(); |
|
if (statusCode != HttpStatus.SC_OK) { |
|
response.close(); |
|
LogKit.info("http请求出错,http status:" + statusCode); |
|
return ""; |
|
} |
|
|
|
HttpEntity httpEntity = response.getEntity(); |
|
if (httpEntity == null) { |
|
response.close(); |
|
LogKit.info("http请求出错,http响应内容为空"); |
|
return ""; |
|
} |
|
String responseContent = EntityUtils.toString(httpEntity, "UTF-8"); |
|
response.close(); |
|
if (StringKit.isEmpty(responseContent)) { |
|
LogKit.info("http请求出错,http响应内容为空1"); |
|
return ""; |
|
} |
|
return responseContent; |
|
} |
|
|
|
public static synchronized String createHttpPostContent(CloseableHttpClient httpClient, String url, String bodyContent) throws IOException { |
|
if ((httpClient == null) || (StringKit.isEmpty(url)) || (StringKit.isEmpty(bodyContent))) { |
|
return ""; |
|
} |
|
|
|
HttpPost httpPost = new HttpPost(url); |
|
httpPost.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT); |
|
httpPost.setConfig(Utils.REQUEST_CONFIG); |
|
StringEntity bodyEntity = new StringEntity(bodyContent, "UTF-8"); |
|
httpPost.setEntity(bodyEntity); |
|
CloseableHttpResponse response = httpClient.execute(httpPost); |
|
int statusCode = response.getStatusLine().getStatusCode(); |
|
if (statusCode != HttpStatus.SC_OK) { |
|
response.close(); |
|
LogKit.info("http请求出错,http status:" + statusCode); |
|
return ""; |
|
} |
|
|
|
HttpEntity httpEntity = response.getEntity(); |
|
if (httpEntity == null) { |
|
response.close(); |
|
LogKit.info("http请求出错,http响应内容为空"); |
|
return ""; |
|
} |
|
String responseContent = EntityUtils.toString(httpEntity, "UTF-8"); |
|
response.close(); |
|
if (StringKit.isEmpty(responseContent)) { |
|
LogKit.info("http请求出错,http响应内容为空1"); |
|
return ""; |
|
} |
|
return responseContent; |
|
} |
|
|
|
|
|
/** |
|
* 获取完整请求链接 |
|
* |
|
* @param req 请求 |
|
* @return |
|
*/ |
|
public static String getFullRequestUrl(HttpServletRequest req) { |
|
if (req == null) { |
|
return ""; |
|
} |
|
String url = req.getRequestURL().toString(); |
|
String queryUrl = req.getQueryString(); |
|
if ((queryUrl == null) || "null".equalsIgnoreCase(queryUrl)) { |
|
queryUrl = ""; |
|
} else { |
|
queryUrl = "?" + queryUrl; |
|
} |
|
String fullUrl = url + queryUrl; |
|
return fullUrl; |
|
} |
|
|
|
|
|
/** |
|
* 重定向 |
|
* |
|
* @param res |
|
* @param url |
|
*/ |
|
public static void sendRedirect(HttpServletResponse res, String url) { |
|
if ((res == null) || (StringKit.isEmpty(url))) { |
|
return; |
|
} |
|
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); |
|
res.setHeader("Location", url); |
|
} |
|
|
|
|
|
public static synchronized String getUuid() { |
|
String uuid = UUID.randomUUID().toString().replace("-", ""); |
|
return uuid; |
|
} |
|
|
|
public static String getOAuthCodeUrl(String url) throws UnsupportedEncodingException { |
|
|
|
String tempUrl = UriUtils.encodeQueryParam(url, "UTF-8"); |
|
LogKit.info("集成登录,授权报表地址:" + url); |
|
//if (tempUrl.indexOf("?") >= 0) { |
|
// tempUrl = UriUtils.encodeQueryParam(tempUrl, "UTF-8"); |
|
//} |
|
String authUrl = CustomDataConfig.getInstance().getoAuthCodeUrl() + "?client_id=" + CustomDataConfig.getInstance().getIdmClientId() + "&redirect_uri=" + tempUrl + "&response_type=code"; |
|
LogKit.info("集成登录,获取临时授权码地址:" + authUrl); |
|
return authUrl; |
|
} |
|
|
|
|
|
public static String getOAuthCodeUrlWithFr() throws UnsupportedEncodingException { |
|
String url = CustomDataConfig.getInstance().getFrUrl(); |
|
return getOAuthCodeUrl(url); |
|
} |
|
|
|
|
|
public static String getLogoutUrl() throws UnsupportedEncodingException { |
|
String url = CustomDataConfig.getInstance().getFrUrl(); |
|
String tempUrl = UriUtils.encodeQueryParam(url, "UTF-8"); |
|
String logoutUrl = CustomDataConfig.getInstance().getLogoutUrl() + "?service=" + tempUrl + "&client_id=" + CustomDataConfig.getInstance().getIdmClientId(); |
|
return logoutUrl; |
|
} |
|
|
|
|
|
}
|
|
|