Skip to content

Commit 6ce0b80

Browse files
authored
Merge pull request #180 from marc92w/feature/jaxrs-tls-config
Feature: JAX-RS TLS server configuration
2 parents f78dfaf + b7b0b0a commit 6ce0b80

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Bugfix: Better error handling in CloudconductorPropertyProvider
2424
* Fixed vulnerabilities: CVE-2024-13009(Jetty), CVE-2025-23184(Apache CXF), CVE-2024-57699 (Json-smart),CVE-2025-27533 (ActiveMQ)
2525
* Logging improvement and extension options for DaemonMessageListener
26+
* Add TLS server parameters for JAX-RS
2627
* Improved Errormessage in case of non parseable JSON strings in the space of InterconnectObjects and Messsaging.
2728

2829

jaxrs-swagger/src/main/java/de/taimos/dvalin/jaxrs/swagger/OpenAPIProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected boolean hasAnnotation(Class<?> clz, Class<? extends Annotation> ann) {
6666

6767
protected void configureServerURL(OpenAPI openAPI) {
6868
String port = System.getProperty(SpringCXFProperties.JAXRS_BINDPORT, System.getProperty("svc.port", "8080"));
69-
String serverUrl = System.getProperty(SpringCXFProperties.SERVER_URL, "http://localhost:" + port);
69+
String serverUrl = System.getProperty(SpringCXFProperties.SERVER_URL, System.getProperty("jaxrs.protocol", "http") + "://localhost:" + port);
7070
String path = System.getProperty(SpringCXFProperties.JAXRS_PATH, "");
7171
if (!path.startsWith("/")) {
7272
serverUrl += "/";

jaxrs/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ password hashes secured by the SHA-512 function using a 512 bit salt and a dynam
6464
Several settings of the dvalin framework can be customized using system properties which are described
6565
in `de.taimos.dvalin.jaxrs.SpringCXFProperties`.
6666

67+
For HTTPS, configure protocol and KeyStore via system properties:
68+
```
69+
jaxrs.protocol=https
70+
jaxrs.server.keyStore=/path/to/keystore.jks
71+
jaxrs.server.keyStorePassword=password
72+
jaxrs.server.keyStoreType=JKS
73+
```
74+
6775
### Testing
6876

6977
moved to `test` sub-project.

jaxrs/src/main/java/de/taimos/dvalin/jaxrs/JAXRSServerConfig.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import de.taimos.dvalin.jaxrs.websocket.WebSocketContextHandler;
44
import org.apache.cxf.Bus;
5+
import org.apache.cxf.configuration.jsse.TLSServerParameters;
56
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
67
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine;
78
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
@@ -19,9 +20,18 @@
1920
import org.springframework.context.annotation.ImportResource;
2021
import org.springframework.core.annotation.Order;
2122

23+
import javax.net.ssl.KeyManagerFactory;
24+
import javax.net.ssl.TrustManagerFactory;
25+
import java.io.File;
2226
import java.io.IOException;
2327
import java.lang.annotation.Annotation;
28+
import java.nio.file.Files;
2429
import 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;
2535
import 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+
}

jaxrs/src/main/java/de/taimos/dvalin/jaxrs/context/JAXRSContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
@Component
4848
public class JAXRSContextImpl implements DvalinRSContext {
4949

50-
@Value("${server.url:http://localhost:${jaxrs.bindport:${svc.port:8080}}}")
50+
@Value("${server.url:${jaxrs.protocol:http}://localhost:${jaxrs.bindport:${svc.port:8080}}}")
5151
private String serverURL;
5252

5353
@Override

jaxrs/src/main/resources/spring/jaxrs-server.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2222
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
2323
xsi:schemaLocation="
24-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
25-
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"
24+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
25+
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"
2626
profile="http">
2727

28-
<jaxrs:server id="restService" address="http://${jaxrs.bindhost:0.0.0.0}:${jaxrs.bindport:${svc.port:8080}}${jaxrs.path:}"
28+
<jaxrs:server id="restService" address="${jaxrs.protocol:http}://${jaxrs.bindhost:0.0.0.0}:${jaxrs.bindport:${svc.port:8080}}${jaxrs.path:}"
2929
depends-on="cxf-engine" serviceAnnotation="${jaxrs.annotation:de.taimos.dvalin.jaxrs.JaxRsComponent}">
3030
</jaxrs:server>
3131

0 commit comments

Comments
 (0)