Spring+CXF配置HTTP代理访问Internet

时间:2025-02-15 18:40:37

转载地址:/Hafeyang/archive/2010/01/12/

一个典型的CXF Webservice项目部署到正式环境后,服务器不能访问外网,只能通过代理访问外网,找了很多资料,刚开始想法是不用Spring的配置文件,直接用java编程访问webservice。在java的Http请求中使用代理的方法如下:

String authentication="username:password";//用户+”:”+密码
        String encodedLogin= new BASE64Encoder().encode(());
        Proxy proxy = new Proxy(.HTTP, new InetSocketAddress("IP", PORT));
        
        HttpsURLConnection conn = (HttpsURLConnection) (proxy );
        ("Proxy-Authorization", " Basic " + encodedLogin);

使用JaxWsProxyFactoryBean创建client

("", "true");
        ("", "IP");
        ("", "PORT");
        
        JaxWsProxyFactoryBean f = new JaxWsProxyFactoryBean();
        ("THE URL OF WEBSERVICE");
        (IService.class);
        IService client = (IService) ();

可以使用代理,但是用户名密码是不能像第一种方法中那样设置到HttpConnection里面去的。

又在网上找了很多资料,终于找到了一个配置的方法。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans" 
        xmlns:xsi="http:///2001/XMLSchema-instance" 
        xmlns:cxf="/core" 
        xmlns:sec="/configuration/security" 
        xmlns:http-conf="/transports/http/configuration" 
        xsi:schemaLocation=" 
                /schema/beans
                /schema/beans/spring-beans-2.
                /core /schemas/
                /configuration/security
                /schemas/configuration/
                /transports/http/configuration
                /schemas/configuration/"> 
        <http-conf:conduit name="*.http-conduit"> 
            <http-conf:proxyAuthorization>
            
                    <!-- 用户名 -->
                    <sec:UserName>***</sec:UserName>
                    <!-- 密码 -->
                    <sec:Password>***</sec:Password>
                    
             </http-conf:proxyAuthorization>
            <!--
                ProxyServer IP                ProxyServerPort PORT
                ProxyServerType: HTTP or SOCKS
             -->
            <http-conf:client
                    ProxyServer="192.168.1.4" 
                    ProxyServerPort="808"
                    ProxyServerType="HTTP"
                    Connection="Keep-Alive" 
                    AllowChunking="false"
                    ConnectionTimeout="50000" 
                    ReceiveTimeout="120000"
                    /> 
    </http-conf:conduit> 

    <!-- Client   -->
    </beans>

<http-conf:conduit name="*.http-conduit"这里的name为”*.http-conduit”时,将会对所有的client类启用这个代理,如果要配置某个client类使用代理,可以这么写

<http-conf:conduit name="{http://widgets/}-conduit>
    ...
  </http-conf:conduit>

{}里面的内容是webservice的wsdl的<wsdl:definitions name="serviceName" targetNamespace=""的targetNamespace属性

{}之后.之前的内容是

<wsdl:port name="BasicHttpBinding_IService" binding="i0:BasicHttpBinding_IService">

<soap:address location="the address" />

</wsdl:port>

的name属性。

上述wsdl对应的配置是

<http-conf:conduit name="{}BasicHttpBinding_IService.http-conduit> ... </http-conf:conduit>

参考资料

/docs/framework/2.2/bind_trans/

/u/20090331/09/

/http:

/mod_mbox/servicemix-users//