CXF使用WSS4J实现WS-Security规范之使用用户名令牌

CXF使用WSS4J实现WS-Security规范之使用用户名令牌


这个示例是一个简单的UsernameToken,它仅仅包装明文用户名和密码.

pom.xml:
 

www.ishang123.com   复制内容到剪贴板 折叠 XML/HTML 代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0modelVersion>  
  4.     <groupId>test.cxfgroupId>  
  5.     <artifactId>java_first_spring_support1artifactId>  
  6.     <packaging>warpackaging>  
  7.     <name>java_first_spring_support1name>  
  8.     <version>0.0.1-SNAPSHOTversion>  
  9.     <description>CXF使用WSS4J实现WS-Security规范之使用用户名令牌description>  
  10.     <properties>  
  11.         <cxf.version>2.2.5cxf.version>  
  12.     properties>  
  13.     <build>  
  14.         <finalName>java_first_spring_support1finalName>  
  15.         <plugins>  
  16.             <plugin>  
  17.                 <artifactId>maven-compiler-pluginartifactId>  
  18.                 <configuration>  
  19.                     <source>1.6source>  
  20.                     <target>1.6target>  
  21.                     <encoding>UTF-8encoding>  
  22.                 configuration>  
  23.             plugin>  
  24.             <plugin>  
  25.                 <groupId>org.apache.maven.pluginsgroupId>  
  26.                 <artifactId>maven-resources-pluginartifactId>  
  27.                 <configuration>  
  28.                     <encoding>UTF-8encoding>  
  29.                 configuration>  
  30.             plugin>  
  31.         plugins>  
  32.     build>  
  33.     <dependencies>  
  34.         <dependency>  
  35.             <groupId>org.springframeworkgroupId>  
  36.             <artifactId>springartifactId>  
  37.             <version>2.5.6version>  
  38.         dependency>  
  39.         <dependency>  
  40.             <groupId>org.apache.cxfgroupId>  
  41.             <artifactId>cxf-rt-frontend-jaxwsartifactId>  
  42.             <version>${cxf.version}version>  
  43.         dependency>  
  44.         <dependency>  
  45.             <groupId>org.apache.cxfgroupId>  
  46.             <artifactId>cxf-rt-transports-httpartifactId>  
  47.             <version>${cxf.version}version>  
  48.         dependency>  
  49.         <dependency>  
  50.             <groupId>org.apache.cxfgroupId>  
  51.             <artifactId>cxf-rt-ws-securityartifactId>  
  52.             <version>${cxf.version}version>  
  53.         dependency>  
  54.         <dependency>  
  55.             <groupId>junitgroupId>  
  56.             <artifactId>junitartifactId>  
  57.             <version>4.8.1version>  
  58.             <scope>testscope>  
  59.         dependency>  
  60.     dependencies>  
  61. project>  

服务接口:

www.ishang123.com   复制内容到剪贴板 折叠 Java 代码
  1. package cxf.server;   
  2. import javax.jws.WebService;   
  3. @WebService  
  4. public interface HelloWorld {   
  5.     String sayHi(String name);   
  6. }  

接口实现:

www.ishang123.com   复制内容到剪贴板 折叠 Java 代码
  1. package cxf.server;   
  2. import javax.jws.WebService;   
  3. @WebService(endpointInterface="cxf.server.HelloWorld")   
  4. public class HelloWorldImpl implements HelloWorld {   
  5.     @Override  
  6.     public String sayHi(String name) {   
  7.            
  8.         System.out.println("server: say->" + name);   
  9.         return "say-->" + name;   
  10.     }   
  11. }  

服务端的spring配置:

www.ishang123.com   复制内容到剪贴板 折叠 XML/HTML 代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  4.     xsi:schemaLocation="   
  5.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  6.     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  7.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  8.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  9.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  10.     <bean id="serverPasswordCallback" class="cxf.server.ServerPasswordCallback" />  
  11.     <jaxws:endpoint id="helloworld" implementor="cxf.server.HelloWorldImpl"  
  12.         address="/helloworld">  
  13.         <jaxws:inInterceptors>  
  14.             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />  
  15.               
  16.               
  17.             <bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">  
  18.                 <constructor-arg>  
  19.                     <map>  
  20.                         <entry key="action" value="UsernameToken" />  
  21.                         <entry key="passwordType" value="PasswordText" />  
  22.                         <entry key="user" value="FHDServer" />  
  23.                         <entry key="passwordCallbackRef">  
  24.                             <ref bean="serverPasswordCallback" />  
  25.                         entry>  
  26.                     map>  
  27.                 constructor-arg>  
  28.             bean>  
  29.         jaxws:inInterceptors>  
  30.     jaxws:endpoint>  
  31.       
  32.               
  33.             <bean class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">  
  34.                 <constructor-arg>  
  35.                     <map>  
  36.                         <entry key="action" value="UsernameToken" />  
  37.                         <entry key="passwordType" value="PasswordText" />  
  38.                         <entry key="user" value="FHDClient" />  
  39.                         <entry key="passwordCallbackRef">  
  40.                             <ref bean="clientPasswordCallback" />  
  41.                         entry>  
  42.                     map>  
  43.                 constructor-arg>  
  44.             bean>  
  45.         jaxws:outInterceptors>  
  46.     jaxws:client>  
  47. beans>  

客户端的验证:

www.ishang123.com   复制内容到剪贴板 折叠 Java 代码
  1. package cxf.client;   
  2. import java.io.IOException;   
  3. import javax.security.auth.callback.Callback;   
  4. import javax.security.auth.callback.CallbackHandler;   
  5. import javax.security.auth.callback.UnsupportedCallbackException;   
  6. import org.apache.ws.security.WSPasswordCallback;   
  7. public class ClientPasswordCallback implements CallbackHandler {   
  8.     @Override  
  9.     public void handle(Callback[] callbacks) throws IOException,   
  10.             UnsupportedCallbackException {   
  11.         WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];   
  12.         pc.setPassword("888888");   
  13.         pc.setIdentifier("admin");   
  14.     }   
  15. }  

客户端调用代码:

www.ishang123.com   复制内容到剪贴板 折叠 XML/HTML 代码
  1. package cxf.client;   
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  3. import cxf.server.HelloWorld;   
  4. public final class Client {   
  5.     public static void main(String args[]) throws Exception {   
  6.            
  7.         ClassPathXmlApplicationContext context  = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});   
  8.         HelloWorld client = (HelloWorld)context.getBean("client");   
  9.         String str = client.sayHi("fuhaidong");   
  10.         System.out.println("client: " + str);   
  11.     }   
  12. }  

web.xml:

www.ishang123.com   复制内容到剪贴板 折叠 XML/HTML 代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.        
  7.     <display-name>java_first_spring_support1display-name>  
  8.        
  9.     <welcome-file-list>  
  10.         <welcome-file>index.jspwelcome-file>  
  11.     welcome-file-list>  
  12.        
  13.     <context-param>  
  14.         <param-name>contextConfigLocationparam-name>  
  15.         <param-value>classpath:beans.xmlparam-value>  
  16.     context-param>  
  17.     <listener>  
  18.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  19.     listener>  
  20.     <servlet>  
  21.         <servlet-name>CXFServletservlet-name>  
  22.         <servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>  
  23.         <load-on-startup>1load-on-startup>  
  24.     servlet>  
  25.     <servlet-mapping>  
  26.         <servlet-name>CXFServletservlet-name>  
  27.         <url-pattern>/service/*url-pattern>  
  28.     servlet-mapping>  
  29. web-app>  




转自:http://www.ishang123.com/jishubowen/java/2012-08-22/161.html


你可能感兴趣的:(CXF使用WSS4J实现WS-Security规范之使用用户名令牌)