Samples JDK
Server.java
1 package com.freemindcafe.apache.cxf.jaxrs.sample9;
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.phase.Phase;
26 import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
27 import org.junit.Test;
28 
29 public class Server {
30 
31  @Test
32  public void start_server_without_ssl() throws Exception{
33  OrderInfoImpl implementor = new OrderInfoImpl();
34  JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean();
35  svrFactory.setServiceClass(OrderInfoImpl.class);
36  svrFactory.setAddress("http://localhost:9001/bizsvc");
37  svrFactory.setServiceBean(implementor);
38  //in interceptors
39  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
40  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
41  // out normal response interceptor
42  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
43  svrFactory.getOutInterceptors().add(new OutSecurityInterceptor());
44  //out fault interceptor
45  svrFactory.getOutFaultInterceptors().add(new OutSecurityFaultInterceptor());
46 
47  org.apache.cxf.endpoint.Server server = svrFactory.create();
48  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
49  System.out.println("Server started at " + endpoint);
50  synchronized(server){
51  server.wait();
52  }
53  }
54 
55  @Test
56  /*
57  * Invocation order of interceptors depends on the phase.
58  * Please refer to http://cxf.apache.org/docs/interceptors.html for the Phase ordering
59  * Since Phase.PRE_PROTOCOL comes before Phase.UNMARSHAL, SSL interceptor will be invoked first.
60  * If two interceptors share the same Phase, then they will be invoked in the order they
61  * are attched to the bus. Whichsoever is atatched first is invoked first.
62  */
63  public void start_server_with_2_way_ssl() throws Exception{
64  OrderInfoImpl implementor = new OrderInfoImpl();
65  JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean();
66  svrFactory.setServiceClass(OrderInfoImpl.class);
67  svrFactory.setAddress("https://localhost:9001/bizsvc");
68  svrFactory.setServiceBean(implementor);
69  //in interceptors
70  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
71  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
72  svrFactory.getInInterceptors().add(new SSLInterceptor());
73  // out normal response interceptor
74  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
75  svrFactory.getOutInterceptors().add(new OutSecurityInterceptor());
76  //out fault interceptor
77  svrFactory.getOutFaultInterceptors().add(new OutSecurityFaultInterceptor());
78 
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);
83  synchronized(server){
84  server.wait();
85  }
86  }
87 
88  private JAXRSServerFactoryBean configureSSLOnTheServer(JAXRSServerFactoryBean sf, int port) {
89  try {
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);
100 
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);
107 // FiltersType filter = new FiltersType();
108 // filter.getInclude().add(".*_EXPORT_.*");
109 // filter.getInclude().add(".*_EXPORT1024_.*");
110 // filter.getInclude().add(".*_WITH_DES_.*");
111 // filter.getInclude().add(".*_WITH_NULL_.*");
112 // filter.getExclude().add(".*_DH_anon_.*");
113 // tlsParams.setCipherSuitesFilter(filter);
114  ClientAuthentication ca = new ClientAuthentication();
115  ca.setRequired(true);
116  ca.setWant(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());
134  }
135 
136  return sf;
137  }
138 
139 }
140