15
15
*/
16
16
package io .micronaut .servlet .tomcat ;
17
17
18
+ import io .micronaut .context .annotation .Requires ;
19
+ import io .micronaut .core .annotation .Nullable ;
20
+ import io .micronaut .core .util .StringUtils ;
21
+ import jakarta .inject .Named ;
18
22
import java .io .File ;
19
23
import java .util .List ;
20
24
48
52
@ Factory
49
53
public class TomcatFactory extends ServletServerFactory {
50
54
55
+ private static final String HTTPS = "HTTPS" ;
51
56
private static final Logger LOG = LoggerFactory .getLogger (TomcatFactory .class );
52
57
53
58
/**
@@ -77,12 +82,16 @@ public TomcatConfiguration getServerConfiguration() {
77
82
* The Tomcat server bean.
78
83
*
79
84
* @param connector The connector
85
+ * @param httpsConnector The HTTPS connector
80
86
* @param configuration The servlet configuration
81
87
* @return The Tomcat server
82
88
*/
83
89
@ Singleton
84
90
@ Primary
85
- protected Tomcat tomcatServer (Connector connector , MicronautServletConfiguration configuration ) {
91
+ protected Tomcat tomcatServer (
92
+ Connector connector ,
93
+ @ Named (HTTPS ) @ Nullable Connector httpsConnector ,
94
+ MicronautServletConfiguration configuration ) {
86
95
configuration .setAsyncFileServingEnabled (false );
87
96
Tomcat tomcat = new Tomcat ();
88
97
tomcat .setHostname (getConfiguredHost ());
@@ -118,56 +127,15 @@ protected Tomcat tomcatServer(Connector connector, MicronautServletConfiguration
118
127
configuration .getMultipartConfigElement ()
119
128
.ifPresent (servlet ::setMultipartConfigElement );
120
129
121
- SslConfiguration sslConfiguration = getSslConfiguration ();
122
- if (sslConfiguration .isEnabled ()) {
123
- String protocol = sslConfiguration .getProtocol ().orElse ("TLS" );
124
- int sslPort = sslConfiguration .getPort ();
125
- if (sslPort == SslConfiguration .DEFAULT_PORT && getEnvironment ().getActiveNames ().contains (Environment .TEST )) {
126
- sslPort = 0 ;
127
- }
128
- Connector httpsConnector = new Connector ();
129
- SSLHostConfig sslHostConfig = new SSLHostConfig ();
130
- SSLHostConfigCertificate certificate = new SSLHostConfigCertificate (sslHostConfig , SSLHostConfigCertificate .Type .UNDEFINED );
131
- sslHostConfig .addCertificate (certificate );
132
- httpsConnector .addSslHostConfig (sslHostConfig );
133
- httpsConnector .setPort (sslPort );
134
- httpsConnector .setSecure (true );
135
- httpsConnector .setScheme ("https" );
136
- httpsConnector .setProperty ("clientAuth" , "false" );
137
- httpsConnector .setProperty ("sslProtocol" , protocol );
138
- httpsConnector .setProperty ("SSLEnabled" , "true" );
139
- sslConfiguration .getCiphers ().ifPresent (cyphers ->
140
- sslHostConfig .setCiphers (String .join ("," , cyphers ))
141
- );
142
- sslConfiguration .getClientAuthentication ().ifPresent (ca ->
143
- httpsConnector .setProperty ("clientAuth" , ca == ClientAuthentication .WANT ? "want" : "true" )
144
- );
145
-
146
-
147
- SslConfiguration .KeyStoreConfiguration keyStoreConfig = sslConfiguration .getKeyStore ();
148
- keyStoreConfig .getPassword ().ifPresent (certificate ::setCertificateKeystorePassword );
149
- keyStoreConfig .getPath ().ifPresent (certificate ::setCertificateKeystoreFile );
150
- keyStoreConfig .getProvider ().ifPresent (certificate ::setCertificateKeystorePassword );
151
- keyStoreConfig .getType ().ifPresent (certificate ::setCertificateKeystoreType );
152
-
153
- SslConfiguration .TrustStoreConfiguration trustStore = sslConfiguration .getTrustStore ();
154
- trustStore .getPassword ().ifPresent (sslHostConfig ::setTruststorePassword );
155
- trustStore .getPath ().ifPresent (sslHostConfig ::setTruststoreFile );
156
- trustStore .getProvider ().ifPresent (sslHostConfig ::setTruststoreProvider );
157
- trustStore .getType ().ifPresent (sslHostConfig ::setTruststoreType );
158
-
159
- SslConfiguration .KeyConfiguration keyConfig = sslConfiguration .getKey ();
160
- keyConfig .getAlias ().ifPresent (certificate ::setCertificateKeyAlias );
161
- keyConfig .getPassword ().ifPresent (certificate ::setCertificateKeyPassword );
162
-
130
+ if (httpsConnector != null ) {
163
131
tomcat .getService ().addConnector (httpsConnector );
164
132
}
165
133
166
134
return tomcat ;
167
135
}
168
136
169
137
/**
170
- * @return Create the protocol .
138
+ * @return Create the connector .
171
139
*/
172
140
@ Singleton
173
141
@ Primary
@@ -177,4 +145,54 @@ protected Connector tomcatConnector() {
177
145
return tomcatConnector ;
178
146
}
179
147
180
- }
148
+ /**
149
+ * The HTTPS connector.
150
+ * @param sslConfiguration The SSL configuration.
151
+ * @return The SSL connector
152
+ */
153
+ @ Singleton
154
+ @ Named (HTTPS )
155
+ @ Requires (property = SslConfiguration .PREFIX + ".enabled" , value = StringUtils .TRUE )
156
+ protected Connector sslConnector (SslConfiguration sslConfiguration ) {
157
+ String protocol = sslConfiguration .getProtocol ().orElse ("TLS" );
158
+ int sslPort = sslConfiguration .getPort ();
159
+ if (sslPort == SslConfiguration .DEFAULT_PORT && getEnvironment ().getActiveNames ().contains (Environment .TEST )) {
160
+ sslPort = 0 ;
161
+ }
162
+ Connector httpsConnector = new Connector ();
163
+ SSLHostConfig sslHostConfig = new SSLHostConfig ();
164
+ SSLHostConfigCertificate certificate = new SSLHostConfigCertificate (sslHostConfig , SSLHostConfigCertificate .Type .UNDEFINED );
165
+ sslHostConfig .addCertificate (certificate );
166
+ httpsConnector .addSslHostConfig (sslHostConfig );
167
+ httpsConnector .setPort (sslPort );
168
+ httpsConnector .setSecure (true );
169
+ httpsConnector .setScheme ("https" );
170
+ httpsConnector .setProperty ("clientAuth" , "false" );
171
+ httpsConnector .setProperty ("sslProtocol" , protocol );
172
+ httpsConnector .setProperty ("SSLEnabled" , "true" );
173
+ sslConfiguration .getCiphers ().ifPresent (cyphers ->
174
+ sslHostConfig .setCiphers (String .join ("," , cyphers ))
175
+ );
176
+ sslConfiguration .getClientAuthentication ().ifPresent (ca ->
177
+ httpsConnector .setProperty ("clientAuth" , ca == ClientAuthentication .WANT ? "want" : "true" )
178
+ );
179
+
180
+
181
+ SslConfiguration .KeyStoreConfiguration keyStoreConfig = sslConfiguration .getKeyStore ();
182
+ keyStoreConfig .getPassword ().ifPresent (certificate ::setCertificateKeystorePassword );
183
+ keyStoreConfig .getPath ().ifPresent (certificate ::setCertificateKeystoreFile );
184
+ keyStoreConfig .getProvider ().ifPresent (certificate ::setCertificateKeystorePassword );
185
+ keyStoreConfig .getType ().ifPresent (certificate ::setCertificateKeystoreType );
186
+
187
+ SslConfiguration .TrustStoreConfiguration trustStore = sslConfiguration .getTrustStore ();
188
+ trustStore .getPassword ().ifPresent (sslHostConfig ::setTruststorePassword );
189
+ trustStore .getPath ().ifPresent (sslHostConfig ::setTruststoreFile );
190
+ trustStore .getProvider ().ifPresent (sslHostConfig ::setTruststoreProvider );
191
+ trustStore .getType ().ifPresent (sslHostConfig ::setTruststoreType );
192
+
193
+ SslConfiguration .KeyConfiguration keyConfig = sslConfiguration .getKey ();
194
+ keyConfig .getAlias ().ifPresent (certificate ::setCertificateKeyAlias );
195
+ keyConfig .getPassword ().ifPresent (certificate ::setCertificateKeyPassword );
196
+ return httpsConnector ;
197
+ }
198
+ }
0 commit comments