1 package com.freemindcafe.apache.cxf.jaxrs;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.security.GeneralSecurityException;
8 import java.security.KeyStore;
9 import java.security.KeyStoreException;
10 import java.security.NoSuchAlgorithmException;
11 import java.security.UnrecoverableKeyException;
12 import java.security.cert.CertificateException;
14 import javax.net.ssl.KeyManager;
15 import javax.net.ssl.KeyManagerFactory;
16 import javax.net.ssl.TrustManager;
17 import javax.net.ssl.TrustManagerFactory;
19 import org.
apache.cxf.configuration.jsse.TLSServerParameters;
20 import org.
apache.cxf.configuration.security.ClientAuthentication;
21 import org.
apache.cxf.interceptor.LoggingInInterceptor;
22 import org.
apache.cxf.interceptor.LoggingOutInterceptor;
23 import org.
apache.cxf.jaxrs.JAXRSServerFactoryBean;
24 import org.
apache.cxf.jaxws.JaxWsServerFactoryBean;
25 import org.
apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
26 import org.junit.Test;
33 public void start_server_without_ssl()
throws Exception{
35 JAXRSServerFactoryBean svrFactory =
new JAXRSServerFactoryBean();
37 svrFactory.setAddress(
"http://localhost:9001/bizsvc");
38 svrFactory.setServiceBean(implementor);
40 svrFactory.getInInterceptors().add(
new LoggingInInterceptor());
43 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
48 org.
apache.cxf.endpoint.Server server = svrFactory.create();
49 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
50 System.out.println(
"Server started at " + endpoint);
57 public void start_server_with_2_way_ssl()
throws Exception{
59 JAXRSServerFactoryBean svrFactory =
new JAXRSServerFactoryBean();
61 svrFactory.setAddress(
"https://localhost:9001/bizsvc");
62 svrFactory.setServiceBean(implementor);
64 svrFactory.getInInterceptors().add(
new LoggingInInterceptor());
68 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
73 svrFactory = configureSSLOnTheServer(svrFactory, 9001);
74 org.
apache.cxf.endpoint.Server server = svrFactory.create();
75 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
76 System.out.println(
"Server started at " + endpoint);
82 private JAXRSServerFactoryBean configureSSLOnTheServer(JAXRSServerFactoryBean sf,
int port) {
84 System.setProperty(
"javax.net.debug",
"ssl:handshake");
85 TLSServerParameters tlsParams =
new TLSServerParameters();
86 KeyStore keyStore = KeyStore.getInstance(
"JKS");
87 String password =
"password";
88 File keystoreFile =
new File(
"src\\com\\freemindcafe\\apache\\cxf\\jaxrs\\serverkeystore.jks");
89 keyStore.load(
new FileInputStream(keystoreFile), password.toCharArray());
90 KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
91 keyFactory.init(keyStore, password.toCharArray());
92 KeyManager[] km = keyFactory.getKeyManagers();
93 tlsParams.setKeyManagers(km);
95 File truststoreFile =
new File(
"src\\com\\freemindcafe\\apache\\cxf\\jaxrs\\serverkeystore.jks");
96 keyStore.load(
new FileInputStream(truststoreFile), password.toCharArray());
97 TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
98 trustFactory.init(keyStore);
99 TrustManager[] tm = trustFactory.getTrustManagers();
100 tlsParams.setTrustManagers(tm);
108 ClientAuthentication ca =
new ClientAuthentication();
109 ca.setRequired(
true);
111 tlsParams.setClientAuthentication(ca);
112 JettyHTTPServerEngineFactory factory =
new JettyHTTPServerEngineFactory();
113 factory.setTLSServerParametersForPort(port, tlsParams);
114 }
catch (KeyStoreException kse) {
115 System.out.println(
"Security configuration failed with the following: " + kse.getCause());
116 }
catch (NoSuchAlgorithmException nsa) {
117 System.out.println(
"Security configuration failed with the following: " + nsa.getCause());
118 }
catch (FileNotFoundException fnfe) {
119 System.out.println(
"Security configuration failed with the following: " + fnfe.getCause());
120 }
catch (UnrecoverableKeyException uke) {
121 System.out.println(
"Security configuration failed with the following: " + uke.getCause());
122 }
catch (CertificateException ce) {
123 System.out.println(
"Security configuration failed with the following: " + ce.getCause());
124 }
catch (GeneralSecurityException gse) {
125 System.out.println(
"Security configuration failed with the following: " + gse.getCause());
126 }
catch (IOException ioe) {
127 System.out.println(
"Security configuration failed with the following: " + ioe.getCause());
Generates java code from xsd and wsdl. Verifies basic, wsse token and 2 way auth interceptors. Uses soap out and fault interceptors. Uses SOAP UI as a client.