---
title: "CXF ws-security密码验证"
author: "asdcls"
date: "2009-12-31T00:10:40Z"
tags: ["Apache", "SOAP", "Security", "WebService", "XML"]
categories: ["企业架构"]
slug: "cxf-ws-security"
draft: false
lastmod: "2009-12-31T00:13:18Z"
id: 560138
url: "https://asdcls.iteye.com/blog/560138"
---

这两天学习webservice 看了很长时间资料决定用cxf ， 但把cxf自带的demo转化成自己的代码还是遇到了不小的麻烦。今天终于调通了个简单的示例。 运行环境是tomcat服务器，cxf 2.2.3 这里面为了简单，自采用单向的验证，只验证客户，双向验证类似，在客户端做配置jaxws:inInterceptors就可以了。 在服务端和客户端得回调类里设置密码就可以了，如果不相同就会验证失败，这里面传输的是加密后的密码。也可以传输密码明文 服务器端设置： 1.首先，创建web.xml \[code="xml"] contextConfigLocation WEB-INF/ws-context.xml WEB-INF/wssec.xml org.springframework.web.context.ContextLoaderListener CXFServlet **CXF** Servlet org.apache.**cxf**.transport.servlet.CXFServlet 1 CXFServlet /* \[/code] 2.创建ws-context.xml用于发布webservice \[code="xml"] cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://**cxf**.apache.org/jaxws http://**cxf**.apache.org/schemas/jaxws.xsd"&gt; cxf/**cxf**.xml" /&gt; cxf/**cxf**-extension-soap.xml" /&gt; cxf/**cxf**-servlet.xml" /&gt; cxf.binding.soap.saaj.SAAJInInterceptor" /&gt; cxf.ws.security.wss4j.WSS4JInInterceptor"&gt; \[/code] 3.拦截处理类 验证密码 \[code="java"]package cn.org.coral.biz.examples.webservice.handler; 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 WsAuthHandler implements CallbackHandler{ public void handle(Callback\[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i &lt; callbacks.length; i++) { WSPasswordCallback pc = (WSPasswordCallback) callbacks\[i]; int usage = pc.getUsage(); System.out.println("identifier: " + pc.getIdentifier()); System.out.println("usage: " + pc.getUsage()); pc.setPassword("admin"); } } } \[/code] \[code="java"]package cn.org.coral.biz.examples.webservice.handler; 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 WsAuthHandler implements CallbackHandler{ public void handle(Callback\[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i &lt; callbacks.length; i++) { WSPasswordCallback pc = (WSPasswordCallback) callbacks\[i]; int usage = pc.getUsage(); System.out.println("identifier: " + pc.getIdentifier()); System.out.println("usage: " + pc.getUsage()); pc.setPassword("admin"); } } }\[/code] 客户端配置： 1.wsclient-context.xml \[code="java"] cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://**cxf**.apache.org/jaxws http://**cxf**.apache.org/schemas/jaxws.xsd"&gt; cxf/**cxf**.xml" /&gt; cxf/**cxf**-extension-soap.xml" /&gt; cxf/**cxf**-servlet.xml" /&gt; cxf.jaxws.JaxWsProxyFactoryBean"&gt; cxf.binding.soap.saaj.SAAJOutInterceptor" /&gt; cxf.ws.security.wss4j.WSS4JOutInterceptor"&gt; passwordCallbackRef \[/code] \[code="xml"] passwordCallbackRef \[/code] 2.验证密码类 \[code="java"]package cn.org.coral.biz.examples.webservice.handler; 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 WsClinetAuthHandler implements CallbackHandler{ public void handle(Callback\[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i &lt; callbacks.length; i++) { WSPasswordCallback pc = (WSPasswordCallback) callbacks\[i]; int usage = pc.getUsage(); System.out.println("identifier: " + pc.getIdentifier()); System.out.println("usage: " + pc.getUsage()); pc.setPassword("admin"); } } } \[/code] \[code="java"]package cn.org.coral.biz.examples.webservice.handler; 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 WsClinetAuthHandler implements CallbackHandler{ public void handle(Callback\[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i &lt; callbacks.length; i++) { WSPasswordCallback pc = (WSPasswordCallback) callbacks\[i]; int usage = pc.getUsage(); System.out.println("identifier: " + pc.getIdentifier()); System.out.println("usage: " + pc.getUsage()); pc.setPassword("admin"); } } } \[/code]