關于如何使用java代碼設置代理服務器
在今年年底和外資合作做的一個項目中,由于客戶限制了服務器的外網訪問權限,導致很多涉及到了第三方的API都無法進行訪問,小編給項目組的成員提了一個建議
由于項目組的開發人員公共使用的調用第三方的工具類是基于
org.springframework.web.client.RestTemplate進行開發的,所以本節我們就講解RestTemplate中如何使用java代理
1.)當使用的代理服務器不需要密碼驗證時(使用系統參數進行設置代理)'作用域:整個系統'
static {
String proxyHost = "代理的ip地址或域名";
String proxyPort = "代理的端口";
System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("http.proxyHost", proxyHost);
System.getProperties().setProperty("http.proxyPort", proxyPort);
System.getProperties().setProperty("https.proxyHost", proxyHost);
System.getProperties().setProperty("https.proxyPort", proxyPort);
}
上面這一部分代碼可以放到RestTemplate工具類中,在項目進行啟動的時候就進行全局設置代理,這個方法的作用范圍是整個系統;
2.)當使用的代理服務器不需要密碼驗證時(使用Proxy設置代理)'作用域:指定的請求URL'
public String getData(String url, Map<String, String> param) throws IOException {
// 設置代理
SocketAddress socketAddress = new InetSocketAddress("代理的ip地址或域名", 代理的端口);
Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
URL proxyUrl = new URL(url);
proxyUrl.openConnection(proxy);
// 請勿輕易改變此提交方式,大部分的情況下,提交方式都是表單提交
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
return restTemplate.getForEntity(url, String.class, param).getBody();
}
3.)當使用的代理服務器需要密碼驗證時(使用Proxy設置代理)作用域:指定的URL
由于代理服務器需要密碼驗證,所以我們需要使用java.net.Authenticator.Authenticator.setDefault(Authenticator authenticator)來注冊實現密碼驗證
public class AuthenticatorUtil extends Authenticator {
// 代理服務器用戶名
private String user = "";
//代理服務器密碼
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
public String getData(String url, Map<String, String> param) throws IOException {
// TODO: 2023/1/5 設置代理服務器用戶名和密碼
Authenticator.setDefault(new AuthenticatorUtil("username", "password"));
// TODO: 2023/1/5 設置代理服務器的ip地址(域名)和端口
SocketAddress socketAddress = new InetSocketAddress("代理的ip地址或域名", 代理的端口);
Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
URL proxyUrl = new URL(url);
proxyUrl.openConnection(proxy);
// 請勿輕易改變此提交方式,大部分的情況下,提交方式都是表單提交
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
return restTemplate.getForEntity(url, String.class, param).getBody();
}
Java在請求某些不受信任的https網站時會報:'PKIX path building failed'
這個問題也是在做開發的時候遇到的,報錯如下
"I/0 error on PosT reguest for "https://sfapi-sbox,sf-express.com/std/service ": sunsecurity.validator.ValidatorException: PKIX pathbuilding failed: sun,security,provider.certpathSunCertPathbuilderException: unable to find valid certification path to reguested target; nestedexception is iavax.net.ssl.SSLHandshakeException: sun,security.validator.ValidatorException: PKIX pathbuilding failed: sun,security.provider,certpath,SunCertPathbuilderException: unable to find validcertification-path to requested target"
有些小伙伴看到這個報錯,可能以為是請求不通,其實并不是,而是https://sfapi-sbox,sf-express.com/std/service這個地址不受信任,使用https進行請求的話,所以就有了下面的工具類,用程序重新方法,信任所有的SSL證書
package com.vca.common.utils;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* @Description:信任所有證書工具類
* @author:chenbing
* @date 2023/1/4 17:40
*/
public class SslUtil {
//創建日志記錄工具
public static final Logger logger = LoggerFactory.getLogger(SslUtil.class);
private static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements TrustManager, X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
}
/**
* @Description:忽略HTTPS請求的SSL證書,必須在openConnection之前調用
* @author:chenbing
* @date 2023/1/4 18:00
*/
@SneakyThrows
public static void ignoreSsl() {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
logger.info("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}
public String postFormData(String url, MultiValueMap<String, String> map) {
HttpHeaders headers = new HttpHeaders();
//再發送請求之前調用ignoreSsl()方法,忽略掉HTTPS請求的SSL證書
SslUtil.ignoreSsl();
// headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntityString, String>> requests = new HttpEntityString, String>>(map, headers);
String body = restTemplate.postForEntity(url, requests, String.class).getBody();
return body;
}
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
JAVA
+關注
關注
20文章
3001瀏覽量
116419 -
開發
+關注
關注
0文章
378瀏覽量
42144 -
工具
+關注
關注
4文章
317瀏覽量
28868
發布評論請先 登錄
相關推薦
熱點推薦
10個關于linux中Squid代理服務器的實用面試問答
不僅是系統管理員和網絡管理員時不時會聽到“代理服務器”這個詞,我們也經常聽到。代理服務器已經成為一種企業常態,而且經常會接觸到它。它現在也出現在一些小型的學校或者大型跨國公司的自助餐廳里。Squid
發表于 09-28 10:19
Apache代理服務器配置說明
Apache代理服務器 1. 安裝apache。 2. 修改\Apache\conf\httpd.conf配置文件, 首先要添加代理服務器模塊。找到下面這幾行: #LoadModule
發表于 04-29 16:49
?0次下載
代理服務器IP如何使用,這幾點需要注意了
作為信息的中轉站,偽裝自己的真實IP,保障上網安全。那么,如何安全且合理的使用代理IP技術呢? 代理服務器是介于客戶端和Web服務器之間的另一臺服務器,有了它之后,瀏覽
恒訊科技分析:代理服務器的類型有哪些?
代理服務器是一臺攔截和管理兩個設備、網絡或協議之間的流量的計算機。代理是充當我們的計算機與我們正在使用的網站和互聯網服務之間的中介的網關。它們可以用作防火墻、過濾器、緩存或促進共享網絡連接。這是
代理服務器用戶名,主要作用是什么?
代理服務器用戶名是指用于訪問和控制代理服務器的身份驗證信息之一。用戶名通常由代理服務器的管理員或服務提供商設定,用于確保只有授權用戶能夠訪問和使用代
使用Python構建高效的HTTP代理服務器
構建一個高效的HTTP代理服務器在Python中涉及多個方面,包括性能優化、并發處理、協議支持(HTTP/HTTPS)、錯誤處理以及日志記錄等。
Python中代理服務器的配置與應用
在網絡通信中,代理服務器作為一種重要的網絡中間件,充當著客戶端和目標服務器之間的中間人角色。它能夠接收來自客戶端的請求,并將這些請求轉發給目標服務器,然后將服務器的響應返回給客戶端。P
如何使用java代碼設置代理服務器?
評論