1 package com.freemindcafe.apache.cxf.jaxrs.sample9;
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;
26 import org.
apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
27 import org.junit.Test;
32 public void start_server_without_ssl()
throws Exception{
34 JAXRSServerFactoryBean svrFactory =
new JAXRSServerFactoryBean();
36 svrFactory.setAddress(
"http://localhost:9001/bizsvc");
37 svrFactory.setServiceBean(implementor);
39 svrFactory.getInInterceptors().add(
new LoggingInInterceptor());
42 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
47 org.
apache.cxf.endpoint.Server server = svrFactory.create();
48 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
49 System.out.println(
"Server started at " + endpoint);
63 public void start_server_with_2_way_ssl()
throws Exception{
65 JAXRSServerFactoryBean svrFactory =
new JAXRSServerFactoryBean();
67 svrFactory.setAddress(
"https://localhost:9001/bizsvc");
68 svrFactory.setServiceBean(implementor);
70 svrFactory.getInInterceptors().add(
new LoggingInInterceptor());
74 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
79 svrFactory = configureSSLOnTheServer(svrFactory, 9001);
80 org.
apache.cxf.endpoint.Server server = svrFactory.create();
81 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
82 System.out.println(
"Server started at " + endpoint);
88 private JAXRSServerFactoryBean configureSSLOnTheServer(JAXRSServerFactoryBean sf,
int port) {
90 System.setProperty(
"javax.net.debug",
"ssl:handshake");
91 TLSServerParameters tlsParams =
new TLSServerParameters();
92 KeyStore keyStore = KeyStore.getInstance(
"JKS");
93 String password =
"password";
94 File keystoreFile =
new File(
"src\\com\\freemindcafe\\apache\\cxf\\jaxrs\\sample9\\serverkeystore.jks");
95 keyStore.load(
new FileInputStream(keystoreFile), password.toCharArray());
96 KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
97 keyFactory.init(keyStore, password.toCharArray());
98 KeyManager[] km = keyFactory.getKeyManagers();
99 tlsParams.setKeyManagers(km);
101 File truststoreFile =
new File(
"src\\com\\freemindcafe\\apache\\cxf\\jaxrs\\sample9\\serverkeystore.jks");
102 keyStore.load(
new FileInputStream(truststoreFile), password.toCharArray());
103 TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
104 trustFactory.init(keyStore);
105 TrustManager[] tm = trustFactory.getTrustManagers();
106 tlsParams.setTrustManagers(tm);
114 ClientAuthentication ca =
new ClientAuthentication();
115 ca.setRequired(
true);
117 tlsParams.setClientAuthentication(ca);
118 JettyHTTPServerEngineFactory factory =
new JettyHTTPServerEngineFactory();
119 factory.setTLSServerParametersForPort(port, tlsParams);
120 }
catch (KeyStoreException kse) {
121 System.out.println(
"Security configuration failed with the following: " + kse.getCause());
122 }
catch (NoSuchAlgorithmException nsa) {
123 System.out.println(
"Security configuration failed with the following: " + nsa.getCause());
124 }
catch (FileNotFoundException fnfe) {
125 System.out.println(
"Security configuration failed with the following: " + fnfe.getCause());
126 }
catch (UnrecoverableKeyException uke) {
127 System.out.println(
"Security configuration failed with the following: " + uke.getCause());
128 }
catch (CertificateException ce) {
129 System.out.println(
"Security configuration failed with the following: " + ce.getCause());
130 }
catch (GeneralSecurityException gse) {
131 System.out.println(
"Security configuration failed with the following: " + gse.getCause());
132 }
catch (IOException ioe) {
133 System.out.println(
"Security configuration failed with the following: " + ioe.getCause());