Before referring to this example, read the following two documents:
Http://blog.csdn.net/kunshan_shenbin/archive/2008/12/26/3613918.aspx
Http://blog.csdn.net/kunshan_shenbin/archive/2008/12/27/3621990.aspx
We use Apache wss4j, an open-source WS-Security implementation. For more information, see:
Http://ws.apache.org/wss4j/
Wss4j supports the following modes:
XML security
XML Signature
XML Encryption
Tokens
Username tokens
Timestamps
SAML tokens
Timestamps + encryption + signature will be used here.
First, you need to generate the server and client key file:
Generatekeypair. bat
Rem @ echo off <br/> echo alias % 1 <br/> echo keypass % 2 <br/> echo keystorename % 3 <br/> echo keystorepass % 4 <br/> echo keyname % 5 </P> <p> echo keyname % 5 <br/> keytool-genkey-alias % 1-keypass % 2-keystore % 3-storepass % 4-dname "cn = % 1"-keyalg RSA <br/> keytool-selfcert-alias % 1-keystore % 3-storepass % 4-keypass % 2 <br/> keytool-export- alias % 1-file % 5-keystore % 3-storepass % 4
Generateserverkey. bat
Call generatekeypair. bat apmserver apmserverpass serverstore. jks keystorepass serverkey. RSA <br/> call generatekeypair. bat apmclient apmclientpass clientstore. jks keystorepass clientkey. RSA <br/> keytool-import-alias apmserver-file serverkey. RSA-keystore clientstore. jks-storepass keystorepass-noprompt <br/> keytool-import-alias apmclient-file clientkey. RSA-keystore serverstore. jks-storepass keystorepass-noprompt
Run generateserverkey. BAT to generate the clientstore. jks and serverstore. jks files.
Information contained in the generated key file:
Server account: apmserver/apmserverpass
Client account: apmclient/apmclientpass
Create a project as shown in:
List of jar packages used
Passwordhandler. Java
Package COM. cecltd. security; </P> <p> Import Java. io. ioexception; <br/> Import Java. util. hashmap; <br/> Import Java. util. map; </P> <p> Import javax. security. auth. callback. callback; <br/> Import javax. security. auth. callback. callbackhandler; <br/> Import javax. security. auth. callback. unsupportedcallbackexception; </P> <p> Import Org. apache. WS. security. wspasswordcallback; </P> <p> public class passwordhandler implements callbackhandler {</P> <p> private Map <string, string> passwords = new hashmap <string, string> (); </P> <p> Public passwordhandler () {<br/> passwords. put ("apmserver", "apmserverpass"); <br/> passwords. put ("apmclient", "apmclientpass"); <br/>}</P> <p> Public void handle (callback [] callbacks) throws ioexception, <br/> unsupportedcallbackexception {<br/> wspasswordcallback Pc = (wspasswordcallback) callbacks [0]; <br/> string id = pc. getidentifer (); <br/> PC. setpassword (string) passwords. get (ID); <br/>}< br/>}
Sayhisrvcimpl. Java
Package COM. cecltd. service. impl; </P> <p> Import COM. cecltd. service. sayhisrvc; </P> <p> public class sayhisrvcimpl implements sayhisrvc {</P> <p> Public String sayhi (string username) {</P> <p> return "hi," + username + "! "; <Br/>}< br/>}
Sayhisrvc. Java
Package COM. cecltd. service; </P> <p> Import javax. JWS. webService; </P> <p> @ WebService <br/> Public interface sayhisrvc {</P> <p> Public String sayhi (string username); <br/>}
Server_insecurity_enc.properties
Org. apache. WS. security. crypto. provider = org. apache. WS. security. components. crypto. merlin <br/> Org. apache. WS. security. crypto. merlin. keystore. type = jks <br/> Org. apache. WS. security. crypto. merlin. keystore. password = keystorepass <br/> Org. apache. WS. security. crypto. merlin. alias. password = apmserverpass <br/> Org. apache. WS. security. crypto. merlin. keystore. alias = apmserver <br/> Org. apache. WS. security. crypto. merlin. file = serverstore. jks <br/>
Server_insecurity_sign.properties
Org. apache. WS. security. crypto. provider = org. apache. WS. security. components. crypto. merlin <br/> Org. apache. WS. security. crypto. merlin. keystore. type = jks <br/> Org. apache. WS. security. crypto. merlin. keystore. password = keystorepass <br/> # Org. apache. WS. security. crypto. merlin. alias. password = apmserverpass <br/> Org. apache. WS. security. crypto. merlin. keystore. alias = apmserver <br/> Org. apache. WS. security. crypto. merlin. file = serverstore. jks
Server_outsecurity_enc.properties
Org. apache. WS. security. crypto. provider = org. apache. WS. security. components. crypto. merlin <br/> Org. apache. WS. security. crypto. merlin. keystore. type = jks <br/> Org. apache. WS. security. crypto. merlin. keystore. password = keystorepass <br/> # Org. apache. WS. security. crypto. merlin. alias. password = apmserverpass <br/> # Org. apache. WS. security. crypto. merlin. keystore. alias = apmserver <br/> Org. apache. WS. security. crypto. merlin. file = serverstore. jks
Sayhiservicetest. Java
Package COM. service. test; </P> <p> Import static Org. JUnit. assert. *; <br/> Import Org. JUnit. beforeclass; <br/> Import Org. JUnit. test; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; </P> <p> Import COM. cecltd. service. sayhisrvc; </P> <p> public class sayhiservicetest {</P> <p> Private Static sayhisrvc; </P> <p> @ Be Foreclass <br/> Public static void setup () {</P> <p> applicationcontext context = new classpathxmlapplicationcontext (New String [] {"applicationcontext. XML "}); <br/> sayhisrvc = (sayhisrvc) context. getbean ("sayhisrvc"); <br/>}</P> <p> @ test <br/> Public void testsayhi () {</P> <p> assertequals ("Hi, shenbin! ", Sayhisrvc. sayhi (" shenbin "); <br/>}< br/>}
Applicationcontext. xml
<Beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: jaxws = "http://cxf.apache.org/jaxws" <br/> xsi: schemalocation = "<br/> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd <br/> http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> </P> <p> <bean id = "sayhisrvc" class = "com. cecltd. service. sayhisrvc "factory-bean =" sayhisrvcfactory "factory-method =" CREATE "/> </P> <p> <bean id =" sayhisrvcfactory "class =" org. apache. cxf. jaxws. jaxwsproxyfactorybean "> <br/> <property name =" serviceclass "value =" com. cecltd. service. sayhisrvc "/> <br/> <property name =" Address "value =" $ {Host. URL}/sayhisrvc "/> <br/> <property name =" outinterceptors "> <br/> <list> <br/> <Bean class =" org. apache. cxf. binding. soap. SAAJ. saajoutinterceptor "/> <br/> <ref bean =" wss4joutconfiguration "/> <br/> </List> <br/> </property> <br/> <property name = "ininterceptors"> <br/> <list> <br/> <Bean class = "org. apache. cxf. binding. soap. SAAJ. saajininterceptor "/> <br/> <ref bean =" wss4jinconfiguration "/> <br/> </List> <br/> </property> <br/> </Bean> </P> <p> <bean id = "propertyconfigurer" class = "org. springframework. beans. factory. config. propertyplaceholderconfigurer "> <br/> <property name =" locations "> <br/> <list> <br/> <value> serverhost. properties </value> <br/> </List> <br/> </property> <br/> </bean> </P> <p> <bean id = "wss4joutconfiguration" class = "org. apache. cxf. WS. security. wss4j. wss4joutinterceptor "> <br/> <property name =" properties "> <br/> <map> <br/> <Entry key =" action "value =" timestamp encrypt signature" /> <br/> <Entry key = "user" value = "apmclient"/> <br/> <Entry key = "encryptionuser" value = "apmserver"/> <br /> <Entry key = "signaturepropfile" value = "outsecurity_sign.properties"/> <br/> <Entry key = "signaturekeyidentifier" value = "issuerserial"/> <br/> <entry key = "encryptionpropfile" value = "outsecurity_enc.properties"/> <br/> <entry> <br/> <key> <br/> <value> passwordcallbackref </value> <br /> </key> <br/> <ref bean = "passwordcallback"/> <br/> </entry> <br/> </map> <br/> </property> <br/> </bean> </P> <p> <bean id = "wss4jinconfiguration" class = "org. apache. cxf. WS. security. wss4j. wss4jininterceptor "> <br/> <property name =" properties "> <br/> <map> <br/> <Entry key =" action "value =" timestamp encrypt signature" /> <br/> <Entry key = "user" value = "apmclient"/> <br/> <Entry key = "decryptionpropfile" value = "insecurity_enc.properties"/> <br /> <Entry key = "enablesignatureconfirmation" value = "true"/> <br/> <Entry key = "signaturepropfile" value = "outsecurity_sign.properties"/> <br/> <entry key = "signaturekeyidentifier" value = "issuerserial"/> <br/> <entry> <br/> <key> <br/> <value> passwordcallbackref </value> <br /> </key> <br/> <ref bean = "passwordcallback"/> <br/> </entry> <br/> </map> <br/> </property> <br/> </bean> </P> <p> <bean id = "passwordcallback" class = "com. cecltd. security. passwordhandler "/> </P> <p> </beans>
Insecurity_enc.properties
Org. apache. WS. security. crypto. provider = org. apache. WS. security. components. crypto. merlin <br/> Org. apache. WS. security. crypto. merlin. keystore. type = jks <br/> Org. apache. WS. security. crypto. merlin. keystore. password = keystorepass <br/> Org. apache. WS. security. crypto. merlin. alias. password = apmclientpass <br/> Org. apache. WS. security. crypto. merlin. keystore. alias = apmclient <br/> Org. apache. WS. security. crypto. merlin. file = clientstore. jks
Outsecurity_enc.properties
Org. apache. WS. security. crypto. provider = org. apache. WS. security. components. crypto. merlin <br/> Org. apache. WS. security. crypto. merlin. keystore. type = jks <br/> Org. apache. WS. security. crypto. merlin. keystore. password = keystorepass <br/> Org. apache. WS. security. crypto. merlin. alias. password = apmclientpass <br/> Org. apache. WS. security. crypto. merlin. keystore. alias = apmclient <br/> Org. apache. WS. security. crypto. merlin. file = clientstore. jks
Outsecurity_sign.properties
Org. apache. WS. security. crypto. provider = org. apache. WS. security. components. crypto. merlin <br/> Org. apache. WS. security. crypto. merlin. keystore. type = jks <br/> Org. apache. WS. security. crypto. merlin. keystore. password = keystorepass <br/> Org. apache. WS. security. crypto. merlin. alias. password = apmclientpass <br/> Org. apache. WS. security. crypto. merlin. keystore. alias = apmclient <br/> Org. apache. WS. security. crypto. merlin. file = clientstore. jks
Serverhost. Properties
Host. url = http: // 127.0.0.1: 8080/j6cxfsrvc/services
Cxf-config.xml
<Beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: jaxws = "http://cxf.apache.org/jaxws" <br/> xsi: schemalocation = "<br/> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd <br/> http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> </P> <p> <import resource = "classpath: META-INF/cxf. XML "/> <br/> <import resource =" classpath: META-INF/cxf/cxf-extension-soap.xml "/> <br/> <import resource =" classpath: META-INF/cxf/cxf-servlet.xml "/> </P> <p> <jaxws: endpoint id =" sayhisrvc "implementor =" com. cecltd. service. impl. sayhisrvcimpl "address ="/sayhisrvc "> <br/> <jaxws: ininterceptors> <br/> <Bean class =" org. apache. cxf. interceptor. loggingininterceptor "/> <br/> <Bean class =" org. apache. cxf. binding. soap. SAAJ. saajininterceptor "/> <br/> <ref bean =" wss4jinconfiguration "/> <br/> </jaxws: ininterceptors> <br/> <jaxws: outinterceptors> <br/> <Bean class = "org. apache. cxf. interceptor. loggingoutinterceptor "/> <br/> <Bean class =" org. apache. cxf. binding. soap. SAAJ. saajoutinterceptor "/> <br/> <ref bean =" wss4joutconfiguration "/> <br/> </jaxws: outinterceptors> <br/> </jaxws: endpoint> </P> <p> <bean id = "wss4jinconfiguration" class = "org. apache. cxf. WS. security. wss4j. wss4jininterceptor "> <br/> <property name =" properties "> <br/> <map> <br/> <Entry key =" action "value =" timestamp encrypt signature" /> <br/> <Entry key = "decryptionpropfile" value = "server_insecurity_enc.properties"/> <br/> <Entry key = "signaturepropfile" value = "server_insecurity_sign.properties"/> <br /> <entry> <br/> <key> <br/> <value> passwordcallbackref </value> <br/> </key> <br/> <ref bean = "passwordcallback"/> <br/> </entry> <br/> </map> <br/> </property> <br/> </bean> </P> <p> <bean id = "wss4joutconfiguration" class = "org. apache. cxf. WS. security. wss4j. wss4joutinterceptor "> <br/> <property name =" properties "> <br/> <map> <br/> <Entry key =" action "value =" timestamp encrypt signature" /> <br/> <Entry key = "user" value = "apmserver"/> <br/> <Entry key = "encryptionuser" value = "apmclient"/> <br /> <Entry key = "encryptionpropfile" value = "server_outsecurity_enc.properties"/> <br/> <Entry key = "signaturepropfile" value = "server_insecurity_sign.properties"/> <br/> <entry> <br/> <key> <br/> <value> passwordcallbackref </value> <br/> </key> <br/> <ref bean = "passwordcallback "/> <br/> </entry> <br/> </map> <br/> </property> <br/> </bean> </P> <p> <bean id = "passwordcallback" class = "com. cecltd. security. passwordhandler "/> </P> <p> </beans>
Web. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee <br/> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </P> <p> <context-param> <br/> <param-Name> contextconfiglocation </param-Name> <br/> <param-value> WEB-INF/cxf-config.xml </param-value> <br/> </context-param> </P> <p> <listener> <br/> <listener-class> <br/> Org. springframework. web. context. contextloaderlistener <br/> </listener-class> <br/> </listener> </P> <p> <servlet> <br/> <servlet-Name> cxfservlet </servlet-Name> <br/> <servlet-class> <br/> Org. apache. cxf. transport. servlet. cxfservlet <br/> </servlet-class> <br/> <load-on-startup> 1 </load-on-startup> <br/> </servlet> </P> <p> <servlet-mapping> <br/> <servlet-Name> cxfservlet </servlet-Name> <br/> <URL-pattern>/services /* </url-pattern> <br/> </servlet-mapping> </P> <p> </Web-app>