1 package com.freemindcafe.apache.cxf.wsdl.sample1;
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));
63 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
64 svrFactory.getOutInterceptors().add(
new SoapOutSecurityInterceptor());
66 svrFactory.getOutFaultInterceptors().add(
new SoapOutSecurityFaultInterceptor());
68 org.
apache.cxf.endpoint.Server server = svrFactory.create();
69 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
70 System.out.println(
"Server started at " + endpoint);
84 JaxWsServerFactoryBean svrFactory =
new JaxWsServerFactoryBean();
85 svrFactory.setServiceClass(
Greeter.class);
86 svrFactory.setAddress(
"http://localhost:9001/SoapContext/SoapPort");
87 svrFactory.setServiceBean(implementor);
89 svrFactory.getInInterceptors().add(
new LoggingInInterceptor());
90 svrFactory.getInInterceptors().add(
new BasicAuthInterceptor());
91 Map<String, Object> properties =
new HashMap<>();
92 properties.put(
"action",
"UsernameToken");
93 properties.put(
"passwordType",
"PasswordText");
94 properties.put(
"passwordCallbackRef",
new SecurityContextCallback());
95 svrFactory.getInInterceptors().add(
new WSSecurityInterceptor(properties));
97 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
98 svrFactory.getOutInterceptors().add(
new SoapOutSecurityInterceptor());
100 svrFactory.getOutFaultInterceptors().add(
new SoapOutSecurityFaultInterceptor());
102 org.
apache.cxf.endpoint.Server server = svrFactory.create();
103 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
104 System.out.println(
"Server started at " + endpoint);
107 JaxWsProxyFactoryBean factory =
new JaxWsProxyFactoryBean();
108 factory.getInInterceptors().add(
new LoggingInInterceptor());
109 factory.getOutInterceptors().add(
new LoggingOutInterceptor());
110 factory.setServiceClass(
Greeter.class);
111 factory.setAddress(
"http://localhost:9000/SoapContext/SoapPort");
114 client.greetMeOneWay(
"abc");
116 synchronized(server){
128 Bus bus = BusFactory.getDefaultBus();
131 JaxWsServerFactoryBean svrFactory =
new JaxWsServerFactoryBean();
132 svrFactory.setServiceClass(
Greeter.class);
133 svrFactory.setAddress(
"http://localhost:9001/SoapContext/SoapPort");
134 svrFactory.setServiceBean(implementor);
136 bus.getInInterceptors().add(
new LoggingInInterceptor());
137 bus.getInInterceptors().add(
new BasicAuthInterceptor());
138 Map<String, Object> properties =
new HashMap<>();
139 properties.put(
"action",
"UsernameToken");
140 properties.put(
"passwordType",
"PasswordText");
141 properties.put(
"passwordCallbackRef",
new SecurityContextCallback());
142 bus.getInInterceptors().add(
new WSSecurityInterceptor(properties));
144 bus.getOutInterceptors().add(
new LoggingOutInterceptor());
145 bus.getOutInterceptors().add(
new SoapOutSecurityInterceptor());
147 bus.getOutFaultInterceptors().add(
new SoapOutSecurityFaultInterceptor());
149 org.
apache.cxf.endpoint.Server server = svrFactory.create();
150 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
151 System.out.println(
"Server started at " + endpoint);
154 JaxWsProxyFactoryBean factory =
new JaxWsProxyFactoryBean();
155 factory.getInInterceptors().add(
new LoggingInInterceptor());
156 factory.getOutInterceptors().add(
new LoggingOutInterceptor());
157 factory.setServiceClass(
Greeter.class);
158 factory.setAddress(
"http://localhost:9000/SoapContext/SoapPort");
161 client.greetMeOneWay(
"abc");
163 synchronized(server){
180 JaxWsServerFactoryBean svrFactory =
new JaxWsServerFactoryBean();
181 svrFactory.setServiceClass(
Greeter.class);
182 svrFactory.setAddress(
"https://localhost:9001/SoapContext/SoapPort");
183 svrFactory.setServiceBean(implementor);
188 svrFactory.getInInterceptors().add(
new LoggingInInterceptor());
189 svrFactory.getInInterceptors().add(
new BasicAuthInterceptor());
190 Map<String, Object> properties =
new HashMap<>();
191 properties.put(
"action",
"UsernameToken");
192 properties.put(
"passwordType",
"PasswordText");
193 properties.put(
"passwordCallbackRef",
new SecurityContextCallback());
194 svrFactory.getInInterceptors().add(
new WSSecurityInterceptor(properties));
195 svrFactory.getInInterceptors().add(
new SSLInterceptor());
197 svrFactory.getOutInterceptors().add(
new LoggingOutInterceptor());
198 svrFactory.getOutInterceptors().add(
new SoapOutSecurityInterceptor());
200 svrFactory.getOutFaultInterceptors().add(
new SoapOutSecurityFaultInterceptor());
202 svrFactory = configureSSLOnTheServer(svrFactory, 9001);
203 org.
apache.cxf.endpoint.Server server = svrFactory.create();
204 String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
205 System.out.println(
"Server started at " + endpoint);
206 synchronized(server){
212 private JaxWsServerFactoryBean configureSSLOnTheServer(JaxWsServerFactoryBean sf,
int port) {
214 System.setProperty(
"javax.net.debug",
"ssl:handshake");
215 TLSServerParameters tlsParams =
new TLSServerParameters();
216 KeyStore keyStore = KeyStore.getInstance(
"JKS");
217 String password =
"password";
218 File keystoreFile =
new File(
"src\\com\\freemindcafe\\apache\\cxf\\wsdl\\sample1\\serverkeystore.jks");
219 keyStore.load(
new FileInputStream(keystoreFile), password.toCharArray());
220 KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
221 keyFactory.init(keyStore, password.toCharArray());
222 KeyManager[] km = keyFactory.getKeyManagers();
223 tlsParams.setKeyManagers(km);
225 File truststoreFile =
new File(
"src\\com\\freemindcafe\\apache\\cxf\\wsdl\\sample1\\serverkeystore.jks");
226 keyStore.load(
new FileInputStream(truststoreFile), password.toCharArray());
227 TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
228 trustFactory.init(keyStore);
229 TrustManager[] tm = trustFactory.getTrustManagers();
230 tlsParams.setTrustManagers(tm);
238 ClientAuthentication ca =
new ClientAuthentication();
239 ca.setRequired(
true);
241 tlsParams.setClientAuthentication(ca);
242 JettyHTTPServerEngineFactory factory =
new JettyHTTPServerEngineFactory();
243 factory.setTLSServerParametersForPort(port, tlsParams);
244 }
catch (KeyStoreException kse) {
245 System.out.println(
"Security configuration failed with the following: " + kse.getCause());
246 }
catch (NoSuchAlgorithmException nsa) {
247 System.out.println(
"Security configuration failed with the following: " + nsa.getCause());
248 }
catch (FileNotFoundException fnfe) {
249 System.out.println(
"Security configuration failed with the following: " + fnfe.getCause());
250 }
catch (UnrecoverableKeyException uke) {
251 System.out.println(
"Security configuration failed with the following: " + uke.getCause());
252 }
catch (CertificateException ce) {
253 System.out.println(
"Security configuration failed with the following: " + ce.getCause());
254 }
catch (GeneralSecurityException gse) {
255 System.out.println(
"Security configuration failed with the following: " + gse.getCause());
256 }
catch (IOException ioe) {
257 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()
void start_server_without_ssl_and_use_client_in_same_jvm_with_interceptors_on_bus()
void start_server_with_2_way_ssl()