|
|
@ -38,11 +38,13 @@ import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
|
|
import org.springframework.ldap.support.filter.EqualsFilter; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
@Component |
|
|
|
@Component |
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
public class LdapService { |
|
|
|
public class LdapService { |
|
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LdapService.class); |
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LdapService.class); |
|
|
|
|
|
|
|
|
|
|
|
@Value("${security.authentication.ldap.user.admin:#{null}}") |
|
|
|
@Value("${security.authentication.ldap.user.admin:#{null}}") |
|
|
@ -89,20 +91,19 @@ public class LdapService { |
|
|
|
Properties searchEnv = getManagerLdapEnv(); |
|
|
|
Properties searchEnv = getManagerLdapEnv(); |
|
|
|
LdapContext ctx = null; |
|
|
|
LdapContext ctx = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
//Connect to the LDAP server and Authenticate with a service user of whom we know the DN and credentials
|
|
|
|
// Connect to the LDAP server and Authenticate with a service user of whom we know the DN and credentials
|
|
|
|
ctx = new InitialLdapContext(searchEnv, null); |
|
|
|
ctx = new InitialLdapContext(searchEnv, null); |
|
|
|
SearchControls sc = new SearchControls(); |
|
|
|
SearchControls sc = new SearchControls(); |
|
|
|
sc.setReturningAttributes(new String[]{ldapEmailAttribute}); |
|
|
|
sc.setReturningAttributes(new String[]{ldapEmailAttribute}); |
|
|
|
sc.setSearchScope(SearchControls.SUBTREE_SCOPE); |
|
|
|
sc.setSearchScope(SearchControls.SUBTREE_SCOPE); |
|
|
|
String searchFilter = String.format("(%s=%s)", ldapUserIdentifyingAttribute, userId); |
|
|
|
EqualsFilter filter = new EqualsFilter(ldapUserIdentifyingAttribute, userId); |
|
|
|
//Search for the user you want to authenticate, search him with some attribute
|
|
|
|
NamingEnumeration<SearchResult> results = ctx.search(ldapBaseDn, filter.toString(), sc); |
|
|
|
NamingEnumeration<SearchResult> results = ctx.search(ldapBaseDn, searchFilter, sc); |
|
|
|
|
|
|
|
if (results.hasMore()) { |
|
|
|
if (results.hasMore()) { |
|
|
|
// get the users DN (distinguishedName) from the result
|
|
|
|
// get the users DN (distinguishedName) from the result
|
|
|
|
SearchResult result = results.next(); |
|
|
|
SearchResult result = results.next(); |
|
|
|
NamingEnumeration<? extends Attribute> attrs = result.getAttributes().getAll(); |
|
|
|
NamingEnumeration<? extends Attribute> attrs = result.getAttributes().getAll(); |
|
|
|
while (attrs.hasMore()) { |
|
|
|
while (attrs.hasMore()) { |
|
|
|
//Open another connection to the LDAP server with the found DN and the password
|
|
|
|
// Open another connection to the LDAP server with the found DN and the password
|
|
|
|
searchEnv.put(Context.SECURITY_PRINCIPAL, result.getNameInNamespace()); |
|
|
|
searchEnv.put(Context.SECURITY_PRINCIPAL, result.getNameInNamespace()); |
|
|
|
searchEnv.put(Context.SECURITY_CREDENTIALS, userPwd); |
|
|
|
searchEnv.put(Context.SECURITY_CREDENTIALS, userPwd); |
|
|
|
try { |
|
|
|
try { |
|
|
@ -149,7 +150,8 @@ public class LdapService { |
|
|
|
|
|
|
|
|
|
|
|
public LdapUserNotExistActionType getLdapUserNotExistAction() { |
|
|
|
public LdapUserNotExistActionType getLdapUserNotExistAction() { |
|
|
|
if (StringUtils.isBlank(ldapUserNotExistAction)) { |
|
|
|
if (StringUtils.isBlank(ldapUserNotExistAction)) { |
|
|
|
logger.info("security.authentication.ldap.user.not.exist.action configuration is empty, the default value 'CREATE'"); |
|
|
|
logger.info( |
|
|
|
|
|
|
|
"security.authentication.ldap.user.not.exist.action configuration is empty, the default value 'CREATE'"); |
|
|
|
return LdapUserNotExistActionType.CREATE; |
|
|
|
return LdapUserNotExistActionType.CREATE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|