java - camel 代理 WS-Security webservice
伊谢尔伦
伊谢尔伦 2017-04-17 16:11:14
[Java讨论组]
  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的配置




    
    
    

    
    
    
    
     
          
              
                  
                  
                 
                 
              
          
      
     
     
         
             
                 
                 
                 
                 
             
         
        
    
    
    
    

       
       
    
       
     
               
    
    
    
                   
        
            
            
        
        
             
        
     
                       

      
        com.lucky  
    
  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 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 soapHeaders = CastUtils.cast((List) exchange.getIn().getHeader(Header.HEADER_LIST));
        SoapHeader newHeader;
        if(soapHeaders == null){
            soapHeaders = new ArrayList();
        }
        
        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 = ""
                    + login
                    + ""
                    + password + "";
        //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") ;
//    }
}


                            
                                    
            
            
            
            
            
            
            
            
            
            
            
            
            
            

            
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(2)
天蓬老师

无需特殊配置。

巴扎黑

answer...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号