Samples JDK
Server.java
1 package com.freemindcafe.apache.cxf.wsdl.sample3;
2 
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.net.URL;
8 import java.security.GeneralSecurityException;
9 import java.security.KeyStore;
10 import java.security.KeyStoreException;
11 import java.security.NoSuchAlgorithmException;
12 import java.security.UnrecoverableKeyException;
13 import java.security.cert.CertificateException;
14 import java.util.HashMap;
15 import java.util.Map;
16 
17 import javax.net.ssl.KeyManager;
18 import javax.net.ssl.KeyManagerFactory;
19 import javax.net.ssl.TrustManager;
20 import javax.net.ssl.TrustManagerFactory;
21 import javax.xml.namespace.QName;
22 
23 import org.apache.cxf.Bus;
24 import org.apache.cxf.BusFactory;
25 import org.apache.cxf.configuration.jsse.TLSServerParameters;
26 import org.apache.cxf.configuration.security.ClientAuthentication;
27 import org.apache.cxf.configuration.security.FiltersType;
28 import org.apache.cxf.interceptor.LoggingInInterceptor;
29 import org.apache.cxf.interceptor.LoggingOutInterceptor;
30 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
31 import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
32 import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
37 import org.junit.Test;
38 
39 public class Server {
40 
41  /**
42  * It has basic auth, wsse security token as in interceptor.
43  * It also has out and fault interceptors.
44  * Interceptors prints messages on server console.
45  */
46  @Test
47  public void start_server_without_ssl() throws Exception{
48 
49  GreeterImpl implementor = new GreeterImpl();
50  JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
51  svrFactory.setServiceClass(Greeter.class);
52  svrFactory.setAddress("http://localhost:9001/SoapContext/SoapPort");
53  svrFactory.setServiceBean(implementor);
54  //in interceptors
55  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
56  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
57  Map<String, Object> properties = new HashMap<>();
58  properties.put("action", "UsernameToken");
59  properties.put("passwordType", "PasswordText");
60  properties.put("passwordCallbackRef", new SecurityContextCallback());
61  svrFactory.getInInterceptors().add(new WSSecurityInterceptor(properties));
62  Map<String, Object> wssParams = new HashMap<>();
63  wssParams.put("ws-security.ut.validator", new SampleUsernameTokenValidator());
64  wssParams.put("ws-security.validate.token", "true");
65  svrFactory.setProperties(wssParams);
66  // out normal response interceptor
67  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
68  svrFactory.getOutInterceptors().add(new SoapOutSecurityInterceptor());
69  //out fault interceptor
70  svrFactory.getOutFaultInterceptors().add(new SoapOutSecurityFaultInterceptor());
71 
72  org.apache.cxf.endpoint.Server server = svrFactory.create();
73  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
74  System.out.println("Server started at " + endpoint);
75 
76  synchronized(server){
77  server.wait();
78  }
79  }
80 
81  /**
82  * @throws Exception
83  */
84  @Test
86 
87  GreeterImpl implementor = new GreeterImpl();
88  JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
89  svrFactory.setServiceClass(Greeter.class);
90  svrFactory.setAddress("http://localhost:9001/SoapContext/SoapPort");
91  svrFactory.setServiceBean(implementor);
92  //in interceptors
93  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
94  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
95  Map<String, Object> properties = new HashMap<>();
96  properties.put("action", "UsernameToken");
97  properties.put("passwordType", "PasswordText");
98  properties.put("passwordCallbackRef", new SecurityContextCallback());
99  svrFactory.getInInterceptors().add(new WSSecurityInterceptor(properties));
100  Map<String, Object> wssParams = new HashMap<>();
101  wssParams.put("ws-security.ut.validator", new SampleUsernameTokenValidator());
102  wssParams.put("ws-security.validate.token", "true");
103  svrFactory.setProperties(wssParams);
104  // out normal response interceptor
105  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
106  svrFactory.getOutInterceptors().add(new SoapOutSecurityInterceptor());
107  //out fault interceptor
108  svrFactory.getOutFaultInterceptors().add(new SoapOutSecurityFaultInterceptor());
109 
110  org.apache.cxf.endpoint.Server server = svrFactory.create();
111  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
112  System.out.println("Server started at " + endpoint);
113 
114  //client
115  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
116  factory.getInInterceptors().add(new LoggingInInterceptor());
117  factory.getOutInterceptors().add(new LoggingOutInterceptor());
118  factory.setServiceClass(Greeter.class);
119  factory.setAddress("http://localhost:9000/SoapContext/SoapPort");
120  Greeter client = (Greeter) factory.create();
121 
122  client.greetMeOneWay("abc");
123 
124  synchronized(server){
125  server.wait();
126  }
127 
128  }
129 
130  /**
131  * @throws Exception
132  */
133  @Test
135 
136  Bus bus = BusFactory.getDefaultBus();
137 
138  GreeterImpl implementor = new GreeterImpl();
139  JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
140  svrFactory.setServiceClass(Greeter.class);
141  svrFactory.setAddress("http://localhost:9001/SoapContext/SoapPort");
142  svrFactory.setServiceBean(implementor);
143  //in interceptors
144  bus.getInInterceptors().add(new LoggingInInterceptor());
145  bus.getInInterceptors().add(new BasicAuthInterceptor());
146  Map<String, Object> properties = new HashMap<>();
147  properties.put("action", "UsernameToken");
148  properties.put("passwordType", "PasswordText");
149  properties.put("passwordCallbackRef", new SecurityContextCallback());
150  bus.getInInterceptors().add(new WSSecurityInterceptor(properties));
151  Map<String, Object> wssParams = new HashMap<>();
152  wssParams.put("ws-security.ut.validator", new SampleUsernameTokenValidator());
153  wssParams.put("ws-security.validate.token", "true");
154  bus.setProperties(wssParams);
155  // out normal response interceptor
156  bus.getOutInterceptors().add(new LoggingOutInterceptor());
157  bus.getOutInterceptors().add(new SoapOutSecurityInterceptor());
158  //out fault interceptor
159  bus.getOutFaultInterceptors().add(new SoapOutSecurityFaultInterceptor());
160 
161  org.apache.cxf.endpoint.Server server = svrFactory.create();
162  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
163  System.out.println("Server started at " + endpoint);
164 
165  //client
166  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
167  factory.getInInterceptors().add(new LoggingInInterceptor());
168  factory.getOutInterceptors().add(new LoggingOutInterceptor());
169  factory.setServiceClass(Greeter.class);
170  factory.setAddress("http://localhost:9000/SoapContext/SoapPort");
171  Greeter client = (Greeter) factory.create();
172 
173  client.greetMeOneWay("abc");
174 
175  synchronized(server){
176  server.wait();
177  }
178 
179  }
180 
181  /**
182  * @throws Exception
183  */
184  @Test
185  public void start_server_with_2_way_ssl() throws Exception{
186  GreeterImpl implementor = new GreeterImpl();
187  JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
188  svrFactory.setServiceClass(Greeter.class);
189  svrFactory.setAddress("https://localhost:9001/SoapContext/SoapPort");
190  svrFactory.setServiceBean(implementor);
191 
192  //in interceptors
193  //Order of interceptors is decided by phases
194  //RECEIVE -> (PRE/USER/POST)_PROTOCOL (This could have muliple interceptors) -> UNMARSHAL
195  svrFactory.getInInterceptors().add(new LoggingInInterceptor());
196  svrFactory.getInInterceptors().add(new BasicAuthInterceptor());
197  Map<String, Object> properties = new HashMap<>();
198  properties.put("action", "UsernameToken");
199  properties.put("passwordType", "PasswordText");
200  properties.put("passwordCallbackRef", new SecurityContextCallback());
201  svrFactory.getInInterceptors().add(new WSSecurityInterceptor(properties));
202  Map<String, Object> wssParams = new HashMap<>();
203  wssParams.put("ws-security.ut.validator", new SampleUsernameTokenValidator());
204  wssParams.put("ws-security.validate.token", "true");
205  svrFactory.setProperties(wssParams);
206  svrFactory.getInInterceptors().add(new SSLInterceptor());
207  // out normal response interceptor
208  svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
209  svrFactory.getOutInterceptors().add(new SoapOutSecurityInterceptor());
210  //out fault interceptor
211  svrFactory.getOutFaultInterceptors().add(new SoapOutSecurityFaultInterceptor());
212 
213  svrFactory = configureSSLOnTheServer(svrFactory, 9001);
214  org.apache.cxf.endpoint.Server server = svrFactory.create();
215  String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
216  System.out.println("Server started at " + endpoint);
217  synchronized(server){
218  server.wait();
219  }
220  }
221 
222 
223  private JaxWsServerFactoryBean configureSSLOnTheServer(JaxWsServerFactoryBean sf, int port) {
224  try {
225  System.setProperty("javax.net.debug", "ssl:handshake");
226  TLSServerParameters tlsParams = new TLSServerParameters();
227  KeyStore keyStore = KeyStore.getInstance("JKS");
228  String password = "password";
229  File keystoreFile = new File("src\\com\\freemindcafe\\apache\\cxf\\wsdl\\serverkeystore.jks");
230  keyStore.load(new FileInputStream(keystoreFile), password.toCharArray());
231  KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
232  keyFactory.init(keyStore, password.toCharArray());
233  KeyManager[] km = keyFactory.getKeyManagers();
234  tlsParams.setKeyManagers(km);
235 
236  File truststoreFile = new File("src\\com\\freemindcafe\\apache\\cxf\\wsdl\\serverkeystore.jks");
237  keyStore.load(new FileInputStream(truststoreFile), password.toCharArray());
238  TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
239  trustFactory.init(keyStore);
240  TrustManager[] tm = trustFactory.getTrustManagers();
241  tlsParams.setTrustManagers(tm);
242 // FiltersType filter = new FiltersType();
243 // filter.getInclude().add(".*_EXPORT_.*");
244 // filter.getInclude().add(".*_EXPORT1024_.*");
245 // filter.getInclude().add(".*_WITH_DES_.*");
246 // filter.getInclude().add(".*_WITH_NULL_.*");
247 // filter.getExclude().add(".*_DH_anon_.*");
248 // tlsParams.setCipherSuitesFilter(filter);
249  ClientAuthentication ca = new ClientAuthentication();
250  ca.setRequired(true);
251  ca.setWant(true);
252  tlsParams.setClientAuthentication(ca);
253  JettyHTTPServerEngineFactory factory = new JettyHTTPServerEngineFactory();
254  factory.setTLSServerParametersForPort(port, tlsParams);
255  } catch (KeyStoreException kse) {
256  System.out.println("Security configuration failed with the following: " + kse.getCause());
257  } catch (NoSuchAlgorithmException nsa) {
258  System.out.println("Security configuration failed with the following: " + nsa.getCause());
259  } catch (FileNotFoundException fnfe) {
260  System.out.println("Security configuration failed with the following: " + fnfe.getCause());
261  } catch (UnrecoverableKeyException uke) {
262  System.out.println("Security configuration failed with the following: " + uke.getCause());
263  } catch (CertificateException ce) {
264  System.out.println("Security configuration failed with the following: " + ce.getCause());
265  } catch (GeneralSecurityException gse) {
266  System.out.println("Security configuration failed with the following: " + gse.getCause());
267  } catch (IOException ioe) {
268  System.out.println("Security configuration failed with the following: " + ioe.getCause());
269  }
270 
271  return sf;
272  }
273 
274 }
void start_server_without_ssl_and_use_client_in_same_jvm_with_interceptors_on_endpoint()
Definition: Server.java:85
void start_server_without_ssl_and_use_client_in_same_jvm_with_interceptors_on_bus()
Definition: Server.java:134