(转自http://blog.progs.be/71/cxf-simple-frontend-allow-all-ssl-certificates-and-set-basic-authentication-credentials)
CXF is a wonderful web services framework. It is mostly configured using spring, however, this falls short when trying to assure that all SSL certificates are accepted. In this case, programmatic configuration is needed.
In the case where I needed this, SSL was used only to assure that the communication is encrypted at the transport level. Though the server certificate is normally used to assure the that it cannot be replaced without being noticed, this was not our concern. Specifically, self signed certificates are used, and there is no guarantee that they will not be changed.
In CXF the configuration of the transport is done by the conduit. The following snippet indicates how this can be accessed for the simple frontend.
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass( PingService.class );
factory.setAddress( "https://localhost:8443/ca/pxws/1.0/ping" );
PingService client = (PingService) factory.create();
Client proxy = ClientProxy.getClient( client );
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
TLSClientParameters tcp = new TLSClientParameters();
tcp.setTrustManagers( new TrustManager[]{ new TrustAllX509TrustManager() } );
conduit.setTlsClientParameters( tcp );
Similarly, the conduit can also be used to set the credentials which may be needed when the service is secured using basic authentication (as can be configured in web.xml).
The full code for the test is
package example.ws10.test;
import example.ws10.PingService;
import junit.framework.TestCase;
import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.configuration.security.AuthorizationPolicy;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.equanda.util.security.SslUtil;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
/**
* Test the Ping service
*
* @author <a href="mailto:joachim@progs.be">Joachim Van der Auwera</a>
*/
public class PingTest
extends TestCase
{
public void testPingService()
throws Exception
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass( PingService.class );
factory.setAddress( "https://localhost:8443/ca/pxws/1.0/ping" );
PingService client = (PingService) factory.create();
Client proxy = ClientProxy.getClient( client );
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
TLSClientParameters tcp = new TLSClientParameters();
tcp.setTrustManagers( new TrustManager[]{ new SslUtil.TrustAllX509TrustManager() } );
conduit.setTlsClientParameters( tcp );
AuthorizationPolicy auth = conduit.getAuthorization();
if ( null == auth ) auth = new AuthorizationPolicy();
auth.setUserName( "local" );
auth.setPassword( "local" );
String res = client.getPing();
assertTrue( res.startsWith( "Ping back @" ) );
}
/**
* This class allow any X509 certificates to be used to authenticate the remote side of a secure socket, including
* self-signed certificates.
*/
public static class TrustAllX509TrustManager
implements X509TrustManager
{
/** Empty array of certificate authority certificates. */
private static final X509Certificate[] acceptedIssuers = new X509Certificate[]{ };
/**
* Always trust for client SSL chain peer certificate chain with any authType authentication types.
*
* @param chain the peer certificate chain.
* @param authType the authentication type based on the client certificate.
*/
public void checkClientTrusted( X509Certificate[] chain, String authType )
{}
/**
* Always trust for server SSL chain peer certificate chain with any authType exchange algorithm types.
*
* @param chain the peer certificate chain.
* @param authType the key exchange algorithm used.
*/
public void checkServerTrusted( X509Certificate[] chain, String authType )
{}
/**
* Return an empty array of certificate authority certificates which are trusted for authenticating peers.
*
* @return a empty array of issuer certificates.
*/
public X509Certificate[] getAcceptedIssuers()
{
return ( acceptedIssuers );
}
}
}
分享到:
相关推荐
赠送jar包:cxf-rt-frontend-simple-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-simple-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-simple-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-...
在CXF中,Simple FrontEnd Project (SFP) 方式是一种简化Web服务发布的模式,它允许开发者无需在业务接口上添加特定的协议注解,如`@WebService`和`@WebMethod`,从而使得业务逻辑与Web服务技术实现分离。...
在本文中,我们将探讨如何使用Apache CXF框架的Simple Frontend和Aegis绑定来发布和获取Web服务。这种方法提供了一种简洁的方式,无需过多的注解或特定于技术的接口,使得我们可以轻松地将任何类转换为Web服务。 1....
赠送jar包:cxf-rt-frontend-jaxrs-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxrs-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxrs-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxrs...
赠送jar包:cxf-rt-frontend-jaxws-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxws-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxws-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxws...
cxf中会出现编码问题的cxf-rt-frontend-simple-3.1.13.jar的源码
### CXF实现SSL安全验证 在现代网络应用中,安全通信是至关重要的。Apache CXF 是一个开源项目,提供了一套强大的工具和服务框架,用于构建和开发基于标准的服务(如 Web Services)。本文将详细介绍如何使用 CXF ...
在IT行业中,Web服务是应用程序之间进行通信的一种标准方法,CXF框架是Java世界中广泛使用的Web服务实现库。本文将深入探讨CXF安全访问的一个重要方面:HTTP基本认证(Http Basic Auth)。这是一种简单但有效的身份...
本篇将详细探讨如何使用CXF实现单向SSL(Secure Sockets Layer)和双向SSL的安全访问。首先,我们需要理解SSL的基本概念。 SSL是一种网络安全协议,用于在客户端和服务器之间建立加密连接,确保数据传输的隐私和...
CXF提供两种类型的前端(Frontend):JAX-WS和简单前端(Simple Frontend)。本节将详细介绍JAX-WS前端。 JAX-WS前端 Code-First方式 创建Service Endpoint Interface ( SEI) 添加Java注解 发布服务 开发客户端 ...
cxf-rt-frontend-jaxws-3.0.16.jar jar包下载3.0.16版本下载
利用Apache CXF开发webservice接口需要用到的jar集合 cxf-core-3.0.15.jar cxf-rt-bindings-soap-3.0.15.jar ...cxf-rt-frontend-simple-3.0.15.jar cxf-rt-transports-http-3.0.15.jar cxf-rt-wsdl-3.0.15.jar
CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and ...
标题 "cxf-rt-frontend-jaxrs-2.7.16.zip" 提供了我们正在处理的软件组件的核心信息。CXF 是一个流行的开放源代码服务框架,它允许开发人员构建和消费 Web 服务。RT(Run-Time)部分指的是 CXF 的运行时组件,而 ...
7. **安全性**:CXF提供了多种安全机制,如基本认证、Digest认证、OAuth、SSL/TLS以及WS-Security,确保Web服务的安全通信。 在"apache-cxf-3.1.1跟3.1.6所有jar包"中,每个版本通常会包含一组核心库和扩展库,这些...
<bean id="yourServiceClient" class="org.apache.cxf.frontend.ClientProxyFactoryBean"> ``` 以上配置中,`serviceClass`是WSDL中定义的服务接口,`wsdlLocation`指向WSDL文件,`service QName`是服务的...
Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL...
3. **cxf-rt-frontend-jaxws-2.7.12.jar**: JAX-WS(Java API for XML Web Services)前端实现,使得开发者可以使用Java注解来定义Web服务接口和实现,自动生成WSDL(Web服务描述语言)文档。此模块支持服务端和...