CXF开通用户认证
程序员文章站
2022-07-13 13:53:44
...
第一部分:环境
1.系统版本:Fedora14
2.Eclipse版本:J2ee版3.5
3.jdk版本:jdk1.6
4.maven local repository:/var/javaproject/repo
5.tomcat 版本:1.6
6.tomcat 端口:9080
假设以上环境已经安装完毕,各软件能独立正确运行。
第二部分:配置eclipse工作区
在适当的地方建立一个cxftest目录,然后在此目录再建一个cxftest_build目录
在cxftest_build目录中建立pom.xml文件,输入一下内容:
<!-- sub pom.xml start--->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bruce.cxftest</groupId>
<artifactId>cxftest_build</artifactId>
<packaging>pom</packaging>
<name>cxf test Parent Project</name>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../cxftest_public_impl</module>
<module>../cxftest_public_intf</module>
<module>../cxftest_web</module>
<module>../cxftest_client</module>
</modules>
<properties>
<wss4j.version>1.6.4</wss4j.version>
<spring.version>3.0.2.RELEASE</spring.version>
<cxf.version>2.5.2</cxf.version>
<junit.version>4.8.1</junit.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>
<!-- sub pom.xml end --->
我的电脑最终目录结构为:
cxftest/
|/cxftest_build
|/cxftest_client
|/cxftest_public_impl
|/cxftest_public_intf
|/cxftest_web
并以 cxftest目录为基础建立eclipse 工作区
第二部分:开发公共接口项目
在eclipse中建立名为cxftest_public_intf的java工程,设置java源文件的目录为/src/main/java, 资源目录为/src/main/resorces
在cxftest_public_intf项目根目录建立名为pom.xml输入以下内容:
<!-- pom.xml start →
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.cxftest</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>cxftest_build</artifactId>
<relativePath>../cxftest_build/pom.xml</relativePath>
</parent>
<artifactId>cxftest_public_intf</artifactId>
<packaging>jar</packaging>
<name>cxf test web service interface Project</name>
<build>
<finalName>${artifactId}-${version}</finalName>
<defaultGoal>process-resources</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/service.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
</project>
<!-- pom.xml end -->
用eclipse自带的工具src/main/wsdl 目录编辑wsdl文件 service.wsdl与dto.xsd:
<!-- service.wsdl start -->
<?xml version="1.0" ?>
<wsdl:definitions targetNamespace="http://www.bruce.com/cxftest/service"
xmlns:service="http://www.bruce.com/cxftest/service"
xmlns:dto="http://www.bruce.com/cxftest/dto"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.bruce.com/cxftest/dto" schemaLocation="dto.xsd">
</xsd:import>
</xsd:schema>
</wsdl:types>
<wsdl:message name="GetPhoneRequest">
<wsdl:part name="name" element="dto:getPhoneRequest" />
</wsdl:message>
<wsdl:message name="GetPhoneResponse">
<wsdl:part name="phone" element="dto:getPhoneResponse" />
</wsdl:message>
<wsdl:portType name="AddressBookService">
<wsdl:operation name="getPhone">
<wsdl:input message="service:GetPhoneRequest" />
<wsdl:output message="service:GetPhoneResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AddressBook" type="service:AddressBookService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getPhone">
<soap:operation
soapAction="http://www.bruce.com/cxftest/service/getPhone" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AddressBookService">
<wsdl:port name="AddressBookService" binding="service:AddressBook">
<soap:address location="http://www.bruce.com/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
<!-- service.wsdl end -->
<!-- dto.xsd start -->
<?xml version="1.0" ?>
<xsd:schema targetNamespace="http://www.bruce.com/cxftest/dto" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dto="http://www.bruce.com/cxftest/dto">
<xsd:complexType name="Phone">
<xsd:sequence>
<xsd:element name="areaCode" type="xsd:int" />
<xsd:element name="exchange" type="xsd:int" />
<xsd:element name="number" type="xsd:int" />
<xsd:element name="date" type="xsd:date"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="getPhoneRequest" type="xsd:string"></xsd:element>
<xsd:element name="getPhoneResponse" type="dto:Phone"></xsd:element>
</xsd:schema>
<!-- dto.xsd end -->
在目录cxftest/cxftest_public_intf/ 下运行:
#mvn install
以生成Webservice的接口与Dto类。
第三部分:开发公共实现项目
在eclipse中建立名为cxftest_public_impl的java工程,设置java源文件的目录为/src/main/java, 资源目录为/src/main/resorces
在cxftest_public_impl项目根目录建立名为pom.xml输入以下内容:
<!-- pom.xml start →
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.cxftest</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>cxftest_build</artifactId>
<relativePath>../cxftest_build/pom.xml</relativePath>
</parent>
<artifactId>cxftest_public_impl</artifactId>
<packaging>jar</packaging>
<name>cxf test web service implement Project</name>
<build>
<finalName>${artifactId}-${version}</finalName>
<defaultGoal>process-resources</defaultGoal>
<plugins>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.bruce.cxftest</groupId>
<version>${version}</version>
<artifactId>cxftest_public_intf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ws.security</groupId>
<artifactId>wss4j</artifactId>
<version>${wss4j.version}</version>
</dependency>
</dependencies>
</project>
<!-- pom.xml end -->
建立 服务实现类com.bruce.cxftest.service.AddressBookServiceImpl,输入以下内容(这个类应该也有相应的工具生成,但是我还没找到):
//**************AddressBookServiceImpl start ********************
package com.bruce.cxftest.service;
import com.bruce.cxftest.dto.Phone;
public class AddressBookServiceImpl implements AddressBookService {
@Override
public Phone getPhone(String name) {
Phone phone;
System.out.println(name);
phone = new Phone();
phone.setAreaCode(120);
phone.setExchange(10);
phone.setNumber(10);
return phone;
}
}
//**************AddressBookServiceImpl end ********************
建立 服务端用户回调类类com.bruce.cxftest.security . ServerPasswordCallback,输入以下内容(这个类应该也有相应的工具生成,但是我还没找到):
//**************ServerPasswordCallback start ********************
package com.bruce.cxftest.security;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class ServerPasswordCallback implements CallbackHandler {
Map<String,String> userMap = new HashMap<String,String>();
public ServerPasswordCallback(){
userMap.put("joe", "password");
}
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
if (userMap.containsKey(pc.getIdentifier())) {
pc.setPassword(userMap.get(pc.getIdentifier()));
}
}
}
第四部分:开发公共WEB服务项目
在eclipse中建立名为cxftest_web的java工程,设置java源文件的目录为/src/main/java, 资源文件目录为 /src/main/resources.
目录结构为:
cxftest_web
/src
| /main
| /java
| /resources
/WebContent
/WEB-INF
/classes
/lib
在cxftest_web项目根目录建立名为pom.输入以下内容:
<!-- pom.xml start -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.cxftest</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>cxftest_build</artifactId>
<relativePath>../cxftest_build/pom.xml</relativePath>
</parent>
<artifactId>cxftest_web</artifactId>
<packaging>war</packaging>
<name>cxf test web Project</name>
<build>
<finalName>${artifactId}-${version}</finalName>
<defaultGoal>process-resources</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.bruce.cxftest</groupId>
<version>${version}</version>
<artifactId>cxftest_public_intf</artifactId>
</dependency>
<dependency>
<groupId>com.bruce.cxftest</groupId>
<version>${version}</version>
<artifactId>cxftest_public_impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-common-utilities</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
</project>
<!-- pom.xml end -->
建立Service配置文件 /src/main/resources/applicationContext-server.xml,输入以下内容:
<!--******* applicationContext-server.xml start ************* -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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-servlet.xml" />
<bean id="addressBookServiceImpl" class="com.bruce.cxftest.service.AddressBookServiceImpl" />
<bean id="myPasswordCallback" class="com.bruce.cxftest.security.ServerPasswordCallback" />
<bean id="sAAJInInterceptor" class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" />
<bean id="wSS4JInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordDigest" />
<entry key="passwordCallbackRef">
<ref bean="myPasswordCallback" />
</entry>
</map>
</constructor-arg>
</bean>
<jaxws:endpoint id="addressBookService" implementor="#addressBookServiceImpl"
address="/AddressBookService">
<jaxws:inInterceptors>
<ref bean="sAAJInInterceptor" />
<ref bean="wSS4JInInterceptor" />
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>
<!--******** applicationContext-server.xml end ************ -->
5 在目录/WebContent/WEB-INF 下创建名为 web.xml 的文件,输入以下内容:
<!-- web.xml **********start -->
<?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">
<display-name>xfireserver</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext-server.xml
classpath:META-INF/cxf/cxf-extension-policy.xml
classpath:META-INF/cxf/cxf-extension-ws-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
<!-- web.xml **********end -->
第五部分:开发webservice服务客户端
在eclipse中建立名为cxftest_client的java工程,设置java源文件的目录为/src/main/java, 资源目录为/src/main/resources.
目录结构为:
cxftest_client
/src
/main
/java
/resources
在cxftest_client项目根目录建立名为pom.输入以下内容:
<!-- pom.xml start -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.cxftest</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>cxftest_build</artifactId>
<relativePath>../cxftest_build/pom.xml</relativePath>
</parent>
<artifactId>cxftest_client</artifactId>
<packaging>jar</packaging>
<name>cxf test client Project</name>
<build>
<finalName>${artifactId}-${version}</finalName>
<defaultGoal>process-resources</defaultGoal>
<plugins>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.bruce.cxftest</groupId>
<version>${version}</version>
<artifactId>cxftest_public_intf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ws.security</groupId>
<artifactId>wss4j</artifactId>
<version>${wss4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
</project>
<!-- pom.xml end -->
建立 服务测试类SpringUsersWsClient.java,输入以下内容
//************** SpringUsersWsClient start ********************
package com.bruce.cxftest.client;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bruce.cxftest.dto.Phone;
import com.bruce.cxftest.service.AddressBookService;
public class SpringUsersWsClient {
public static void main(String[] args) {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
AddressBookService service = ctx.getBean("addressBookClient", AddressBookService.class);
System.out.println("#############Client getPhone##############");
Phone phone = service.getPhone("zph");
System.out.println("AreaCode:" + phone.getAreaCode());
System.out.println("Exchange:" + phone.getExchange());
System.out.println("Number:" + phone.getNumber());
} catch (BeansException e) {
e.printStackTrace();
}
}
}
//**************SpringUsersWsClient end ********************
建立 用户认证回调类ServerPasswordCallback.java,输入以下内容
//************** ServerPasswordCallback start ********************
package com.bruce.cxftest.security;
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class ServerPasswordCallback implements CallbackHandler {
String userName = "joe";
String password = "password";
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
pc.setPassword(password);
pc.setIdentifier(userName);
}
}
//**************ServerPasswordCallback end ********************
建立Client Service配置文件 /src/main/resources/ applicationContext-client.xml ,输入以下内容:
<!--******* applicationContext-client.xml start ************* -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="myPasswordCallback" class="com.bruce.cxftest.security.ServerPasswordCallback" />
<bean id="wSS4JOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordDigest" />
<entry key="user" value="joe" />
<entry key="passwordCallbackRef">
<ref bean="myPasswordCallback" />
</entry>
</map>
</constructor-arg>
</bean>
<jaxws:client id="addressBookClient"
serviceClass="com.bruce.cxftest.service.AddressBookService"
address="http://127.0.0.1:9080/cxftest/service/AddressBookService">
<jaxws:outInterceptors>
<ref bean="wSS4JOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
</beans>
<!--******** applicationContext-client.xml end ************ -->
第六部分:调试运行
1.打开命令行转至cxftest_build目录,运行如下命令
$mvn clean install
$mvn eclipse:eclipse
2.将打好的Wea包拷贝至Tomcat webapps 目录下的 cxftest 目录中。
3.启动Tomcat
4.启动cxftest_client工程里的 SpringUsersWsClient 类:输出如下信息:
AreaCode:120
Exchange:10
Number:10
至此表明Webservice调用成功!
1.系统版本:Fedora14
2.Eclipse版本:J2ee版3.5
3.jdk版本:jdk1.6
4.maven local repository:/var/javaproject/repo
5.tomcat 版本:1.6
6.tomcat 端口:9080
假设以上环境已经安装完毕,各软件能独立正确运行。
第二部分:配置eclipse工作区
在适当的地方建立一个cxftest目录,然后在此目录再建一个cxftest_build目录
在cxftest_build目录中建立pom.xml文件,输入一下内容:
<!-- sub pom.xml start--->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bruce.cxftest</groupId>
<artifactId>cxftest_build</artifactId>
<packaging>pom</packaging>
<name>cxf test Parent Project</name>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../cxftest_public_impl</module>
<module>../cxftest_public_intf</module>
<module>../cxftest_web</module>
<module>../cxftest_client</module>
</modules>
<properties>
<wss4j.version>1.6.4</wss4j.version>
<spring.version>3.0.2.RELEASE</spring.version>
<cxf.version>2.5.2</cxf.version>
<junit.version>4.8.1</junit.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>
<!-- sub pom.xml end --->
我的电脑最终目录结构为:
cxftest/
|/cxftest_build
|/cxftest_client
|/cxftest_public_impl
|/cxftest_public_intf
|/cxftest_web
并以 cxftest目录为基础建立eclipse 工作区
第二部分:开发公共接口项目
在eclipse中建立名为cxftest_public_intf的java工程,设置java源文件的目录为/src/main/java, 资源目录为/src/main/resorces
在cxftest_public_intf项目根目录建立名为pom.xml输入以下内容:
<!-- pom.xml start →
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.cxftest</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>cxftest_build</artifactId>
<relativePath>../cxftest_build/pom.xml</relativePath>
</parent>
<artifactId>cxftest_public_intf</artifactId>
<packaging>jar</packaging>
<name>cxf test web service interface Project</name>
<build>
<finalName>${artifactId}-${version}</finalName>
<defaultGoal>process-resources</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/service.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
</project>
<!-- pom.xml end -->
用eclipse自带的工具src/main/wsdl 目录编辑wsdl文件 service.wsdl与dto.xsd:
<!-- service.wsdl start -->
<?xml version="1.0" ?>
<wsdl:definitions targetNamespace="http://www.bruce.com/cxftest/service"
xmlns:service="http://www.bruce.com/cxftest/service"
xmlns:dto="http://www.bruce.com/cxftest/dto"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.bruce.com/cxftest/dto" schemaLocation="dto.xsd">
</xsd:import>
</xsd:schema>
</wsdl:types>
<wsdl:message name="GetPhoneRequest">
<wsdl:part name="name" element="dto:getPhoneRequest" />
</wsdl:message>
<wsdl:message name="GetPhoneResponse">
<wsdl:part name="phone" element="dto:getPhoneResponse" />
</wsdl:message>
<wsdl:portType name="AddressBookService">
<wsdl:operation name="getPhone">
<wsdl:input message="service:GetPhoneRequest" />
<wsdl:output message="service:GetPhoneResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AddressBook" type="service:AddressBookService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getPhone">
<soap:operation
soapAction="http://www.bruce.com/cxftest/service/getPhone" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AddressBookService">
<wsdl:port name="AddressBookService" binding="service:AddressBook">
<soap:address location="http://www.bruce.com/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
<!-- service.wsdl end -->
<!-- dto.xsd start -->
<?xml version="1.0" ?>
<xsd:schema targetNamespace="http://www.bruce.com/cxftest/dto" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dto="http://www.bruce.com/cxftest/dto">
<xsd:complexType name="Phone">
<xsd:sequence>
<xsd:element name="areaCode" type="xsd:int" />
<xsd:element name="exchange" type="xsd:int" />
<xsd:element name="number" type="xsd:int" />
<xsd:element name="date" type="xsd:date"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="getPhoneRequest" type="xsd:string"></xsd:element>
<xsd:element name="getPhoneResponse" type="dto:Phone"></xsd:element>
</xsd:schema>
<!-- dto.xsd end -->
在目录cxftest/cxftest_public_intf/ 下运行:
#mvn install
以生成Webservice的接口与Dto类。
第三部分:开发公共实现项目
在eclipse中建立名为cxftest_public_impl的java工程,设置java源文件的目录为/src/main/java, 资源目录为/src/main/resorces
在cxftest_public_impl项目根目录建立名为pom.xml输入以下内容:
<!-- pom.xml start →
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.cxftest</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>cxftest_build</artifactId>
<relativePath>../cxftest_build/pom.xml</relativePath>
</parent>
<artifactId>cxftest_public_impl</artifactId>
<packaging>jar</packaging>
<name>cxf test web service implement Project</name>
<build>
<finalName>${artifactId}-${version}</finalName>
<defaultGoal>process-resources</defaultGoal>
<plugins>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.bruce.cxftest</groupId>
<version>${version}</version>
<artifactId>cxftest_public_intf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ws.security</groupId>
<artifactId>wss4j</artifactId>
<version>${wss4j.version}</version>
</dependency>
</dependencies>
</project>
<!-- pom.xml end -->
建立 服务实现类com.bruce.cxftest.service.AddressBookServiceImpl,输入以下内容(这个类应该也有相应的工具生成,但是我还没找到):
//**************AddressBookServiceImpl start ********************
package com.bruce.cxftest.service;
import com.bruce.cxftest.dto.Phone;
public class AddressBookServiceImpl implements AddressBookService {
@Override
public Phone getPhone(String name) {
Phone phone;
System.out.println(name);
phone = new Phone();
phone.setAreaCode(120);
phone.setExchange(10);
phone.setNumber(10);
return phone;
}
}
//**************AddressBookServiceImpl end ********************
建立 服务端用户回调类类com.bruce.cxftest.security . ServerPasswordCallback,输入以下内容(这个类应该也有相应的工具生成,但是我还没找到):
//**************ServerPasswordCallback start ********************
package com.bruce.cxftest.security;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class ServerPasswordCallback implements CallbackHandler {
Map<String,String> userMap = new HashMap<String,String>();
public ServerPasswordCallback(){
userMap.put("joe", "password");
}
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
if (userMap.containsKey(pc.getIdentifier())) {
pc.setPassword(userMap.get(pc.getIdentifier()));
}
}
}
第四部分:开发公共WEB服务项目
在eclipse中建立名为cxftest_web的java工程,设置java源文件的目录为/src/main/java, 资源文件目录为 /src/main/resources.
目录结构为:
cxftest_web
/src
| /main
| /java
| /resources
/WebContent
/WEB-INF
/classes
/lib
在cxftest_web项目根目录建立名为pom.输入以下内容:
<!-- pom.xml start -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.cxftest</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>cxftest_build</artifactId>
<relativePath>../cxftest_build/pom.xml</relativePath>
</parent>
<artifactId>cxftest_web</artifactId>
<packaging>war</packaging>
<name>cxf test web Project</name>
<build>
<finalName>${artifactId}-${version}</finalName>
<defaultGoal>process-resources</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.bruce.cxftest</groupId>
<version>${version}</version>
<artifactId>cxftest_public_intf</artifactId>
</dependency>
<dependency>
<groupId>com.bruce.cxftest</groupId>
<version>${version}</version>
<artifactId>cxftest_public_impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-common-utilities</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
</project>
<!-- pom.xml end -->
建立Service配置文件 /src/main/resources/applicationContext-server.xml,输入以下内容:
<!--******* applicationContext-server.xml start ************* -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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-servlet.xml" />
<bean id="addressBookServiceImpl" class="com.bruce.cxftest.service.AddressBookServiceImpl" />
<bean id="myPasswordCallback" class="com.bruce.cxftest.security.ServerPasswordCallback" />
<bean id="sAAJInInterceptor" class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" />
<bean id="wSS4JInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordDigest" />
<entry key="passwordCallbackRef">
<ref bean="myPasswordCallback" />
</entry>
</map>
</constructor-arg>
</bean>
<jaxws:endpoint id="addressBookService" implementor="#addressBookServiceImpl"
address="/AddressBookService">
<jaxws:inInterceptors>
<ref bean="sAAJInInterceptor" />
<ref bean="wSS4JInInterceptor" />
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>
<!--******** applicationContext-server.xml end ************ -->
5 在目录/WebContent/WEB-INF 下创建名为 web.xml 的文件,输入以下内容:
<!-- web.xml **********start -->
<?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">
<display-name>xfireserver</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext-server.xml
classpath:META-INF/cxf/cxf-extension-policy.xml
classpath:META-INF/cxf/cxf-extension-ws-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
<!-- web.xml **********end -->
第五部分:开发webservice服务客户端
在eclipse中建立名为cxftest_client的java工程,设置java源文件的目录为/src/main/java, 资源目录为/src/main/resources.
目录结构为:
cxftest_client
/src
/main
/java
/resources
在cxftest_client项目根目录建立名为pom.输入以下内容:
<!-- pom.xml start -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.cxftest</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>cxftest_build</artifactId>
<relativePath>../cxftest_build/pom.xml</relativePath>
</parent>
<artifactId>cxftest_client</artifactId>
<packaging>jar</packaging>
<name>cxf test client Project</name>
<build>
<finalName>${artifactId}-${version}</finalName>
<defaultGoal>process-resources</defaultGoal>
<plugins>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.bruce.cxftest</groupId>
<version>${version}</version>
<artifactId>cxftest_public_intf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ws.security</groupId>
<artifactId>wss4j</artifactId>
<version>${wss4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
</project>
<!-- pom.xml end -->
建立 服务测试类SpringUsersWsClient.java,输入以下内容
//************** SpringUsersWsClient start ********************
package com.bruce.cxftest.client;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bruce.cxftest.dto.Phone;
import com.bruce.cxftest.service.AddressBookService;
public class SpringUsersWsClient {
public static void main(String[] args) {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
AddressBookService service = ctx.getBean("addressBookClient", AddressBookService.class);
System.out.println("#############Client getPhone##############");
Phone phone = service.getPhone("zph");
System.out.println("AreaCode:" + phone.getAreaCode());
System.out.println("Exchange:" + phone.getExchange());
System.out.println("Number:" + phone.getNumber());
} catch (BeansException e) {
e.printStackTrace();
}
}
}
//**************SpringUsersWsClient end ********************
建立 用户认证回调类ServerPasswordCallback.java,输入以下内容
//************** ServerPasswordCallback start ********************
package com.bruce.cxftest.security;
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class ServerPasswordCallback implements CallbackHandler {
String userName = "joe";
String password = "password";
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
pc.setPassword(password);
pc.setIdentifier(userName);
}
}
//**************ServerPasswordCallback end ********************
建立Client Service配置文件 /src/main/resources/ applicationContext-client.xml ,输入以下内容:
<!--******* applicationContext-client.xml start ************* -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="myPasswordCallback" class="com.bruce.cxftest.security.ServerPasswordCallback" />
<bean id="wSS4JOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordDigest" />
<entry key="user" value="joe" />
<entry key="passwordCallbackRef">
<ref bean="myPasswordCallback" />
</entry>
</map>
</constructor-arg>
</bean>
<jaxws:client id="addressBookClient"
serviceClass="com.bruce.cxftest.service.AddressBookService"
address="http://127.0.0.1:9080/cxftest/service/AddressBookService">
<jaxws:outInterceptors>
<ref bean="wSS4JOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
</beans>
<!--******** applicationContext-client.xml end ************ -->
第六部分:调试运行
1.打开命令行转至cxftest_build目录,运行如下命令
$mvn clean install
$mvn eclipse:eclipse
2.将打好的Wea包拷贝至Tomcat webapps 目录下的 cxftest 目录中。
3.启动Tomcat
4.启动cxftest_client工程里的 SpringUsersWsClient 类:输出如下信息:
AreaCode:120
Exchange:10
Number:10
至此表明Webservice调用成功!
上一篇: 软件调用时间数量级
下一篇: window10上编译openjdk9