Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
aa4dad1d
Commit
aa4dad1d
authored
Mar 04, 2020
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Allow Embedded directory to be used without spring-data-ldap"
See gh-20223
parent
a92c57c0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
47 deletions
+36
-47
EmbeddedLdapAutoConfiguration.java
...onfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java
+11
-16
EmbeddedLdapProperties.java
...t/autoconfigure/ldap/embedded/EmbeddedLdapProperties.java
+6
-5
EmbeddedLdapAutoConfigurationTests.java
...ure/ldap/embedded/EmbeddedLdapAutoConfigurationTests.java
+19
-26
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java
View file @
aa4dad1d
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -76,8 +76,6 @@ import org.springframework.util.StringUtils;
@Conditional
(
EmbeddedLdapAutoConfiguration
.
EmbeddedLdapCondition
.
class
)
public
class
EmbeddedLdapAutoConfiguration
{
private
static
final
String
DIRECTORY_SERVER_BEAN_NAME
=
"directoryServer"
;
private
static
final
String
PROPERTY_SOURCE_NAME
=
"ldap.ports"
;
private
final
EmbeddedLdapProperties
embeddedProperties
;
...
...
@@ -88,11 +86,11 @@ public class EmbeddedLdapAutoConfiguration {
this
.
embeddedProperties
=
embeddedProperties
;
}
@Bean
(
name
=
DIRECTORY_SERVER_BEAN_NAME
)
@Bean
public
InMemoryDirectoryServer
directoryServer
(
ApplicationContext
applicationContext
)
throws
LDAPException
{
String
[]
baseDn
=
StringUtils
.
toStringArray
(
this
.
embeddedProperties
.
getBaseDn
());
InMemoryDirectoryServerConfig
config
=
new
InMemoryDirectoryServerConfig
(
baseDn
);
if
(
this
.
embeddedProperties
.
hasCredential
())
{
if
(
this
.
embeddedProperties
.
getCredential
().
isAvailable
())
{
config
.
addAdditionalBindCredentials
(
this
.
embeddedProperties
.
getCredential
().
getUsername
(),
this
.
embeddedProperties
.
getCredential
().
getPassword
());
}
...
...
@@ -129,8 +127,6 @@ public class EmbeddedLdapAutoConfiguration {
}
}
private
void
importLdif
(
ApplicationContext
applicationContext
)
throws
LDAPException
{
String
location
=
this
.
embeddedProperties
.
getLdif
();
if
(
StringUtils
.
hasText
(
location
))
{
...
...
@@ -197,25 +193,24 @@ public class EmbeddedLdapAutoConfiguration {
}
/**
* Creates an {@link LdapContextSource} for use with Embedded LDAP.
*/
@Configuration
@Configuration
(
proxyBeanMethods
=
false
)
@ConditionalOnClass
(
ContextSource
.
class
)
static
class
EmbeddedLdapContextConfiguration
{
@Bean
@DependsOn
(
DIRECTORY_SERVER_BEAN_NAME
)
@DependsOn
(
"directoryServer"
)
@ConditionalOnMissingBean
public
LdapContextSource
ldapContextSource
(
Environment
environment
,
LdapProperties
properties
,
EmbeddedLdapProperties
embeddedProperties
)
{
LdapContextSource
ldapContextSource
(
Environment
environment
,
LdapProperties
properties
,
EmbeddedLdapProperties
embeddedProperties
)
{
LdapContextSource
source
=
new
LdapContextSource
();
if
(
embeddedProperties
.
hasCredential
())
{
if
(
embeddedProperties
.
getCredential
().
isAvailable
())
{
source
.
setUserDn
(
embeddedProperties
.
getCredential
().
getUsername
());
source
.
setPassword
(
embeddedProperties
.
getCredential
().
getPassword
());
}
source
.
setUrls
(
properties
.
determineUrls
(
environment
));
return
source
;
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapProperties.java
View file @
aa4dad1d
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.ldap.embedded;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.convert.Delimiter
;
import
org.springframework.core.io.Resource
;
...
...
@@ -75,10 +76,6 @@ public class EmbeddedLdapProperties {
this
.
credential
=
credential
;
}
public
boolean
hasCredential
()
{
return
credential
!=
null
&&
StringUtils
.
hasText
(
credential
.
getUsername
())
&&
StringUtils
.
hasText
(
credential
.
getPassword
());
}
public
List
<
String
>
getBaseDn
()
{
return
this
.
baseDn
;
}
...
...
@@ -127,6 +124,10 @@ public class EmbeddedLdapProperties {
this
.
password
=
password
;
}
boolean
isAvailable
()
{
return
StringUtils
.
hasText
(
this
.
username
)
&&
StringUtils
.
hasText
(
this
.
password
);
}
}
public
static
class
Validation
{
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfigurationTests.java
View file @
aa4dad1d
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -22,7 +22,7 @@ import com.unboundid.ldap.sdk.DN;
import
com.unboundid.ldap.sdk.LDAPConnection
;
import
com.unboundid.ldap.sdk.LDAPException
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
;
...
...
@@ -38,7 +38,6 @@ import org.springframework.ldap.core.LdapTemplate;
import
org.springframework.ldap.core.support.LdapContextSource
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatThrownBy
;
/**
* Tests for {@link EmbeddedLdapAutoConfiguration}
...
...
@@ -47,7 +46,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
*/
class
EmbeddedLdapAutoConfigurationTests
{
private
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
EmbeddedLdapAutoConfiguration
.
class
));
@Test
...
...
@@ -151,38 +150,32 @@ class EmbeddedLdapAutoConfigurationTests {
}
@Test
void
testLdapContextSourceWithCredentials
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.ldap.embedded.base-dn:dc=spring,dc=org"
,
void
ldapContextSourceWithCredentialsIsCreated
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.ldap.embedded.base-dn:dc=spring,dc=org"
,
"spring.ldap.embedded.credential.username:uid=root"
,
"spring.ldap.embedded.credential.password:boot"
)
.
run
(
context
->
{
LdapContextSource
ldapContextSource
=
context
.
getBean
(
LdapContextSource
.
class
);
assertThat
(
ldapContextSource
.
getUserDn
()).
isEqualTo
(
"uid=root"
);
assertThat
(
ldapContextSource
.
getUrls
()).
isNotEmpty
(
);
});
.
run
(
context
->
{
LdapContextSource
ldapContextSource
=
context
.
getBean
(
LdapContextSource
.
class
);
assertThat
(
ldapContextSource
.
getUrls
()).
isNotEmpty
(
);
assertThat
(
ldapContextSource
.
getUserDn
()).
isEqualTo
(
"uid=root"
);
});
}
@Test
void
testLdapContextSourceWithoutCredentials
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.ldap.embedded.base-dn:dc=spring,dc=org"
)
.
run
(
context
->
{
void
ldapContextSourceWithoutCredentialsIsCreated
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.ldap.embedded.base-dn:dc=spring,dc=org"
).
run
(
context
->
{
LdapContextSource
ldapContextSource
=
context
.
getBean
(
LdapContextSource
.
class
);
assertThat
(
ldapContextSource
.
getUserDn
()).
isEmpty
();
assertThat
(
ldapContextSource
.
getUrls
()).
isNotEmpty
();
assertThat
(
ldapContextSource
.
getUserDn
()).
isEmpty
();
});
}
@Test
void
testNoLdapContextSourceWithoutContextSourceClass
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.ldap.embedded.base-dn:dc=spring,dc=org"
)
.
withClassLoader
(
new
FilteredClassLoader
(
ContextSource
.
class
))
.
run
(
context
->
{
NoSuchBeanDefinitionException
expectedException
=
new
NoSuchBeanDefinitionException
(
LdapContextSource
.
class
);
assertThatThrownBy
(()->
context
.
getBean
(
LdapContextSource
.
class
))
.
isInstanceOf
(
expectedException
.
getClass
())
.
hasMessage
(
expectedException
.
getMessage
());
});
void
ldapContextWithoutSpringLdapIsNotCreated
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.ldap.embedded.base-dn:dc=spring,dc=org"
)
.
withClassLoader
(
new
FilteredClassLoader
(
ContextSource
.
class
)).
run
(
context
->
{
assertThat
(
context
).
hasNotFailed
();
assertThat
(
context
).
doesNotHaveBean
(
LdapContextSource
.
class
);
});
}
@Configuration
(
proxyBeanMethods
=
false
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment