1 package com.freemindcafe.apache.cxf.wsdl.sample3;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
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;
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;
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;
50 JaxWsServerFactoryBean svrFactory =
new JaxWsServerFactoryBean();
51 svrFactory.setServiceClass(
Greeter.class);
52 svrFactory.setAddress(
"http://localhost:9001/SoapContext/SoapPort");
53 svrFactory.setServiceBean(implementor);
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<>();
64 wssParams.put(
"ws-security.validate.token",
"true");
65 svrFactory.setProperties(wssParams);
67 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
68 svrFactory.getOutInterceptors().add(
new SoapOutSecurityInterceptor());
70 svrFactory.getOutFaultInterceptors().add(
new SoapOutSecurityFaultInterceptor());
72 org.
apache.cxf.endpoint.Server server = svrFactory.create();
73 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
74 System.out.println(
"Server started at " + endpoint);
88 JaxWsServerFactoryBean svrFactory =
new JaxWsServerFactoryBean();
89 svrFactory.setServiceClass(
Greeter.class);
90 svrFactory.setAddress(
"http://localhost:9001/SoapContext/SoapPort");
91 svrFactory.setServiceBean(implementor);
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<>();
102 wssParams.put(
"ws-security.validate.token",
"true");
103 svrFactory.setProperties(wssParams);
105 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
106 svrFactory.getOutInterceptors().add(
new SoapOutSecurityInterceptor());
108 svrFactory.getOutFaultInterceptors().add(
new SoapOutSecurityFaultInterceptor());
110 org.
apache.cxf.endpoint.Server server = svrFactory.create();
111 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
112 System.out.println(
"Server started at " + endpoint);
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");
122 client.greetMeOneWay(
"abc");
124 synchronized(server){
136 Bus bus = BusFactory.getDefaultBus();
139 JaxWsServerFactoryBean svrFactory =
new JaxWsServerFactoryBean();
140 svrFactory.setServiceClass(
Greeter.class);
141 svrFactory.setAddress(
"http://localhost:9001/SoapContext/SoapPort");
142 svrFactory.setServiceBean(implementor);
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<>();
153 wssParams.put(
"ws-security.validate.token",
"true");
154 bus.setProperties(wssParams);
156 bus.getOutInterceptors().add(
new LoggingOutInterceptor());
157 bus.getOutInterceptors().add(
new SoapOutSecurityInterceptor());
159 bus.getOutFaultInterceptors().add(
new SoapOutSecurityFaultInterceptor());
161 org.
apache.cxf.endpoint.Server server = svrFactory.create();
162 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
163 System.out.println(
"Server started at " + endpoint);
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");
173 client.greetMeOneWay(
"abc");
175 synchronized(server){
187 JaxWsServerFactoryBean svrFactory =
new JaxWsServerFactoryBean();
188 svrFactory.setServiceClass(
Greeter.class);
189 svrFactory.setAddress(
"https://localhost:9001/SoapContext/SoapPort");
190 svrFactory.setServiceBean(implementor);
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<>();
204 wssParams.put(
"ws-security.validate.token",
"true");
205 svrFactory.setProperties(wssParams);
206 svrFactory.getInInterceptors().add(
new SSLInterceptor());
208 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
209 svrFactory.getOutInterceptors().add(
new SoapOutSecurityInterceptor());
211 svrFactory.getOutFaultInterceptors().add(
new SoapOutSecurityFaultInterceptor());
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){
223 private JaxWsServerFactoryBean configureSSLOnTheServer(JaxWsServerFactoryBean sf,
int port) {
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);
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);
249 ClientAuthentication ca =
new ClientAuthentication();
250 ca.setRequired(
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());
void start_server_without_ssl_and_use_client_in_same_jvm_with_interceptors_on_endpoint()
void start_server_without_ssl_and_use_client_in_same_jvm_with_interceptors_on_bus()
void start_server_without_ssl()
void start_server_with_2_way_ssl()