22
33import de .taimos .dvalin .jaxrs .websocket .WebSocketContextHandler ;
44import org .apache .cxf .Bus ;
5+ import org .apache .cxf .configuration .jsse .TLSServerParameters ;
56import org .apache .cxf .jaxrs .JAXRSServerFactoryBean ;
67import org .apache .cxf .transport .http_jetty .JettyHTTPServerEngine ;
78import org .apache .cxf .transport .http_jetty .JettyHTTPServerEngineFactory ;
1920import org .springframework .context .annotation .ImportResource ;
2021import org .springframework .core .annotation .Order ;
2122
23+ import javax .net .ssl .KeyManagerFactory ;
24+ import javax .net .ssl .TrustManagerFactory ;
25+ import java .io .File ;
2226import java .io .IOException ;
2327import java .lang .annotation .Annotation ;
28+ import java .nio .file .Files ;
2429import java .security .GeneralSecurityException ;
30+ import java .security .KeyStore ;
31+ import java .security .KeyStoreException ;
32+ import java .security .NoSuchAlgorithmException ;
33+ import java .security .UnrecoverableKeyException ;
34+ import java .security .cert .CertificateException ;
2535import java .util .List ;
2636
2737/**
@@ -43,6 +53,14 @@ public class JAXRSServerConfig {
4353 protected int port ;
4454 @ Value ("${jaxrs.protocol:http}" )
4555 protected String protocol ;
56+
57+ @ Value ("${jaxrs.server.keyStore:}" )
58+ protected String keyStorePath ;
59+ @ Value ("${jaxrs.server.keyStorePassword:}" )
60+ protected String keyStorePassword ;
61+ @ Value ("${jaxrs.server.keyStoreType:JKS}" )
62+ protected String keyStoreType ;
63+
4664 @ Value ("${jetty.minThreads:5}" )
4765 protected int minThreads ;
4866 @ Value ("${jetty.maxThreads:150}" )
@@ -78,6 +96,9 @@ public JettyHTTPServerEngineFactory serverEngineFactory(Bus cxf, //
7896 }
7997
8098 protected void createServerEngine (JettyHTTPServerEngineFactory factory , List <Handler > handlers ) throws GeneralSecurityException , IOException {
99+ if (this .protocol .equals ("https" )) {
100+ factory .setTLSServerParametersForPort (this .port , this .createTLSServerParameters ());
101+ }
81102 JettyHTTPServerEngine engine = factory .createJettyHTTPServerEngine (this .host , this .port , this .protocol );
82103 engine .setThreadingParameters (this .createThreadingParameters ());
83104 engine .setSendServerVersion (this .sendVersion );
@@ -92,6 +113,20 @@ protected ThreadingParameters createThreadingParameters() {
92113 return threadingParams ;
93114 }
94115
116+ protected TLSServerParameters createTLSServerParameters () throws KeyStoreException , NoSuchAlgorithmException , IOException , CertificateException , UnrecoverableKeyException {
117+ TLSServerParameters tlsParams = new TLSServerParameters ();
118+ KeyStore keyStore = KeyStore .getInstance (this .keyStoreType );
119+ keyStore .load (Files .newInputStream (new File (this .keyStorePath ).toPath ()), this .keyStorePassword .toCharArray ());
120+ KeyManagerFactory keyFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
121+ keyFactory .init (keyStore , this .keyStorePassword .toCharArray ());
122+ tlsParams .setKeyManagers (keyFactory .getKeyManagers ());
123+
124+ TrustManagerFactory trustFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
125+ trustFactory .init (keyStore );
126+ tlsParams .setTrustManagers (trustFactory .getTrustManagers ());
127+ return tlsParams ;
128+ }
129+
95130 protected ContextHandler createResourceContext (String contextPath , Resource base ) {
96131 ContextHandler context = new ContextHandler (contextPath );
97132 ResourceHandler res = new ResourceHandler ();
@@ -103,19 +138,19 @@ protected ContextHandler createResourceContext(String contextPath, Resource base
103138 @ Order (1 )
104139 @ Bean (name = "web-server-context-static" )
105140 public ContextHandler staticContextHandler () throws IOException {
106- return createResourceContext ("/static" , Resource .newResource ("./static" ));
141+ return this . createResourceContext ("/static" , Resource .newResource ("./static" ));
107142 }
108143
109144 @ Order (2 )
110145 @ Bean (name = "web-server-context-web-fs" )
111146 public ContextHandler webFSContextHandler () throws IOException {
112- return createResourceContext ("/" , Resource .newResource ("./web" ));
147+ return this . createResourceContext ("/" , Resource .newResource ("./web" ));
113148 }
114149
115150 @ Order (3 )
116151 @ Bean (name = "web-server-context-web" )
117152 public ContextHandler webContextHandler () {
118- return createResourceContext ("/" , Resource .newClassPathResource ("/web" ));
153+ return this . createResourceContext ("/" , Resource .newClassPathResource ("/web" ));
119154 }
120155
121156 @ Order (10 )
@@ -129,4 +164,4 @@ public WebSocketContextHandler websocketContextHandler() {
129164 public DefaultHandler defaultHandler () {
130165 return new DefaultHandler ();
131166 }
132- }
167+ }
0 commit comments