List of usage examples for org.springframework.security.ldap LdapUtils closeContext
public static void closeContext(Context ctx)
From source file:org.artifactory.security.ldap.ArtifactoryLdapAuthenticationProvider.java
@Override public Authentication authenticate(Authentication authentication) { String userName = authentication.getName(); // If it's an anonymous user, don't bother searching for the user. if (UserInfo.ANONYMOUS.equals(userName)) { return null; }/*ww w .j a va 2 s. co m*/ log.debug("Trying to authenticate user '{}' via ldap.", userName); LdapSetting usedLdapSetting = null; DirContextOperations user = null; AddonsManager addonsManager = InternalContextHelper.get().beanForType(AddonsManager.class); LdapGroupAddon ldapGroupAddon = addonsManager.addonByType(LdapGroupAddon.class); try { RuntimeException authenticationException = null; for (Map.Entry<String, BindAuthenticator> entry : authenticator.getAuthenticators().entrySet()) { LdapSetting currentLdapSetting = centralConfig.getDescriptor().getSecurity() .getLdapSettings(entry.getKey()); BindAuthenticator bindAuthenticator = entry.getValue(); try { user = bindAuthenticator.authenticate(authentication); if (user != null) { usedLdapSetting = currentLdapSetting; break; } } catch (AuthenticationException e) { authenticationException = e; checkIfBindAndSearchActive(currentLdapSetting, userName); } catch (org.springframework.security.core.AuthenticationException e) { authenticationException = e; checkIfBindAndSearchActive(currentLdapSetting, userName); } catch (RuntimeException e) { authenticationException = e; } } if (user == null) { if (authenticationException != null) { UserInfo userInfo = userGroupService.findUser(userName); if (userInfo != null) { log.debug("user {} failed to perform ldap authentication (not bad credential)", userInfo.getUsername()); removeUserLdapRelatedGroups(userInfo); } throw authenticationException; } throw new AuthenticationServiceException(ArtifactoryLdapAuthenticator.LDAP_SERVICE_MISCONFIGURED); } // user authenticated via ldap log.debug("'{}' authenticated successfully by ldap server.", userName); //Collect internal groups, and if using external groups add them to the user info MutableUserInfo userInfo = InfoFactoryHolder.get().copyUser( userGroupService.findOrCreateExternalAuthUser(userName, !usedLdapSetting.isAutoCreateUser())); userInfo.setRealm(LdapService.REALM); String emailAttribute = usedLdapSetting.getEmailAttribute(); if (StringUtils.isNotBlank(emailAttribute)) { String email = user.getStringAttribute(emailAttribute); if (StringUtils.isNotBlank(email)) { log.debug("User '{}' has email address '{}'", userName, email); userInfo.setEmail(email); } } log.debug("Loading LDAP groups"); ldapGroupAddon.populateGroups(user, userInfo); log.debug("Finished Loading LDAP groups"); SimpleUser simpleUser = new SimpleUser(userInfo); // update user with latest attribute userGroupService.updateUser(userInfo, false); // create new authentication response containing the user and it's authorities return new LdapRealmAwareAuthentication(simpleUser, authentication.getCredentials(), simpleUser.getAuthorities()); } catch (AuthenticationException e) { String message = String.format("Failed to authenticate user '%s' via LDAP: %s", userName, e.getMessage()); log.debug(message); throw new AuthenticationServiceException(message, e); } catch (CommunicationException ce) { String message = String.format("Failed to authenticate user '%s' via LDAP: communication error", userName); log.warn(message); log.debug(message, ce); throw new AuthenticationServiceException(message, ce); } catch (org.springframework.security.core.AuthenticationException e) { String message = String.format("Failed to authenticate user '%s' via LDAP: %s", userName, e.getMessage()); log.debug(message); throw e; } catch (NamingException e) { String message = String.format("Failed to locate directory entry for authenticated user: %s", e.getMostSpecificCause().getMessage()); log.debug(message); throw new AuthenticationServiceException(message, e); } catch (InvalidNameException e) { String message = String.format("Failed to persist user '%s': %s", userName, e.getMessage()); log.warn(message); log.debug("Cause: {}", e); throw new InternalAuthenticationServiceException(message, e); } catch (Exception e) { String message = "Unexpected exception in LDAP authentication:"; log.error(message, e); throw new AuthenticationServiceException(message, e); } finally { LdapUtils.closeContext(user); } }
From source file:org.fao.geonet.kernel.security.ldap.AutoCreateGroups.java
public boolean groupExists(String group) { DistinguishedName dn = buildDn(group); try {//from w w w . j a v a 2 s.c om Object obj = template.lookup(dn); if (obj instanceof Context) { LdapUtils.closeContext((Context) obj); } return true; } catch (org.springframework.ldap.NameNotFoundException e) { return false; } }
From source file:org.fao.geonet.kernel.security.ldap.LdapUserDetailsManager.java
public boolean userExists(String username) { DistinguishedName dn = usernameMapper.buildDn(username); try {//from w w w. j ava 2s. c o m Object obj = template.lookup(dn); if (obj instanceof Context) { LdapUtils.closeContext((Context) obj); } return true; } catch (org.springframework.ldap.NameNotFoundException e) { return false; } }