Tomcat and proxy settings

老子叫甜甜 提交于 2019-12-30 06:17:14

问题


There is a servlet running on tomcat7 and it makes a webservice call to a third party website. The call works fine from the windows machine but when run from tomcat it fails. Wont Tomcat automatically use the Windows' proxy settings? I added

set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxySet=true"
set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxyHost=IP"
set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxyPort=8080"

to CATALINA.BAT and

http.proxyHost=IP
http.proxyPort=8080

to catalina.properties But still there is no change. How do we set Tomcat to use the proxy settings of windows and is there a way to check if tomcat is picking up the proxy settings specified?


回答1:


No, Tomcat won't automatically use the system proxy settings.

I suggest you look into the facilities provided by java.net.Proxy. This allows you to dynamically specifiy a proxy at runtime. The system properties work but they are only read once, and if Tomcat has already used an HttpURLConnection for its own purposes prior to you setting them that's the end of that: the setting has no effect.




回答2:


I do not agree with the usage of java.net.Proxy.

What happens if you need to change it ? New build, new release. The setting of the proxy should be easy. It works well with both system properties or tomcat JAVA_OPTS. I used it in both ways. Just pay attention and be sure you know what JAVA_OPTS are loaded, what java is used and so on, because there are tomcats that have their own java version. Regardint the previous post, there is no way java can be used before was loaded :). So Tomcat cannot use it before the system properties are used...only if tomcat uses another JRE that does not read system properties.

I just test this setup :

set "JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=proxy.com -Dhttp.proxyPort=8080 "

in catalina.bat and works well.




回答3:


While specifying proxy settings, you have to define the proxy server name like below:

"-Dhttp.proxyHost=proxy.example.com"



回答4:


Create a /bin/setenv.sh (for WINDOWS \bin\setenv.bat):

JAVA_OPTS="-Dhttp.proxySet=true -Dhttp.proxyHost=<proxy_hostname> -Dhttp.proxyPort=<port_number> -Dhttp.nonProxyHosts=<domain_one>|<domain two> $JAVA_OPTS"

NOTE: if you already have setenv.sh/setenv.bat, you can add a line of above command. Tomcat startup script automatically runs setenv script before starting a tomcat instance.




回答5:


You can implement HTTP proxy, HTTPS proxy and HTTP/HTTPS non-proxy hosts also in Tomcat. You need to update two files i.e, bin/Catalina.sh and conf/catalina.properties.




回答6:


You can use jProxyLoader library. Using this lib you can configure Tomcat to use proxy only for connections to specific host. In your case you can configure Tomcat to go via proxy only for connections to host serving the webservice (all the other connections will be handled by Tomcat "normal" way - without proxy).

Complete setup is explained on project website: http://jproxyloader.sourceforge.net/examples/web-application-on-tomcat.html



来源:https://stackoverflow.com/questions/12309800/tomcat-and-proxy-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!