- 浏览: 1213365 次
- 性别:
- 来自: 广州
博客专栏
-
Apache CXF使用s...
浏览量:109931
文章分类
- 全部博客 (189)
- Spring (13)
- Hibernate (10)
- liferay portal (3)
- Java (18)
- 数据库 (32)
- JS (12)
- Eclipse (16)
- Log4j (3)
- 我的收藏夹 (8)
- FF and IE (2)
- Groovy Grails (0)
- Web Service (16)
- Quartz (6)
- Tomcat (8)
- Linux (4)
- xml (4)
- Silverlight (1)
- Flex (10)
- JBoss (4)
- EJB (2)
- WAS(webSphere) (5)
- DOS命令 (2)
- JSON (2)
- Maven (1)
- OThers (1)
- SVN (1)
- iBatis (4)
- OS (1)
- 问题解决 (8)
- 待看文章 (2)
- 多线程 (2)
- 代码收藏(即拿即用工具类) (5)
- Socket (2)
- Android (4)
- 其他 (1)
- python (1)
- Genymotion (1)
最新评论
-
a807966224:
还是 多谢楼主 总结的挺好的 !!!
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用) -
a807966224:
然后 通过 SOAPMessage.getHeader(qna ...
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用) -
a807966224:
我也是接触这东西不久,QName qname = new QN ...
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用) -
a807966224:
楼主 不知道你有没有出现 从headers 里取出来长 ...
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用) -
xdc0209:
兄弟呀,报错啦 2011-12-15 13:27:15 n ...
Hibernate+EhCache配置二级缓存
注意:以下客户端调用代码中获取服务端ws实例,都是通过CXF 入门: 远程接口调用方式实现
直入正题!
以下是服务端配置
========================================================
一,web.xml配置,具体不在详述
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <context-param> <param-name>contextConfigLocation</param-name> <!--ws-context.xml(必须)是cxf配置文件, wssec.xml可选,作用可以打印出加密信息类容 --> <param-value>WEB-INF/ws-context.xml,WEB-INF/wssec.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
二,ws具体代码
简单的接口
import javax.jws.WebService; @WebService() public interface WebServiceSample { String say(String name); }
接口具体实现类
public class WebServiceSampleImpl implements WebServiceSample { public String say(String name) { return "你好," + name; } }
三,ws回调函数,必须实现javax.security.auth.callback.CallbackHandler
从cxf2.4.x后校验又cxf内部实现校验,所以不必自己校验password是否相同,但客户端必须设置,详情请参考:http://cxf.apache.org/docs/24-migration-guide.html的Runtime Changes片段
回调函数WsAuthHandler代码,校验客户端请求是否合法 ,合法就放行,否则拒绝执行任何操作
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.cxf.interceptor.Fault; import org.apache.ws.security.WSPasswordCallback; import org.apache.xmlbeans.impl.soap.SOAPException; public class WsAuthHandler implements CallbackHandler { public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { WSPasswordCallback pc = (WSPasswordCallback) callbacks[i]; String identifier = pc.getIdentifier(); int usage = pc.getUsage(); if (usage == WSPasswordCallback.USERNAME_TOKEN) {// 密钥方式USERNAME_TOKEN // username token pwd... // ▲这里的值必须和客户端设的值相同,从cxf2.4.x后校验方式改为cxf内部实现校验,不必自己比较password是否相同 // 请参考:http://cxf.apache.org/docs/24-migration-guide.html的Runtime // Changes片段 pc.setPassword("testPassword");// ▲【这里非常重要】▲ // ▲PS 如果和客户端不同将抛出org.apache.ws.security.WSSecurityException: // The // security token could not be authenticated or // authorized异常,服务端会认为客户端为非法调用 } else if (usage == WSPasswordCallback.SIGNATURE) {// 密钥方式SIGNATURE // set the password for client's keystore.keyPassword // ▲这里的值必须和客户端设的值相同,从cxf2.4.x后校验方式改为cxf内部实现校验,不必自己比较password是否相同; // 请参考:http://cxf.apache.org/docs/24-migration-guide.html的Runtime // Changes片段 pc.setPassword("testPassword");// //▲【这里非常重要】▲ // ▲PS:如果和客户端不同将抛出org.apache.ws.security.WSSecurityException:The // security token could not be authenticated or // authorized异常,服务端会认为客户端为非法调用 } //不用做其他操作 } } }
四,CXF配置ws-context.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:jaxws="http://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"> <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" /> <!-- 以上未基本配置,必须,位置在cxf jar中 --> <jaxws:endpoint id="webServiceSample" address="/WebServiceSample" implementor="com.service.impl.WebServiceSampleImpl"> <!--inInterceptors表示被外部调用时,调用此拦截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" /> <bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> <constructor-arg> <map> <!-- 设置加密类型 --> <entry key="action" value="UsernameToken" /> <!-- 设置密码类型为明文 --> <entry key="passwordType" value="PasswordText" /> <!--<entry key="action" value="UsernameToken Timestamp" /> 设置密码类型为加密<entry key="passwordType" value="PasswordDigest" /> --> <entry key="passwordCallbackClass" value="com.service.handler.WsAuthHandler" /> </map> </constructor-arg> </bean> </jaxws:inInterceptors> </jaxws:endpoint> </beans>
CXF配置wssec.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:cxf="http://cxf.apache.org/core" xmlns:wsa="http://cxf.apache.org/ws/addressing" xmlns:http="http://cxf.apache.org/transports/http/configuration" xmlns:wsrm-policy="http://schemas.xmlsoap.org/ws/2005/02/rm/policy" xmlns:wsrm-mgr="http://cxf.apache.org/ws/rm/manager" xsi:schemaLocation=" http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd http://schemas.xmlsoap.org/ws/2005/02/rm/policy http://schemas.xmlsoap.org/ws/2005/02/rm/wsrm-policy.xsd http://cxf.apache.org/ws/rm/manager http://cxf.apache.org/schemas/configuration/wsrm-manager.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <cxf:bus> <cxf:features> <cxf:logging /> <wsa:addressing /> </cxf:features> </cxf:bus> </beans>
服务端代码及配置到此结束!!!
=========================================================
=========================================================
以下是客户端配置,主要是回调函数,在客户端调用服务端前被调用,负责安全信息的设置
----------------------------------------------------------------------------------------
一,先实现回调函数WsClinetAuthHandler,同样必须实现javax.security.auth.callback.CallbackHandler
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 < callbacks.length; i++) { WSPasswordCallback pc = (WSPasswordCallback) callbacks[i]; System.out.println("identifier: " + pc.getIdentifier()); // 这里必须设置密码,否则会抛出:java.lang.IllegalArgumentException: pwd == null // but a password is needed pc.setPassword("testPassword");// ▲【这里必须设置密码】▲ } } }
二,客户端调用代码:
import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor; import org.apache.ws.security.WSConstants; import org.apache.ws.security.handler.WSHandlerConstants; import test.saa.client.WebServiceSample; import test.saa.handler.WsClinetAuthHandler; public class TestClient { public static void main(String[] args) { // 以下和服务端配置类似,不对,应该说服务端和这里的安全验证配置一致 Map<String, Object> outProps = new HashMap<String, Object>(); outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN); outProps.put(WSHandlerConstants.USER, "admin"); outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT); // 指定在调用远程ws之前触发的回调函数WsClinetAuthHandler,其实类似于一个拦截器 outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, WsClinetAuthHandler.class.getName()); ArrayList list = new ArrayList(); // 添加cxf安全验证拦截器,必须 list.add(new SAAJOutInterceptor()); list.add(new WSS4JOutInterceptor(outProps)); JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); // WebServiceSample服务端接口实现类,这里并不是直接把服务端的类copy过来,具体请参考http://learning.iteye.com/blog/1333223 factory.setServiceClass(WebServiceSample.class); // 设置ws访问地址 factory.setAddress("http://localhost:8080/cxf-wssec/services/WebServiceSample"); //注入拦截器,用于加密安全验证信息 factory.getOutInterceptors().addAll(list); WebServiceSample service = (WebServiceSample) factory.create(); String response = service.say("2012"); System.out.println(response); } }
客户端到此结束!!!!
========================================================================
#######################################################################
PS:客户端的另一种调用方式,主要通过配置文件,不过需要spring bean的配置文件(第一种就不用牵扯到spring的配置,比较通用吧!)
一,回调函数WsClinetAuthHandler不变,和上面一样
二,client-beans.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:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> <!--这里无非是通过配置来替代JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean()创建代理并实例化一个ws--> <bean id="client" class="test.saa.client.WebServiceSample" factory-bean="clientFactory" factory-method="create" /> <!-- 通过代理创建ws实例 --> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="test.saa.client.WebServiceSample" /> <!-- ws地址,也可以是完整的wsdl地址 --> <property name="address" value="http://localhost:8080/cxf-wssec/services/WebServiceSample" /> <!--outInterceptors表示调用外部指定ws时,调用此拦截器 --> <property name="outInterceptors"> <list> <bean class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor" /> <ref bean="wss4jOutConfiguration" /> </list> </property> </bean> <bean id="wss4jOutConfiguration" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor"> <property name="properties"> <map> <!-- 设置加密类型 ,服务端需要和这里的设置保持一致--> <entry key="action" value="UsernameToken" /> <entry key="user" value="admin" /> <!-- 设置密码为明文 ,服务端需要和这里的设置保持一致--> <entry key="passwordType" value="PasswordText" /> <!-- <entry key="action" value="UsernameToken Timestamp" /> <entry key="user" value="adminTest" /> 设置密码类型为加密方式,服务端需要和这里的设置保持一致<entry key="passwordType" value="PasswordDigest" /> --> <entry key="passwordCallbackRef"> <ref bean="passwordCallback" /> </entry> </map> </property> </bean> <bean id="passwordCallback" class="test.saa.handler.WsClinetAuthHandler" /> </beans>
三,具体调用服务端代码:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import test.saa.client.WebServiceSample; public final class Client { public static void main(String args[]) throws Exception { //加载配置 ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "test/saa/client-beans.xml" }); //获取ws实例 WebServiceSample client = (WebServiceSample) context.getBean("client"); String response = client.say("2012"); System.out.println("Response: " + response); } }
到此客户端第二种实现方式结束
GOOD LUCKY!!!
如有不明,请指教!!!
评论
6 楼
wentao_tang
2013-12-02
客户端调用,这行代码是必须的吗,有什么作用吗?
outProps.put(WSHandlerConstants.USER, "admin");
outProps.put(WSHandlerConstants.USER, "admin");
5 楼
xiangximingong
2013-09-18
我想问下,就是客户端传入错误的密码,也会抛一个异常,但这个异常我不知道在哪捕获,也不知道怎么通知客户端密码错误!能否指教下……
4 楼
zhenglongfei
2013-07-25
想不通,为什么要在服务端要在set一下呢??
3 楼
Sev7en_jun
2012-12-06
ice.kane 写道
zl759869747 写道
我想问下,为什么在服务端的回调函数里面当还要setPassword呢?我很郁闷的是,我在客户端明明已经设定了密码,可是服务端却拿不到,而且如果有服务端不设定密码就抛异常,我想问下到底是怎么样校验的呢?
我也是遇到这个问题。。
在服务端setPassword,那密码校验还有什么意义
1,为什么不设置密码会抛异常:
--既然要把服务加密,就要设置密码,要不然怎么加密,怎么知道哪些客户端是有权限访问的,就像买了一把锁,连自己(服务端)都不知道钥匙是怎么,那别人(客户端)怎么开这把锁
2,服务端设置密码是为了给自己上一把锁,如果要访问它必须有开这把锁的钥匙,即密码,有钥匙还要保证这个钥匙可以开这把锁,即客户端拿的密码是否和服务端锁的密码一致
2 楼
ice.kane
2012-11-27
zl759869747 写道
我想问下,为什么在服务端的回调函数里面当还要setPassword呢?我很郁闷的是,我在客户端明明已经设定了密码,可是服务端却拿不到,而且如果有服务端不设定密码就抛异常,我想问下到底是怎么样校验的呢?
我也是遇到这个问题。。
在服务端setPassword,那密码校验还有什么意义
1 楼
zl759869747
2012-10-23
我想问下,为什么在服务端的回调函数里面当还要setPassword呢?我很郁闷的是,我在客户端明明已经设定了密码,可是服务端却拿不到,而且如果有服务端不设定密码就抛异常,我想问下到底是怎么样校验的呢?
发表评论
-
解决(CXF):The security token could not be authenticated or authorized
2012-01-16 09:50 47211,使用cxf回调函数,服务端setPassword和客 ... -
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用)
2012-01-12 18:23 18555CXF 入门:HelloWorld接口发布 CXF 入门: ... -
解决(CXF):SOAPFaultException: Fault occurred while processing
2012-01-04 17:23 22612javax.xml.ws.soap.SOAPFault ... -
CXF 入门: 远程接口调用
2012-01-02 17:15 7869本篇是接着:CXF 入门之接口发布写的,建议先看上篇 ... -
CXF 入门:HelloWorld接口发布
2012-01-02 16:31 6215第一步:在myeclipse中新建一个web项目名为myWs, ... -
使用XFire+Spring构建Web Service
2011-12-01 12:01 1700FROM: http://shuangjianyu.blog. ... -
使用XFire+Spring+Struts构建Web Service及测试
2011-11-30 13:04 2609FROM: http://shade8109.blog.1 ... -
Apache Axis配置文件WSDD详解
2011-11-30 12:37 26133FROM: http://apps.hi.baidu.co ... -
(Axis 入门) 超简单webservice实例
2011-11-30 11:21 2085From: http://user.qzone.qq.com/ ... -
解决:nor any of its super class is known to this context.
2011-10-26 13:38 17837可以参考http://blog.csdn.net/chen_y ... -
WebService CXF学习(进阶篇3):CXF整合Spring框架 .
2011-10-26 13:28 79转载于:http://blog.csdn.net/chen ... -
WebService CXF学习 自定义对象传递(简单JavaBean对象) ..
2011-10-26 13:25 91这一节我们就CXF框架简单的JavaBean对象传递进行讲解。 ... -
WebService处理复杂对象List Map(jdk api jws)
2011-10-26 13:02 8663XFire不用这么做,Apache CXF 只需要指定集合中具 ... -
解决: is not found. Have you run APT to generate them?
2011-10-25 13:15 31672WSSERVLET11: failed to parse ru ... -
WebServices CXF开发常见异常及解决方法
2011-10-25 13:08 3511转载于:http://apps.hi.baidu.com/ ... -
解决:java.util.List does not have a no-arg default constructor.
2011-10-25 13:05 3014详见:http://learning.iteye.com/ad ... -
解决: counts of IllegalAnnotationExceptions and JAXB can't handle interfaces.
2011-10-25 13:02 21856、Caused by: com.sun.xml.bind.v2 ...
相关推荐
1)参考: ...2)CXFWS工程是基于WS-Security规范,实现X.509身份验证的,同时实现签名和加密 keytool 工具的使用参考 http://hi.baidu.com/qianshuifanchuan/blog/item/6291b8510009ad3c42a75b8e.html ...
纯java调用ws-security+CXF实现的webservice安全接口
CXF使用WSS4J实现WS-Security规范,本例的配置是Timestamp Signature Encrypt,具体使用可以参考我的博客http://blog.csdn.net/wangchsh2008/article/details/6708270
cxf结合ws-security实现webservice 用户名/密码身份认证安全调用,依赖包
基于WS-Security的证据服务系统安全方案的实现,平野,高强 ,证据服务系统的核心和关键是交互的安全性。本文从身份认证、保密性、完整性和不可抵赖性方面提出基于WS-Security的证据服务系统安全�
cxf ws-Security的实现 WS-SecurityPolicy 安全配置指定在客户机和服务之间交换的消息所需的安全处理。在大多数情况下,Web 服务堆栈还需要更多信息,才能对消息交换应用安全措施。 里面有2个project,分别server ...
CXF WS-Security WSS4J 例子 可以运行,运行的时候只要运行client就行,重点是运行完之后要关掉第一个控制台,才能看到结果。一定要记得改一下client的路径名
支持的Web服务标准包括: SOAP WS-Addressing WS-Policy WS-ReliableMessaging WS-Security WS-SecurityPolicy WS-SecureConversation JAX-WS API,用于Web服务开发 WSDL优先工具 Java优先支持 JAX-RS ...
了解 Web 服务规范_第 4 部分:WS-Security源码
经过了几天的努力与查询不少的资料与调试,头都大了,终于给CXF加上了一把密码锁,希望进步;
Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL...
udp-3.0.11.jar,cxf-rt-wsdl-3.0.0.jar,cxf-rt-ws-security-3.0.0.jar,neethi-3.0.3.jar,slf4j-api-1.7.7.jar,stax2-api-3.1.4.jar,woodstox-core-asl-4.4.1.jar,wsdl4j-1.6.3.jar,wss4j-bindings-2.0.9.jar,xml...
Web Services Standards Support: CXF supports a variety of web service standards including SOAP, the Basic Profile, WSDL, WS-Addressing, WS-Policy, WS-ReliableMessaging, WS-Security, WS-SecurityPolicy,...
2. Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 ...
对WebServcie进行双层加密,自己写的一个小例子。
配置cxf ws security的教程
logging-1.1.1.jar,cxf-2.7.6.jar,cxf-manifest.jar,cxf-services-sts-core-2.7.6.jar,cxf-services-ws-discovery-api-2.7.6.jar,cxf-services-ws-discovery-service-2.7.6.jar,cxf-services-wsn-api-2.7.6.jar,cxf-...
cxf用户封装webService,调用webservice, 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security
Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL ...
cxf-services-sts-core-2.6.3.jar cxf-services-wsn-api-2.6.3.jar cxf-services-wsn-core-2.6.3.jar cxf-xjc-boolean-2.6.0.jar cxf-xjc-bug671-2.6.0.jar cxf-xjc-dv-2.6.0.jar cxf-xjc-runtime-2.6.0.jar cxf-xjc...