CXF公開WebService-jaxws:endpoint

8051 ワード

#依存jar
commons-logging-1.1.1.jar cxf-2.7.5.jar httpasyncclient-4.0-beta3.jar httpclient-4.2.1.jar httpcore-4.2.2.jar httpcore-nio-4.2.2.jar neethi-3.0.2.jar org.springframework.aop-3.1.3.RELEASE.jar org.springframework.asm-3.1.3.RELEASE.jar org.springframework.beans-3.1.3.RELEASE.jar org.springframework.context-3.1.3.RELEASE.jar org.springframework.core-3.1.3.RELEASE.jar org.springframework.expression-3.1.3.RELEASE.jar org.springframework.web-3.1.3.RELEASE.jar org.springframework.web.servlet-3.1.3.RELEASE.jar stax2-api-3.1.1.jar woodstox-core-asl-4.2.0.jar wsdl4j-1.6.3.jar wss4j-1.6.10.jar xmlschema-core-2.0.3.jar xmlsec-1.5.4.jar
# download urlhttp://cxf.apache.org/download.htmlhttp://repo.spring.io/release/org/springframework/spring  http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html  
1.インタフェース
package org.demo.ws;

import javax.jws.WebService;

@WebService
public interface HelloWorld
{
    String sayHi(String text);
}
.実装クラス
package org.demo.ws.server;

import javax.jws.WebService;
import org.demo.ws.HelloWorld;

@WebService(serviceName="hello", endpointInterface="org.demo.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
    
    public HelloWorldImpl()
    {
        System.out.println("-- init HelloWorldImpl --");
    }
    
    public String sayHi(String text)
    {
        System.out.println("in sayHi, text : " + text);
        return "hi " + text + ".";
    }
}
3.サービス側ユーザー名パスワードコールバッククラス
package org.demo.ws.server;

import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.servlet.http.HttpServletRequest;
import javax.xml.stream.XMLStreamReader;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.apache.ws.security.WSPasswordCallback;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MyPasswordCallback implements CallbackHandler
{
    
    @Override
    public void handle(Callback[] callbacks) throws IOException,
            UnsupportedCallbackException
    {
        WSPasswordCallback pwCallback = (WSPasswordCallback)callbacks[0];
        String userName = pwCallback.getIdentifier();
        
        // 1. get client ip
        SoapMessage msg = (SoapMessage)pwCallback.getRequestData().getMsgContext();
        HttpServletRequest req = (HttpServletRequest)msg.getContextualProperty(AbstractHTTPDestination.HTTP_REQUEST);
        String clientIp = req.getRemoteAddr();
        
        // 2. get method name
        XMLStreamReader xmlReader = msg.getContent(XMLStreamReader.class);
        String methodName = xmlReader.getLocalName();
        
        // 3. get header
        Header header = msg.getHeaders().get(0);
        Element securityElement = (Element)header.getObject();
        String password = getFirstElementValue(securityElement, "Password");
        String nonce = getFirstElementValue(securityElement, "Nonce");
        String time = getFirstElementValue(securityElement, "Created");
        
        // 4. log
        System.out.println("clientIp=" + clientIp + ", methodName=" + methodName 
                + ", userName=" + userName + ", password=" + password + ", nonce=" + nonce + ", time=" + time);
        
        if ("user1".equals(userName))
        {
            pwCallback.setPassword("password1");
        }
    }
    
    /**
     *   tagName        XML    
     * @param current
     * @param tagName
     * @return
     */
    private String getFirstElementValue(Element current, String tagName)
    {
        String result = null;
        Element tmpElement = null;
        Element firstElement = null;
        
        NodeList nodeList = current.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (!(node instanceof Element)) {
                continue;
            }
            
            tmpElement = (Element)node;
            if (tmpElement.getLocalName().equals(tagName)) {
                result = tmpElement.getTextContent();
                break;
            }
            if (firstElement == null) {
                firstElement = tmpElement;
            }
        }
        
        if (result == null && firstElement != null) {
            result = getFirstElementValue(firstElement, tagName);
        }
        return result;
    }
}
4. web.xml



    
        contextConfigLocation
        classpath:spring.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
        cxf
        org.apache.cxf.transport.servlet.CXFServlet
        1
    
    
        cxf
        /services/*
    

    
        index.html
    
 
 5. spring.xml


    
    
    
    
    
    
    
    
        
            
                
                
                
                
                    
                
            
        
    
    
    
    
        
            
        
        
            
            
            
        
    
    

6.Webサービスへのアクセス
http://localhost:8080/context/services/hello?wsdl