1616
1717package org .springframework .boot .ldap .autoconfigure .embedded ;
1818
19- import java .io .IOException ;
2019import java .io .InputStream ;
21- import java .security .KeyManagementException ;
22- import java .security .KeyStore ;
23- import java .security .KeyStoreException ;
24- import java .security .NoSuchAlgorithmException ;
25- import java .security .SecureRandom ;
26- import java .security .UnrecoverableKeyException ;
27- import java .security .cert .CertificateException ;
2820import java .util .Collections ;
2921import java .util .HashMap ;
3022import java .util .List ;
3123import java .util .Map ;
3224
33- import javax .net .ssl .KeyManager ;
34- import javax .net .ssl .KeyManagerFactory ;
3525import javax .net .ssl .SSLContext ;
3626import javax .net .ssl .SSLServerSocketFactory ;
3727import javax .net .ssl .SSLSocketFactory ;
38- import javax .net .ssl .TrustManager ;
39- import javax .net .ssl .TrustManagerFactory ;
4028
4129import com .unboundid .ldap .listener .InMemoryDirectoryServer ;
4230import com .unboundid .ldap .listener .InMemoryDirectoryServerConfig ;
6452import org .springframework .boot .ldap .autoconfigure .LdapAutoConfiguration ;
6553import org .springframework .boot .ldap .autoconfigure .LdapProperties ;
6654import org .springframework .boot .ldap .autoconfigure .embedded .EmbeddedLdapAutoConfiguration .EmbeddedLdapAutoConfigurationRuntimeHints ;
55+ import org .springframework .boot .ldap .autoconfigure .embedded .EmbeddedLdapProperties .Ssl ;
6756import org .springframework .boot .ssl .SslBundle ;
6857import org .springframework .boot .ssl .SslBundles ;
6958import org .springframework .context .ApplicationContext ;
7968import org .springframework .core .env .MutablePropertySources ;
8069import org .springframework .core .env .PropertySource ;
8170import org .springframework .core .io .Resource ;
82- import org .springframework .core .io .ResourceLoader ;
83- import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
8471import org .springframework .core .type .AnnotatedTypeMetadata ;
8572import org .springframework .ldap .core .ContextSource ;
8673import org .springframework .ldap .core .support .LdapContextSource ;
@@ -106,8 +93,6 @@ public final class EmbeddedLdapAutoConfiguration implements DisposableBean {
10693
10794 private final EmbeddedLdapProperties embeddedProperties ;
10895
109- private final ResourceLoader resourceLoader = new PathMatchingResourcePatternResolver ();
110-
11196 private @ Nullable InMemoryDirectoryServer server ;
11297
11398 EmbeddedLdapAutoConfiguration (EmbeddedLdapProperties embeddedProperties ) {
@@ -116,35 +101,44 @@ public final class EmbeddedLdapAutoConfiguration implements DisposableBean {
116101
117102 @ Bean
118103 InMemoryDirectoryServer directoryServer (ApplicationContext applicationContext ,
119- ObjectProvider <SslBundles > sslBundles ) throws LDAPException , KeyStoreException , IOException ,
120- NoSuchAlgorithmException , CertificateException , UnrecoverableKeyException , KeyManagementException {
104+ ObjectProvider <SslBundles > sslBundles ) throws LDAPException {
121105 String [] baseDn = StringUtils .toStringArray (this .embeddedProperties .getBaseDn ());
122106 InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig (baseDn );
123107 String username = this .embeddedProperties .getCredential ().getUsername ();
124108 String password = this .embeddedProperties .getCredential ().getPassword ();
125109 if (StringUtils .hasText (username ) && StringUtils .hasText (password )) {
126110 config .addAdditionalBindCredentials (username , password );
127111 }
112+ config .setListenerConfigs (createListenerConfig (sslBundles ));
128113 setSchema (config );
129- if (this .embeddedProperties .getSsl ().isEnabled ()) {
130- EmbeddedLdapProperties .Ssl ssl = this .embeddedProperties .getSsl ();
131- SSLContext sslContext = getSslContext (ssl , sslBundles .getIfAvailable ());
132- SSLServerSocketFactory serverSocketFactory = sslContext .getServerSocketFactory ();
133- SSLSocketFactory clientSocketFactory = sslContext .getSocketFactory ();
134- config .setListenerConfigs (InMemoryListenerConfig .createLDAPSConfig ("LDAPS" , null ,
135- this .embeddedProperties .getPort (), serverSocketFactory , clientSocketFactory ));
136- }
137- else {
138- config
139- .setListenerConfigs (InMemoryListenerConfig .createLDAPConfig ("LDAP" , this .embeddedProperties .getPort ()));
140- }
141114 this .server = new InMemoryDirectoryServer (config );
142115 importLdif (this .server , applicationContext );
143116 this .server .startListening ();
144117 setPortProperty (applicationContext , this .server .getListenPort ());
145118 return this .server ;
146119 }
147120
121+ private InMemoryListenerConfig createListenerConfig (ObjectProvider <SslBundles > sslBundles ) throws LDAPException {
122+ SslBundle sslBundle = getSslBundle (sslBundles .getIfAvailable ());
123+ if (sslBundle != null ) {
124+ SSLContext sslContext = sslBundle .createSslContext ();
125+ SSLServerSocketFactory serverSocketFactory = sslContext .getServerSocketFactory ();
126+ SSLSocketFactory clientSocketFactory = sslContext .getSocketFactory ();
127+ return InMemoryListenerConfig .createLDAPSConfig ("LDAPS" , null , this .embeddedProperties .getPort (),
128+ serverSocketFactory , clientSocketFactory );
129+ }
130+ return InMemoryListenerConfig .createLDAPConfig ("LDAP" , this .embeddedProperties .getPort ());
131+ }
132+
133+ private @ Nullable SslBundle getSslBundle (@ Nullable SslBundles sslBundles ) {
134+ Ssl ssl = this .embeddedProperties .getSsl ();
135+ if (ssl .isEnabled () && StringUtils .hasLength (ssl .getBundle ())) {
136+ Assert .notNull (sslBundles , "SSL bundle name has been set but no SSL bundles found in context" );
137+ return sslBundles .getBundle (ssl .getBundle ());
138+ }
139+ return null ;
140+ }
141+
148142 private void setSchema (InMemoryDirectoryServerConfig config ) {
149143 if (!this .embeddedProperties .getValidation ().isEnabled ()) {
150144 config .setSchema (null );
@@ -216,70 +210,6 @@ public void destroy() throws Exception {
216210 }
217211 }
218212
219- private SSLContext getSslContext (EmbeddedLdapProperties .Ssl ssl , @ Nullable SslBundles sslBundles )
220- throws KeyStoreException , IOException , NoSuchAlgorithmException , CertificateException ,
221- UnrecoverableKeyException , KeyManagementException {
222- if (sslBundles != null && StringUtils .hasText (ssl .getBundle ())) {
223- SslBundle sslBundle = sslBundles .getBundle (ssl .getBundle ());
224- Assert .notNull (sslBundle , "SSL bundle name has been set but no SSL bundles found in context" );
225- return sslBundle .createSslContext ();
226-
227- }
228- else {
229- SSLContext sslContext = SSLContext .getInstance (ssl .getAlgorithm ());
230- KeyManager [] keyManagers = configureKeyManagers (ssl );
231- TrustManager [] trustManagers = configureTrustManagers (ssl );
232- sslContext .init (keyManagers , trustManagers , new SecureRandom ());
233- return sslContext ;
234- }
235- }
236-
237- private KeyManager @ Nullable [] configureKeyManagers (EmbeddedLdapProperties .Ssl ssl ) throws KeyStoreException ,
238- IOException , NoSuchAlgorithmException , CertificateException , UnrecoverableKeyException {
239- String keyStoreName = ssl .getKeyStore ();
240- String keyStorePassword = ssl .getKeyStorePassword ();
241- String storeType = ssl .getKeyStoreType ();
242- char [] keyPassphrase = null ;
243- if (keyStorePassword != null ) {
244- keyPassphrase = keyStorePassword .toCharArray ();
245- }
246- KeyManager [] keyManagers = null ;
247- if (StringUtils .hasText (keyStoreName )) {
248- Resource resource = this .resourceLoader .getResource (keyStoreName );
249- KeyStore ks = KeyStore .getInstance (storeType );
250- try (InputStream inputStream = resource .getInputStream ()) {
251- ks .load (inputStream , keyPassphrase );
252- }
253- KeyManagerFactory kmf = KeyManagerFactory .getInstance (ssl .getKeyStoreAlgorithm ());
254- kmf .init (ks , keyPassphrase );
255- keyManagers = kmf .getKeyManagers ();
256- }
257- return keyManagers ;
258- }
259-
260- private TrustManager @ Nullable [] configureTrustManagers (EmbeddedLdapProperties .Ssl ssl )
261- throws KeyStoreException , IOException , NoSuchAlgorithmException , CertificateException {
262- String trustStoreName = ssl .getTrustStore ();
263- String trustStorePassword = ssl .getTrustStorePassword ();
264- String storeType = ssl .getTrustStoreType ();
265- char [] trustPassphrase = null ;
266- if (trustStorePassword != null ) {
267- trustPassphrase = trustStorePassword .toCharArray ();
268- }
269- TrustManager [] trustManagers = null ;
270- if (StringUtils .hasText (trustStoreName )) {
271- Resource resource = this .resourceLoader .getResource (trustStoreName );
272- KeyStore tks = KeyStore .getInstance (storeType );
273- try (InputStream inputStream = resource .getInputStream ()) {
274- tks .load (inputStream , trustPassphrase );
275- }
276- TrustManagerFactory tmf = TrustManagerFactory .getInstance (ssl .getTrustStoreAlgorithm ());
277- tmf .init (tks );
278- trustManagers = tmf .getTrustManagers ();
279- }
280- return trustManagers ;
281- }
282-
283213 /**
284214 * {@link SpringBootCondition} to determine when to apply embedded LDAP
285215 * auto-configuration.
0 commit comments