CXF 发布WebService - jaxws:endpoint
# 依赖 jar commons-logging-1.1.1.jar # download url 1. 一个接口 package org.demo.ws; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(String text); }2. 一个实现类 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 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>?5. spring.xml <?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:context="http://www.springframework.org/schema/context" ? ? xmlns:jaxws="http://cxf.apache.org/jaxws" ? ? xsi:schemaLocation=" ? ? ? ?http://www.springframework.org/schema/beans ? ? ? ?http://www.springframework.org/schema/beans/spring-beans.xsd ? ? ? ?http://www.springframework.org/schema/context ? ? ? ?http://www.springframework.org/schema/context/spring-context.xsd ? ? ? ?http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" > ? ?? ? ? <!-- 隐式地向Spring容器注册了注解处理器 --> ? ? <context:annotation-config /> ? ?? ? ? <!-- CXF Web Service --> ? ? <import resource="classpath:META-INF/cxf/cxf.xml" /> ? ? <bean id="myPasswordCallback" class="org.demo.ws.server.MyPasswordCallback" /> ? ? <bean id="wSS4JInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> ? ? ? ? <constructor-arg> ? ? ? ? ? ? <map> ? ? ? ? ? ? ? ? <entry key="action" value="UsernameToken" /> ? ? ? ? ? ? ? ? <entry key="passwordType" value="PasswordText" /> ? ? ? ? ? ? ? ? <entry key="user" value="cxfServer" /> ? ? ? ? ? ? ? ? <entry key="passwordCallbackRef"> ? ? ? ? ? ? ? ? ? ? <ref bean="myPasswordCallback" /> ? ? ? ? ? ? ? ? </entry> ? ? ? ? ? ? </map> ? ? ? ? </constructor-arg> ? ? </bean> ? ?? ? ? <bean id="helloWorldImpl" class="org.demo.ws.server.HelloWorldImpl" /> ? ? <jaxws:endpoint id="helloWorld" address="/hello" implementor="#helloWorldImpl"> ? ? ? ? <jaxws:inInterceptors> ? ? ? ? ? ? <ref bean="wSS4JInInterceptor"/> ? ? ? ? </jaxws:inInterceptors> ? ? ? ? <jaxws:properties> ? ? ? ? ? ? <!-- the max size of soap message,default value is Long.MAX_VALUE --> ? ? ? ? ? ? <!-- refer to: http://cxf.apache.org/docs/security.html#Security-ControllingLargeRequestPayloads --> ? ? ? ? ? ? <entry key="org.apache.cxf.stax.maxXMLCharacters" value="10240" /> ? ? ? ? </jaxws:properties> ? ? </jaxws:endpoint> ? ?? </beans> 6. 访问 web service http://localhost:8080/context/services/hello?wsdl (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |