Samples JDK
Server.java
1 package com.freemindcafe.apache.cxf.jaxrs;
2 
3 import java.io.File;
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;
13 
14 import javax.net.ssl.KeyManager;
15 import javax.net.ssl.KeyManagerFactory;
16 import javax.net.ssl.TrustManager;
17 import javax.net.ssl.TrustManagerFactory;
18 
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;
27 
28 import com.freemindcafe.apache.cxf.wsdl.sample1.SSLInterceptor;
29 
30 public class Server {
31 
32  @Test
33  public void start_server_without_ssl() throws Exception{
34  OrderInfoImpl implementor = new OrderInfoImpl();
35  JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean();
36  svrFactory.setServiceClass(OrderInfoImpl.class);
37  svrFactory.setAddress("http://localhost:9001/bizsvc");
38  svrFactory.setServiceBean(implementor);
39  //in interceptors
40  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
41  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
42  // out normal response interceptor
43  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
44  svrFactory.getOutInterceptors().add(new OutSecurityInterceptor());
45  //out fault interceptor
46  svrFactory.getOutFaultInterceptors().add(new OutSecurityFaultInterceptor());
47 
48  org.apache.cxf.endpoint.Server server = svrFactory.create();
49  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
50  System.out.println("Server started at " + endpoint);
51  synchronized(server){
52  server.wait();
53  }
54  }
55 
56  @Test
57  public void start_server_with_2_way_ssl() throws Exception{
58  OrderInfoImpl implementor = new OrderInfoImpl();
59  JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean();
60  svrFactory.setServiceClass(OrderInfoImpl.class);
61  svrFactory.setAddress("https://localhost:9001/bizsvc");
62  svrFactory.setServiceBean(implementor);
63  //in interceptors
64  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
65  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
66  svrFactory.getInInterceptors().add(new SSLInterceptor());
67  // out normal response interceptor
68  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
69  svrFactory.getOutInterceptors().add(new OutSecurityInterceptor());
70  //out fault interceptor
71  svrFactory.getOutFaultInterceptors().add(new OutSecurityFaultInterceptor());
72 
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);
77  synchronized(server){
78  server.wait();
79  }
80  }
81 
82  private JAXRSServerFactoryBean configureSSLOnTheServer(JAXRSServerFactoryBean sf, int port) {
83  try {
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);
94 
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);
101 // FiltersType filter = new FiltersType();
102 // filter.getInclude().add(".*_EXPORT_.*");
103 // filter.getInclude().add(".*_EXPORT1024_.*");
104 // filter.getInclude().add(".*_WITH_DES_.*");
105 // filter.getInclude().add(".*_WITH_NULL_.*");
106 // filter.getExclude().add(".*_DH_anon_.*");
107 // tlsParams.setCipherSuitesFilter(filter);
108  ClientAuthentication ca = new ClientAuthentication();
109  ca.setRequired(true);
110  ca.setWant(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());
128  }
129 
130  return sf;
131  }
132 
133 }
134 
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.