`

Spring+CXF配置HTTP代理访问Internet

阅读更多
转:http://www.blogjava.net/Hafeyang/archive/2010/01/12/309178.html
Spring+CXF配置HTTP代理访问Internet

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

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

使用JaxWsProxyFactoryBean创建client

System.setProperty("http.proxySet", "true");
        System.setProperty("http.proxyHost", "IP");
        System.setProperty("http.proxyPort", "PORT");
        
        JaxWsProxyFactoryBean f = new JaxWsProxyFactoryBean();
        f.setAddress("THE URL OF WEBSERVICE");
        f.setServiceClass(IService.class);
        IService client = (IService) f.create();

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

又在网上找了很多资料,终于找到了一个配置的方法。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:cxf="http://cxf.apache.org/core" 
        xmlns:sec="http://cxf.apache.org/configuration/security" 
        xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" 
        xsi:schemaLocation=" 
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
                http://cxf.apache.org/configuration/security
                http://cxf.apache.org/schemas/configuration/security.xsd
                http://cxf.apache.org/transports/http/configuration
                http://cxf.apache.org/schemas/configuration/http-conf.xsd"> 
        <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/widgetvendor.net}widgetSOAPPort.http-conduit>
    ...
  </http-conf:conduit>

{}里面的内容是webservice的wsdl的<wsdl:definitions name="serviceName" targetNamespace="http://hafeyang.blogjava.net"的targetNamespace属性

{}之后.之前的内容是

…

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

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

</wsdl:port>

…

的name属性。

上述wsdl对应的配置是

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

参考资料

http://fusesource.com/docs/framework/2.2/bind_trans/HTTPConsumerConfig.html

http://topic.csdn.net/u/20090331/09/f3ce02e0-29be-40cc-a534-9f2e5ac23f9e.html

http://old.nabble.com/http:conduit-Proxy-td20462360.html

http://mail-archives.apache.org/mod_mbox/servicemix-users/200807.mbox/<4885FCBB.5070506@gmail.com>
分享到:
评论
2 楼 Kevin_jiang2011 2012-06-25  
能直接在,代码里面配置吗?
1 楼 ben_liang 2010-11-25  
#  <http-conf:proxyAuthorization> 
#              
#                     <!-- 用户名 --> 
#                     <sec:UserName>***</sec:UserName> 
#                     <!-- 密码 --> 
#                     <sec:Password>***</sec:Password> 
#                      
#              </http-conf:proxyAuthorization> 


访问代理不需要用户名,密码。那咋搞呢?

相关推荐

    Spring+cxf配置接口+mybatis配置

    在"Spring+cxf配置接口"的场景下,Spring会负责管理和调度CXF服务,提供服务的生命周期管理,而CXF则处理HTTP通信和Web服务协议。这种组合允许开发者在Spring容器中无缝地创建、管理和部署Web服务。 "mybatis配置...

    myBatis+spring+cxf 框架简单整合(包jar)

    整合过程中,开发者可能已经配置了Spring来管理CXF的服务端点,这样就可以利用Spring的依赖注入特性来轻松注入业务逻辑和数据访问层的组件。 在实际应用中,这样的整合有以下几个关键步骤: 1. 配置Spring:创建...

    idea+maven+spring+cxf

    idea+maven+spring+cxf

    Spring+CXF+MyBatis整合代码

    总结来说,"Spring+CXF+MyBatis"的整合代码实现了基于Web服务的数据访问,Spring作为整体框架,管理组件间的依赖;CXF提供了Web服务接口,使得服务对外暴露;而MyBatis则负责底层的数据库操作。这种整合模式在实际...

    spring+cxf 整合jar包

    在下载的"spring3+cxf2.7 整合jar包"中,可能包含了Spring 3.x版本和CXF 2.7版本的库文件,以及相关的示例配置文件,这些可以帮助开发者快速搭建和运行一个Spring+CXF的环境。使用时,开发者需要根据自己的项目需求...

    Spring+CXF+tomcat开发webservice

    2. 创建Spring配置文件,定义服务bean和CXF的配置。 3. 实现Web服务接口,并使用CXF的注解进行标记,如`@WebService`和`@SOAPBinding`。 4. 将服务部署到Tomcat服务器上,CXF会根据配置自动生成WSDL文件,这是服务的...

    idea + spring4.3.7.RELEASE+cxf3.1.0整合+客户端调用

    在"springmvc_cxf.rar"文件中,可能包含了已配置好的Spring MVC项目,包括Spring和CXF的配置文件、服务接口和实现、以及Maven的配置信息。"cxfclinet.rar"则可能包含了客户端的相关代码,如生成的代理类和调用示例。...

    使用Eclipse+Maven+Spring+CXF构建的WebService服务

    Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/

    Spring+cxf请求webService

    Spring可以通过配置文件或Java配置来整合CXF,实现Web服务客户端的创建和管理。 2. **CXF WebService客户端创建** 创建CXF客户端通常包括以下步骤: - 首先,获取Web服务的WSDL(Web Service Definition ...

    CXF2.1.3+spring3.0+struts2.3.4

    【标题】"CXF2.1.3+spring3.0+struts2.3.4" 描述了集成这三大框架实现Web服务的场景。CXF是一个开源的服务框架,它允许开发人员创建和消费各种Web服务。Spring是Java企业级应用的核心框架,提供了依赖注入和面向切面...

    spring+cxf 开发webservice

    【标题】"Spring+CXF 开发Web Service" 在Java世界中,开发Web服务的一个常见选择是使用Spring框架结合Apache CXF。Spring作为一个强大的轻量级框架,提供了丰富的功能,包括依赖注入、AOP(面向切面编程)以及企业...

    spring+cxf_demo

    2. **配置CXF**:在Spring配置文件中,我们需要配置CXF的Servlet或JAX-RS的Provider,以便处理HTTP请求。这可能包括设置服务地址、端点、数据绑定等。 3. **创建Web服务**:利用CXF提供的注解,如`@WebService`和`@...

    spring+cxf小demo

    【Spring+CXF小Demo】是基于Java开发的一个入门级示例,主要展示了如何结合Spring框架与CXF库来创建和消费Web服务。Spring是企业级应用开发的强大框架,而CXF是一个开源的服务栈,用于构建和部署Web服务。这个Demo...

    spring4+cxf3+maven3整合客户端与服务端

    7. **配置客户端**:在Spring配置文件中,配置CXF的JAXRSClientFactoryBean,用于生成客户端代理类。 8. **测试**:编写单元测试和集成测试,确保服务端和客户端的正常工作。 通过这个整合项目,开发者可以学习到...

    spring + cxf + mybatis整合

    然后在Spring配置文件中声明CXF的JAX-WS服务,指定服务接口、实现类和服务地址。CXF会根据这些信息自动暴露服务。 4. **Spring AOP集成**:Spring的AOP模块可以用于事务管理和日志记录。你可以定义一个事务切面,...

    基于spring+cxf实现用户文件传输的webservice

    基于spring+cxf实现用户文件传输的webservice 在本文中,我们将探讨如何使用Spring+CXF实现用户文件传输的Webservice。该Webservice提供了基本的报文上传和查询功能,同时还提供了用户身份验证功能。 Spring 和 ...

    spring+srpingmvc+mybatis+cxf

    在配置文件中,通常会看到Spring的applicationContext.xml用于配置Bean,SpringMVC的servlet-context.xml用于配置Web层,以及Mybatis的mybatis-config.xml和对应的Mapper文件。此外,CXF的相关配置可能在wsdd或cxf....

    maven+spring+cxf+mybatis

    maven+spring+cxf+mybatis 搭建的webservice项目

    spring+mybatis+cxf整合

    在Spring的配置文件中,需要添加MyBatis的数据源、SqlSessionFactory、MapperScannerConfigurer等配置,以及CXF的WebService服务端点和客户端代理等配置。 2. 整合MyBatis:将MyBatis的Mapper接口和XML配置文件集成...

    spring+cxf的webservice

    标题中的“spring+cxf的webservice”指的是使用Spring框架与Apache CXF库共同构建Web服务的过程。Apache CXF是一个开源的Java框架,主要用于创建和消费Web服务,它支持SOAP、RESTful等多种通信协议。Spring框架则是...

Global site tag (gtag.js) - Google Analytics