關(guān)于如何使用java代碼設(shè)置代理服務(wù)器
在今年年底和外資合作做的一個項目中,由于客戶限制了服務(wù)器的外網(wǎng)訪問權(quán)限,導(dǎo)致很多涉及到了第三方的API都無法進(jìn)行訪問,小編給項目組的成員提了一個建議
由于項目組的開發(fā)人員公共使用的調(diào)用第三方的工具類是基于
org.springframework.web.client.RestTemplate
進(jìn)行開發(fā)的,所以本節(jié)我們就講解RestTemplate
中如何使用java代理
1.)當(dāng)使用的代理服務(wù)器不需要密碼驗證時(使用系統(tǒng)參數(shù)進(jìn)行設(shè)置代理)'作用域:整個系統(tǒng)
'
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
工具類中,在項目進(jìn)行啟動的時候就進(jìn)行全局設(shè)置代理,這個方法的作用范圍是整個系統(tǒng);
2.)當(dāng)使用的代理服務(wù)器不需要密碼驗證時(使用Proxy設(shè)置代理)'作用域:指定的請求URL
'
public String getData(String url, Map<String, String> param) throws IOException {
// 設(shè)置代理
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.)當(dāng)使用的代理服務(wù)器需要密碼驗證時(使用Proxy設(shè)置代理)作用域:指定的URL
由于代理服務(wù)器需要密碼驗證,所以我們需要使用java.net.Authenticator.Authenticator.setDefault(Authenticator authenticator)
來注冊實現(xiàn)密碼驗證
public class AuthenticatorUtil extends Authenticator {
// 代理服務(wù)器用戶名
private String user = "";
//代理服務(wù)器密碼
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 設(shè)置代理服務(wù)器用戶名和密碼
Authenticator.setDefault(new AuthenticatorUtil("username", "password"));
// TODO: 2023/1/5 設(shè)置代理服務(wù)器的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網(wǎng)站時會報:'PKIX path building failed'
這個問題也是在做開發(fā)的時候遇到的,報錯如下
"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進(jìn)行請求的話,所以就有了下面的工具類,用程序重新方法,信任所有的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 {
//創(chuàng)建日志記錄工具
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之前調(diào)用
* @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();
//再發(fā)送請求之前調(diào)用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
+關(guān)注
關(guān)注
20文章
2989瀏覽量
109642 -
開發(fā)
+關(guān)注
關(guān)注
0文章
373瀏覽量
41517 -
工具
+關(guān)注
關(guān)注
4文章
315瀏覽量
28290
發(fā)布評論請先 登錄
10個關(guān)于linux中Squid代理服務(wù)器的實用面試問答
linux通過代理服務(wù)器來上網(wǎng)
網(wǎng)絡(luò)代理服務(wù)器的磁盤驅(qū)動器
網(wǎng)絡(luò)代理服務(wù)器的內(nèi)存
網(wǎng)絡(luò)代理服務(wù)器的網(wǎng)絡(luò)接口
網(wǎng)絡(luò)代理服務(wù)器
Apache代理服務(wù)器配置說明
vpn代理服務(wù)器_VPN代理軟件下載
代理服務(wù)器IP如何使用,這幾點需要注意了
恒訊科技分析:代理服務(wù)器的類型有哪些?

評論