首页 > camel 代理 WS-Security webservice

camel 代理 WS-Security webservice

  1. 场景

目前已经实现正常的 webservice(client) 到 camel 到 webservice(server)
但是经过安全认证的WS-Security webservice(client)就会有问题,在camel-cxf 代理
中想仅仅实现代理,不想对客户端发送来的soap有任何的修改,即使是加密的消息。
哪位大侠有这方面的经验给提提,或是有什么样类似的例子,不胜感激。

更多内容可以查看:http://stackoverflow.com/questions/33981341/camel-proxy-webservice-in-ws-security-without-input-password-and-name

  1. cxf 配置文件以及camel的配置

<?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://camel.apache.org/schema/cxf"
    xmlns:camel="http://camel.apache.org/schema/spring"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://camel.apache.org/schema/spring 
        http://camel.apache.org/schema/spring/camel-spring.xsd
        http://camel.apache.org/schema/cxf
        http://camel.apache.org/schema/cxf/camel-cxf.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" />

    <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
    <bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
    
    
    <bean id="wss4jInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> 
         <property name="properties"> 
             <map> 
                 <entry key="action" value="UsernameToken Timestamp"/> 
                 <entry key="passwordType" value="PasswordDigest"/> 
                 <entry key="ws-security.is-bsp-compliant" value="false"/>
                 <entry key="passwordCallbackClass" value="camel.UTPasswordCallback"/>
             </map> 
         </property> 
     </bean> 
     
     <bean id="wss4jOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
         <constructor-arg>
             <map>
                 <entry key="action" value="UsernameToken Timestamp"/>
                 <entry key="passwordType" value="PasswordText"/>
                 <entry key="user" value="Alice"/>
                 <entry key="passwordCallbackClass" value="camel.UTPasswordCallback"/>
             </map>
         </constructor-arg>
        </bean>
    
    <!--<bean id="enrichBean" class="com.lucky.TestServlet"/>-->
    
    <!--发布一个前置webservice
    <cxf:cxfEndpoint id="orderEndpoint"
        address="http://localhost:8999/CXF_HELLO_ObjectSpringService"
        serviceClass="com.lucky.IHello" />-->

    <!--利用wsdl生成服务端映射 all the param will find in wsdl-->   
    <!-- address is what this proxy service address -->   
    <!-- serviceName is the serviceName  of wsdl:service-->
    <!-- endpointName is the name of wsdl:port -->   
    <!-- wsdlURL is the real webservice wsdl --> 
    <!-- xmlns:s is the targetNamespace of the header definitions-->           
    <cxf:cxfEndpoint id="orderEndpoint"
                   address="http://localhost:8989/CXF_HELLO_ObjectSpringService/IHello"
                   serviceName="s:HelloImplService"
                   endpointName="s:HelloImplPort" 
                   wsdlURL="http://localhost:9000/CXF_HELLO_ObjectSpringService/IHello?wsdl"
                   xmlns:s="http://impl.server.cxf.banana.com/"/>
    
    <cxf:cxfEndpoint id="axisEndpoint"
                   address="http://localhost:8988/Aqweb/services/PersonalTaxServicePort"
                   serviceName="s:PersonalTaxService"
                   endpointName="s:PersonalTaxServicePort"
                   wsdlURL="http://localhost:9001/Aqweb/services/PersonalTaxServicePort?wsdl"
                   xmlns:s="http://hellking.webservices.com/">
                   
        <cxf:inInterceptors>
            <ref bean="loggingInInterceptor"/>
            <ref bean="wss4jInInterceptor"/>
        </cxf:inInterceptors>
        <cxf:outInterceptors>
             <ref bean="loggingOutInterceptor"/>
        </cxf:outInterceptors>
     </cxf:cxfEndpoint>
                       

    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">  
        <package>com.lucky</package>  
    </camelContext>
</beans>
  1. 路由创建代码

package com.lucky;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import utile.PropertiesUtil;
import com.bean.RoutBean;

import java.io.StringReader;
import javax.xml.namespace.QName;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.http.HttpMessage;
import org.apache.camel.util.CastUtils;
import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.headers.Header;
import org.apache.cxf.headers.Header.Direction;
import org.apache.cxf.helpers.DOMUtils;



public class RoutLoad extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        //Properties prop = new Properties();
        InputStream in = RoutLoad.class
                .getResourceAsStream("./../../routConfig.properties");
        PropertiesUtil prop = new PropertiesUtil(in);
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        String key = "";
        String old_key = "";
        String configName = "";
        RoutBean routBean = new RoutBean();
        List<Object> keyValue = prop.getKeyList();
        for (Iterator<?> it = keyValue.iterator(); it.hasNext();) {
            key = (String) it.next();
//            System.out.println(prop.get(key));
//            System.out.println("key:"+key);
//            System.out.println("old_key:"+old_key);
            if (!"".equals(old_key)
                    && !key.substring(0, 7).equals(old_key.substring(0, 7))) {
                createCxfEndpoint(routBean);
            } else {
                configName = key.split("\\.")[1];
                if ("address".equals(configName)) {
                    routBean.setAddress((String) prop.get(key));
                } else if ("wsdl".equals(configName)) {
                    routBean.setWsdl((String) prop.get(key));
                } else if ("endPointName".equals(configName)) {
                    routBean.setEndPointName((String) prop.get(key));
                }
                if(!"".equals(old_key)){
                    if(!key.substring(0, 7).equals(old_key.substring(0, 7)) || !it.hasNext()){
                        createCxfEndpoint(routBean);
                    }
                }
            }
            old_key = key;
        }
    }

    public void createCxfEndpoint(RoutBean routBean) {
        Endpoint cxfEndpoint = endpoint("cxf:" + routBean.getAddress() // serviceAddress
                + "?" 
                +"wsdlURL="+ routBean.getWsdl() // wsdl
                + "&" + "dataFormat=MESSAGE" // dataformat type
        ); 
        
        
        from("cxf:bean:" + routBean.getEndPointName() + "?dataFormat=MESSAGE")
        .to(cxfEndpoint);
        
        
//        from("cxf:bean:" + routBean.getEndPointName() + "?dataFormat=MESSAGE")
//        .to("log:input")
//        .process(new Processor(){
//            @Override
//            public void process(Exchange exchange) throws Exception {
//                addWSSESecurityHeader(exchange, "login","password");
//            }
//        })
//        .to(cxfEndpoint)
//        .to("log:output");
    }
    
    public void addSoapHeader(Exchange exchange,String soapHeader){

        List<SoapHeader> soapHeaders = CastUtils.cast((List<?>) exchange.getIn().getHeader(Header.HEADER_LIST));
        SoapHeader newHeader;
        if(soapHeaders == null){
            soapHeaders = new ArrayList<SoapHeader>();
        }
        
        try {
            newHeader = new SoapHeader(new QName("soapHeader"), DOMUtils.readXml(new StringReader(soapHeader)).getDocumentElement());
            newHeader.setDirection(Direction.DIRECTION_OUT);
            soapHeaders.add(newHeader);
            exchange.getIn().setHeader(Header.HEADER_LIST, soapHeaders);
        } catch (Exception e) {
            //log error
        }
    }

    public void addWSSESecurityHeader(Exchange exchange,String login,String password){

        String soapHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\""+ 
            " xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><wsse:UsernameToken wsu:Id=\"UsernameToken-50\"><wsse:Username>"
                    + login
                    + "</wsse:Username><wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">"
                    + password + "</wsse:Password></wsse:UsernameToken></wsse:Security>";
        //Add wsse security header to the exchange
        addSoapHeader(exchange, soapHeader);
        
//        Map ss = exchange.getIn().getHeaders();
//        System.out.println("============key=============");
//        for (Object v : ss.entrySet()) {
//           //System.out.println("value= " + ((Map.Entry)v).getKey());
//           System.out.println(((Map.Entry)v).getKey()+"= " + ss.get(((Map.Entry)v).getKey()) );
//        }
    }
    
//    public void configure() throws Exception {
//        from("endpointIn")
//        .process(new Processor(){
//            @Override
//            public void process(Exchange exchange) throws Exception {
//                addWSSESecurityHeader(exchange, "login","password");
//            }
//        })
//        .to("endointOut") ;
//    }
}

answer...


无需特殊配置。

【热门文章】
【热门文章】