CXF添加攔截器和自定義攔截器


 

  前面講了如何采用CXF開發webservice,現在來講如何添加攔截器和自定義攔截器。

  服務端代碼:

     HelloWorld implementor=new HelloWorldImpl();
        String address="http://192.xxx.15.117:8089/helloWorld";
        // Endpoint.publish(address, implementor); // jdk實現 暴露webservice接口
        JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
        factoryBean.setAddress(address); // 設置暴露地址
        factoryBean.setServiceClass(HelloWorld.class); // 接口類
        factoryBean.setServiceBean(implementor); // 設置實現類
      factoryBean.getInInterceptors().add(new LoggingInInterceptor()); // 添加in攔截器 日志攔截器
      factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); // 添加out攔截器 日志攔截器
        factoryBean.getInInterceptors().add(new MyInterceptor()); //自定義攔截器
        factoryBean.create(); // 創建webservice接口

  自定義攔截器類:

public class MyInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    public MyInterceptor() {
        super(Phase.PRE_INVOKE);  // 在調用方法之前調用自定攔截器
        
    }

    @SuppressWarnings("null")
    public void handleMessage(SoapMessage message) throws Fault {
        List<Header> headers=message.getHeaders();
        if(headers==null && headers.size()==0){
            throw new Fault(new IllegalArgumentException("沒有Header,攔截器實施攔截"));
        }
        Header firstHeader=headers.get(0);
        Element ele=(Element) firstHeader.getObject();
        NodeList uList=ele.getElementsByTagName("userName");
        NodeList pList=ele.getElementsByTagName("password");
        if(uList.getLength()!=1){
            throw new Fault(new IllegalArgumentException("用戶名格式不對"));
        }
        if(pList.getLength()!=1){
            throw new Fault(new IllegalArgumentException("密碼格式不對"));
        }
        String userName=uList.item(0).getTextContent();
        String password=pList.item(0).getTextContent();
        
        if(!userName.equals("java1234")||!password.equals("123456")){
            throw new Fault(new IllegalArgumentException("用戶名或者密碼錯誤!"));
        }
    }

}

  客戶端代碼:

  先通過輸入命令: wsdl2java http://192.xxx.15.117:8089/helloWorld?wsdl 生成需要的代碼,談話編寫調用方法。

     HelloWorldImplService service=new HelloWorldImplService();
        HelloWorld helloWorld=service.getHelloWorldImplPort();
        org.apache.cxf.endpoint.Client client=ClientProxy.getClient(helloWorld);
        
        client.getOutInterceptors().add(new AddHeaderInterceptor("java1234","123")); // 添加自定義攔截器
        client.getInInterceptors().add(new LoggingInInterceptor()); // 添加In攔截器 日志攔截器
        client.getOutInterceptors().add(new LoggingOutInterceptor()); // 添加Out攔截器 日志攔截器

  自定義攔截器:

public class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    private String userName;
    private String password;
    
    public AddHeaderInterceptor(String userName,String password) {
        super(Phase.PREPARE_SEND); // 准備發送SOAP消息的時候調用攔截器
        this.userName=userName;
        this.password=password;
    }

    public void handleMessage(SoapMessage message) throws Fault {
        List<Header> headerList=message.getHeaders();
        
        Document doc=DOMUtils.createDocument();
        Element ele=doc.createElement("authHeader");
        Element uElement=doc.createElement("userName");
        uElement.setTextContent(userName);
        Element pElement=doc.createElement("password");
        pElement.setTextContent(password);
        
        ele.appendChild(uElement);
        ele.appendChild(pElement);
        
        headerList.add(new Header(new QName("java1234"),ele));
        
    }

}

完成!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM