Skip to content

Commit 99d7ddc

Browse files
rozhkoperwendel
authored andcommitted
Fix #506 (#971)
1 parent bfda598 commit 99d7ddc

File tree

4 files changed

+139
-12
lines changed

4 files changed

+139
-12
lines changed

src/main/java/spark/Service.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,58 @@ public synchronized Service secure(String keystoreFile,
180180
String keystorePassword,
181181
String truststoreFile,
182182
String truststorePassword) {
183-
return secure(keystoreFile, keystorePassword, truststoreFile, truststorePassword, false);
183+
return secure(keystoreFile, keystorePassword, null, truststoreFile, truststorePassword, false);
184+
}
185+
186+
/**
187+
* Set the connection to be secure, using the specified keystore and
188+
* truststore. This has to be called before any route mapping is done. You
189+
* have to supply a keystore file, truststore file is optional (keystore
190+
* will be reused). By default, client certificates are not checked.
191+
* This method is only relevant when using embedded Jetty servers. It should
192+
* not be used if you are using Servlets, where you will need to secure the
193+
* connection in the servlet container
194+
*
195+
* @param keystoreFile The keystore file location as string
196+
* @param keystorePassword the password for the keystore
197+
* @param certAlias the default certificate Alias
198+
* @param truststoreFile the truststore file location as string, leave null to reuse
199+
* keystore
200+
* @param truststorePassword the trust store password
201+
* @return the object with connection set to be secure
202+
*/
203+
public synchronized Service secure(String keystoreFile,
204+
String keystorePassword,
205+
String certAlias,
206+
String truststoreFile,
207+
String truststorePassword) {
208+
return secure(keystoreFile, keystorePassword, certAlias, truststoreFile, truststorePassword, false);
209+
}
210+
211+
/**
212+
* Set the connection to be secure, using the specified keystore and
213+
* truststore. This has to be called before any route mapping is done. You
214+
* have to supply a keystore file, truststore file is optional (keystore
215+
* will be reused).
216+
* This method is only relevant when using embedded Jetty servers. It should
217+
* not be used if you are using Servlets, where you will need to secure the
218+
* connection in the servlet container
219+
*
220+
* @param keystoreFile The keystore file location as string
221+
* @param keystorePassword the password for the keystore
222+
* @param truststoreFile the truststore file location as string, leave null to reuse
223+
* keystore
224+
* @param needsClientCert Whether to require client certificate to be supplied in
225+
* request
226+
* @param truststorePassword the trust store password
227+
* @return the object with connection set to be secure
228+
*/
229+
public synchronized Service secure(String keystoreFile,
230+
String keystorePassword,
231+
String truststoreFile,
232+
String truststorePassword,
233+
boolean needsClientCert) {
234+
return secure(keystoreFile, keystorePassword, null, truststoreFile, truststorePassword, needsClientCert);
184235
}
185236

186237
/**
@@ -194,6 +245,7 @@ public synchronized Service secure(String keystoreFile,
194245
*
195246
* @param keystoreFile The keystore file location as string
196247
* @param keystorePassword the password for the keystore
248+
* @param certAlias the default certificate Alias
197249
* @param truststoreFile the truststore file location as string, leave null to reuse
198250
* keystore
199251
* @param needsClientCert Whether to require client certificate to be supplied in
@@ -203,6 +255,7 @@ public synchronized Service secure(String keystoreFile,
203255
*/
204256
public synchronized Service secure(String keystoreFile,
205257
String keystorePassword,
258+
String certAlias,
206259
String truststoreFile,
207260
String truststorePassword,
208261
boolean needsClientCert) {
@@ -215,7 +268,7 @@ public synchronized Service secure(String keystoreFile,
215268
"Must provide a keystore file to run secured");
216269
}
217270

218-
sslStores = SslStores.create(keystoreFile, keystorePassword, truststoreFile, truststorePassword, needsClientCert);
271+
sslStores = SslStores.create(keystoreFile, keystorePassword, certAlias, truststoreFile, truststorePassword, needsClientCert);
219272
return this;
220273
}
221274

src/main/java/spark/Spark.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,30 @@ public static void secure(String keystoreFile,
10301030
getInstance().secure(keystoreFile, keystorePassword, truststoreFile, truststorePassword);
10311031
}
10321032

1033+
/**
1034+
* Set the connection to be secure, using the specified keystore and
1035+
* truststore. This has to be called before any route mapping is done. You
1036+
* have to supply a keystore file, truststore file is optional (keystore
1037+
* will be reused).
1038+
* This method is only relevant when using embedded Jetty servers. It should
1039+
* not be used if you are using Servlets, where you will need to secure the
1040+
* connection in the servlet container
1041+
*
1042+
* @param keystoreFile The keystore file location as string
1043+
* @param keystorePassword the password for the keystore
1044+
* @param certAlias the default certificate Alias
1045+
* @param truststoreFile the truststore file location as string, leave null to reuse
1046+
* keystore
1047+
* @param truststorePassword the trust store password
1048+
*/
1049+
public static void secure(String keystoreFile,
1050+
String keystorePassword,
1051+
String certAlias,
1052+
String truststoreFile,
1053+
String truststorePassword) {
1054+
getInstance().secure(keystoreFile, keystorePassword, certAlias, truststoreFile, truststorePassword);
1055+
}
1056+
10331057
/**
10341058
* Overrides default exception handler during initialization phase
10351059
*
@@ -1064,6 +1088,33 @@ public static void secure(String keystoreFile,
10641088
getInstance().secure(keystoreFile, keystorePassword, truststoreFile, truststorePassword, needsClientCert);
10651089
}
10661090

1091+
/**
1092+
* Set the connection to be secure, using the specified keystore and
1093+
* truststore. This has to be called before any route mapping is done. You
1094+
* have to supply a keystore file, truststore file is optional (keystore
1095+
* will be reused).
1096+
* This method is only relevant when using embedded Jetty servers. It should
1097+
* not be used if you are using Servlets, where you will need to secure the
1098+
* connection in the servlet container
1099+
*
1100+
* @param keystoreFile The keystore file location as string
1101+
* @param keystorePassword the password for the keystore
1102+
* @param certAlias the default certificate Alias
1103+
* @param truststoreFile the truststore file location as string, leave null to reuse
1104+
* keystore
1105+
* @param needsClientCert Whether to require client certificate to be supplied in
1106+
* request
1107+
* @param truststorePassword the trust store password
1108+
*/
1109+
public static void secure(String keystoreFile,
1110+
String keystorePassword,
1111+
String certAlias,
1112+
String truststoreFile,
1113+
String truststorePassword,
1114+
boolean needsClientCert) {
1115+
getInstance().secure(keystoreFile, keystorePassword, certAlias, truststoreFile, truststorePassword, needsClientCert);
1116+
}
1117+
10671118
/**
10681119
* Configures the embedded web server's thread pool.
10691120
*

src/main/java/spark/embeddedserver/jetty/SocketConnectorFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public static ServerConnector createSecureSocketConnector(Server server,
7575
sslContextFactory.setKeyStorePassword(sslStores.keystorePassword());
7676
}
7777

78+
if (sslStores.certAlias() != null) {
79+
sslContextFactory.setCertAlias(sslStores.certAlias());
80+
}
81+
7882
if (sslStores.trustStoreFile() != null) {
7983
sslContextFactory.setTrustStorePath(sslStores.trustStoreFile());
8084
}

src/main/java/spark/ssl/SslStores.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class SslStores {
2323

2424
protected String keystoreFile;
2525
protected String keystorePassword;
26+
protected String certAlias;
2627
protected String truststoreFile;
2728
protected String truststorePassword;
2829
protected boolean needsClientCert;
@@ -41,7 +42,16 @@ public static SslStores create(String keystoreFile,
4142
String truststoreFile,
4243
String truststorePassword) {
4344

44-
return new SslStores(keystoreFile, keystorePassword, truststoreFile, truststorePassword);
45+
return new SslStores(keystoreFile, keystorePassword, null, truststoreFile, truststorePassword, false);
46+
}
47+
48+
public static SslStores create(String keystoreFile,
49+
String keystorePassword,
50+
String certAlias,
51+
String truststoreFile,
52+
String truststorePassword) {
53+
54+
return new SslStores(keystoreFile, keystorePassword, certAlias, truststoreFile, truststorePassword, false);
4555
}
4656

4757
public static SslStores create(String keystoreFile,
@@ -50,26 +60,28 @@ public static SslStores create(String keystoreFile,
5060
String truststorePassword,
5161
boolean needsClientCert) {
5262

53-
return new SslStores(keystoreFile, keystorePassword, truststoreFile, truststorePassword, needsClientCert);
63+
return new SslStores(keystoreFile, keystorePassword, null, truststoreFile, truststorePassword, needsClientCert);
5464
}
5565

56-
private SslStores(String keystoreFile,
57-
String keystorePassword,
58-
String truststoreFile,
59-
String truststorePassword) {
60-
this.keystoreFile = keystoreFile;
61-
this.keystorePassword = keystorePassword;
62-
this.truststoreFile = truststoreFile;
63-
this.truststorePassword = truststorePassword;
66+
public static SslStores create(String keystoreFile,
67+
String keystorePassword,
68+
String certAlias,
69+
String truststoreFile,
70+
String truststorePassword,
71+
boolean needsClientCert) {
72+
73+
return new SslStores(keystoreFile, keystorePassword, certAlias, truststoreFile, truststorePassword, needsClientCert);
6474
}
6575

6676
private SslStores(String keystoreFile,
6777
String keystorePassword,
78+
String certAlias,
6879
String truststoreFile,
6980
String truststorePassword,
7081
boolean needsClientCert) {
7182
this.keystoreFile = keystoreFile;
7283
this.keystorePassword = keystorePassword;
84+
this.certAlias = certAlias;
7385
this.truststoreFile = truststoreFile;
7486
this.truststorePassword = truststorePassword;
7587
this.needsClientCert = needsClientCert;
@@ -89,6 +101,13 @@ public String keystorePassword() {
89101
return keystorePassword;
90102
}
91103

104+
/**
105+
* @return certAlias
106+
*/
107+
public String certAlias() {
108+
return certAlias;
109+
}
110+
92111
/**
93112
* @return trustStoreFile
94113
*/

0 commit comments

Comments
 (0)