如何基于http代理解決Java固定ip問題
背景:對接第三方接口,第三方為安全考慮,需要固定ip設(shè)置白名單。公司對外使用動態(tài)ip。
辦法:
1. 購買一臺ip固定的服務(wù)器,比如云虛擬服務(wù)器。搭建http代理服務(wù)器,比如prioxy。
2.安裝Privoxy代理軟件
yum install -y epel-release privoxy
編輯 /etc/privoxy/config 文件
修改綁定地址,搜索 listen-address ,修改需要綁定的IP
listen-address 0.0.0.0:8118
設(shè)置enable-remote-toggle 為 1
enable-remote-toggle 1
重啟服務(wù)
service privoxy restart
查看服務(wù)狀態(tài)
[root@localhost ~]# service privoxy status Redirecting to /bin/systemctl status privoxy.service ● privoxy.service - Privoxy Web Proxy With Advanced Filtering Capabilities Loaded: loaded (/usr/lib/systemd/system/privoxy.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2020-03-04 17:38:26 CST; 5s ago Process: 23138 ExecStart=/usr/sbin/privoxy --pidfile /run/privoxy.pid --user privoxy /etc/privoxy/config (code=exited, status=0/SUCCESS) Main PID: 23139 (privoxy) Memory: 872.0K CGroup: /system.slice/privoxy.service └─23139 /usr/sbin/privoxy --pidfile /run/privoxy.pid --user privox... Mar 04 17:38:25 localhost.localdomain systemd[1]: Starting Privoxy Web Proxy ... Mar 04 17:38:26 localhost.localdomain systemd[1]: Started Privoxy Web Proxy W... Hint: Some lines were ellipsized, use -l to show in full. [root@localhost ~]# netstat -anop | grep 8118 tcp 0 0 0.0.0.0:8118 0.0.0.0:* LISTEN 23139/privoxy off (0.00/0/0) tcp 0 0 172.16.1.120:8311 172.16.1.123:28118 ESTABLISHED 15519/nginx: worker off (0.00/0/0)
3.HttpClient 設(shè)置代理
HttpPost httpPost = new HttpPost(url); //設(shè)置代理IP,設(shè)置連接超時時間 、 設(shè)置 請求讀取數(shù)據(jù)的超時時間 、 設(shè)置從connect Manager獲取Connection超時時間、 HttpHost proxy = new HttpHost("172.16.1.120",8118); RequestConfig requestConfig = RequestConfig.custom() .setProxy(proxy) .setConnectTimeout(10000) .setSocketTimeout(10000) .setConnectionRequestTimeout(3000) .build(); httpPost.setConfig(requestConfig);
如果是無法修改的jar,本地如何調(diào)試?
找到目標(biāo)源代碼,在項目代碼目錄下新建一個和目標(biāo)代碼一樣的package,并新建同名類文件,拷貝目標(biāo)類代碼至新文件,然后進(jìn)行對應(yīng)代理的設(shè)置。
原理是代碼只加載一次,誰最優(yōu)先加載誰,從而實現(xiàn)代碼覆蓋。
注意:本地開發(fā)調(diào)試完畢后,請刪除這個覆蓋文件。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java理論基礎(chǔ)Stream API終端操作示例解析
這篇文章主要為大家介紹了java理論基礎(chǔ)Stream API終端操作示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03JAVA簡單工廠模式(從現(xiàn)實生活角度理解代碼原理)
本文主要介紹了JAVA簡單工廠模式(從現(xiàn)實生活角度理解代碼原理)的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03解決@Async(“taskExecutor“)異步線程報錯問題
這篇文章主要介紹了解決@Async(“taskExecutor“)異步線程報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08spring security結(jié)合jwt實現(xiàn)用戶重復(fù)登錄處理
本文主要介紹了spring security結(jié)合jwt實現(xiàn)用戶重復(fù)登錄處理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03