Tomcat and proxy settings

前端 未结 6 1364
北海茫月
北海茫月 2021-01-04 10:40

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

相关标签:
6条回答
  • 2021-01-04 11:02

    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.

    0 讨论(0)
  • 2021-01-04 11:06

    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

    0 讨论(0)
  • 2021-01-04 11:08

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

    "-Dhttp.proxyHost=proxy.example.com"
    
    0 讨论(0)
  • 2021-01-04 11:09

    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.

    0 讨论(0)
  • 2021-01-04 11:22

    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.

    0 讨论(0)
  • 2021-01-04 11:26

    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.

    0 讨论(0)
提交回复
热议问题