本文整理汇总了Java中org.springframework.security.ldap.authentication.BindAuthenticator.setUserSearch方法的典型用法代码示例。如果您正苦于以下问题:Java BindAuthenticator.setUserSearch方法的具体用法?Java BindAuthenticator.setUserSearch怎么用?Java BindAuthenticator.setUserSearch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.ldap.authentication.BindAuthenticator
的用法示例。
在下文中一共展示了BindAuthenticator.setUserSearch方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testJndiSpring
import org.springframework.security.ldap.authentication.BindAuthenticator; //导入方法依赖的package包/类
@Test
public void testJndiSpring() throws Exception {
DefaultSpringSecurityContextSource ctxSrc = new DefaultSpringSecurityContextSource(
"ldap://ldap.xxx:389/OU=xxx");
ctxSrc.setUserDn(USER_LDAP);
ctxSrc.setPassword(PASSWORD_LDAP);
ctxSrc.afterPropertiesSet();
logger.info("Base LDAP Path: " + ctxSrc.getBaseLdapPath());
logger.info("Principal: "
+ ctxSrc.getAuthenticationSource().getPrincipal().toString());
logger.info("Credentials: "
+ ctxSrc.getAuthenticationSource().getCredentials());
Authentication bob = new UsernamePasswordAuthenticationToken("bob",
"bob");
BindAuthenticator authenticator = new BindAuthenticator(ctxSrc);
authenticator.setUserSearch(new FilterBasedLdapUserSearch("",
"(&(objectCategory=Person)(sAMAccountName={0}))", ctxSrc));
authenticator.afterPropertiesSet();
authenticator.authenticate(bob);
DirContextOperations user = authenticator.authenticate(bob);
logger.info("User: {}", user);
}
示例2: authenticate
import org.springframework.security.ldap.authentication.BindAuthenticator; //导入方法依赖的package包/类
/**
* Tries to authenticate the user. If the authentication fails an appropriate
* {@link AuthenticationException} is thrown
*
* @param username
* the user name of the user to authenticate.
* @param password
* the password
* @param usernameAttribute
* determines whether the username parameter refers to an email address or alias
* @return the VO of the authenticated user
* @throws LdapAttributeMappingException
* in case the result returned from can not be mapped to the the VO
*/
public ExternalUserVO authenticate(String username, String password,
LdapUserAttribute usernameAttribute) throws LdapAttributeMappingException {
LOGGER.debug("Attempting authentication of user {} against LDAP directory", username);
LdapUserAttributesMapper mapper = new LdapUserAttributesMapper(ldapConfig);
LdapContextSource context = LdapSearchUtils.createLdapContext(ldapConfig, mapper);
DirContextOperations ldapDetails;
CommunoteLdapUserSearch search;
// simple authentication via search for user and bind with the found DN
if (ldapConfig.getSaslMode() == null) {
// create ldap search based on the given user attribute and reusing context
search = new CommunoteLdapUserSearch(ldapConfig.getUserSearch(), mapper, context,
usernameAttribute, null, null);
// create Bind authenticator (which first checks with a search whether the user exists
// and than tries to bind with that user and provided pwd)
BindAuthenticator authenticator = new BindAuthenticator(context);
authenticator.setUserSearch(search);
// authenticate with ldap server with username and password
ldapDetails = authenticator.authenticate(new UsernamePasswordAuthenticationToken(
username, password));
} else {
// do search as that user
search = new CommunoteLdapUserSearch(ldapConfig.getUserSearch(), mapper, context,
usernameAttribute, username, password);
ldapDetails = search.searchForUser(username);
}
ExternalUserVO userVO = search.transformResult(ldapDetails);
checkAccountStatus(ldapDetails, username);
// LDAP account status allows logging in thus set user status to active
userVO.setStatus(UserStatus.ACTIVE);
return userVO;
}
示例3: getBindAuthenticator
import org.springframework.security.ldap.authentication.BindAuthenticator; //导入方法依赖的package包/类
private BindAuthenticator getBindAuthenticator(
FilterBasedLdapUserSearch userSearch,
LdapContextSource ldapContextSource) throws Exception {
BindAuthenticator bindAuthenticator = new BindAuthenticator(
ldapContextSource);
bindAuthenticator.setUserSearch(userSearch);
String[] userDnPatterns = new String[] { ldapUserDNPattern };
bindAuthenticator.setUserDnPatterns(userDnPatterns);
bindAuthenticator.afterPropertiesSet();
return bindAuthenticator;
}
示例4: bindAuthenticator
import org.springframework.security.ldap.authentication.BindAuthenticator; //导入方法依赖的package包/类
@Bean
public BindAuthenticator bindAuthenticator() {
BindAuthenticator auth = new BindAuthenticator(contextSource());
if (StringUtils.isNotEmpty(ldapDnPattern)) {
auth.setUserDnPatterns(new String[]{ldapDnPattern});
}
if (StringUtils.isNotEmpty(ldapSearchPattern)) {
auth.setUserSearch(userSearch());
}
return auth;
}
示例5: loadProvider
import org.springframework.security.ldap.authentication.BindAuthenticator; //导入方法依赖的package包/类
private LdapAuthenticationProvider loadProvider() {
LDAPSettings settings = cachedSettingsService.getCachedSettings(LDAPSettings.class);
if (settings.isEnabled()) {
// LDAP context
DefaultSpringSecurityContextSource ldapContextSource = new DefaultSpringSecurityContextSource(settings.getUrl());
ldapContextSource.setUserDn(settings.getUser());
ldapContextSource.setPassword(settings.getPassword());
try {
ldapContextSource.afterPropertiesSet();
} catch (Exception e) {
throw new CannotInitializeLDAPException(e);
}
// User search
FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch(
settings.getSearchBase(),
settings.getSearchFilter(),
ldapContextSource);
userSearch.setSearchSubtree(true);
// Bind authenticator
BindAuthenticator bindAuthenticator = new BindAuthenticator(ldapContextSource);
bindAuthenticator.setUserSearch(userSearch);
// Provider
LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator, authoritiesPopulator);
ldapAuthenticationProvider.setUserDetailsContextMapper(new ConfigurableUserDetailsContextMapper(settings));
// OK
return ldapAuthenticationProvider;
}
// LDAP not enabled
else {
return null;
}
}
示例6: LDAPAuthenticator
import org.springframework.security.ldap.authentication.BindAuthenticator; //导入方法依赖的package包/类
/**
* Default constructor.
* @param ldapSettings LDAP config map for an app
*/
public LDAPAuthenticator(Map<String, String> ldapSettings) {
if (ldapSettings != null && ldapSettings.containsKey("security.ldap.server_url")) {
String serverUrl = ldapSettings.get("security.ldap.server_url");
String baseDN = ldapSettings.get("security.ldap.base_dn");
String bindDN = ldapSettings.get("security.ldap.bind_dn");
String basePass = ldapSettings.get("security.ldap.bind_pass");
String searchBase = ldapSettings.get("security.ldap.user_search_base");
String searchFilter = ldapSettings.get("security.ldap.user_search_filter");
String dnPattern = ldapSettings.get("security.ldap.user_dn_pattern");
String passAttribute = ldapSettings.get("security.ldap.password_attribute");
boolean usePasswordComparison = ldapSettings.containsKey("security.ldap.compare_passwords");
DefaultSpringSecurityContextSource contextSource =
new DefaultSpringSecurityContextSource(Arrays.asList(serverUrl), baseDN);
contextSource.setAuthenticationSource(new SpringSecurityAuthenticationSource());
contextSource.setCacheEnvironmentProperties(false);
if (!bindDN.isEmpty()) {
contextSource.setUserDn(bindDN);
}
if (!basePass.isEmpty()) {
contextSource.setPassword(basePass);
}
LdapUserSearch userSearch = new FilterBasedLdapUserSearch(searchBase, searchFilter, contextSource);
if (usePasswordComparison) {
PasswordComparisonAuthenticator p = new PasswordComparisonAuthenticator(contextSource);
p.setPasswordAttributeName(passAttribute);
p.setPasswordEncoder(new LdapShaPasswordEncoder());
p.setUserDnPatterns(new String[]{dnPattern});
p.setUserSearch(userSearch);
authenticator = p;
} else {
BindAuthenticator b = new BindAuthenticator(contextSource);
b.setUserDnPatterns(new String[]{dnPattern});
b.setUserSearch(userSearch);
authenticator = b;
}
}
}
示例7: ldapAuthenticator
import org.springframework.security.ldap.authentication.BindAuthenticator; //导入方法依赖的package包/类
@Bean
public LdapAuthenticator ldapAuthenticator() {
BindAuthenticator authenticator = new BindAuthenticator(ldapContextSource());
authenticator.setUserAttributes(new String[] {ldapUserIdAttr});
if (!"".equals(ldapSearchFilter)) {
logger.debug("ldapSearch: {} {}", ldapSearchBase, ldapSearchFilter);
authenticator.setUserSearch(new FilterBasedLdapUserSearch(ldapSearchBase, ldapSearchFilter, ldapContextSource()));
} else {
logger.debug("ldapUserDn: {}", ldapUserDn);
authenticator.setUserDnPatterns(new String[] {ldapUserDn});
}
return authenticator;
}
示例8: getADBindAuthentication
import org.springframework.security.ldap.authentication.BindAuthenticator; //导入方法依赖的package包/类
private Authentication getADBindAuthentication (Authentication authentication) {
try {
String userName = authentication.getName();
String userPassword = "";
if (authentication.getCredentials() != null) {
userPassword = authentication.getCredentials().toString();
}
LdapContextSource ldapContextSource = new DefaultSpringSecurityContextSource(adURL);
ldapContextSource.setUserDn(adBindDN);
ldapContextSource.setPassword(adBindPassword);
ldapContextSource.setReferral(adReferral);
ldapContextSource.setCacheEnvironmentProperties(true);
ldapContextSource.setAnonymousReadOnly(false);
ldapContextSource.setPooled(true);
ldapContextSource.afterPropertiesSet();
if (adUserSearchFilter==null || adUserSearchFilter.trim().isEmpty()) {
adUserSearchFilter="(sAMAccountName={0})";
}
FilterBasedLdapUserSearch userSearch=new FilterBasedLdapUserSearch(adBase, adUserSearchFilter,ldapContextSource);
userSearch.setSearchSubtree(true);
BindAuthenticator bindAuthenticator = new BindAuthenticator(ldapContextSource);
bindAuthenticator.setUserSearch(userSearch);
bindAuthenticator.afterPropertiesSet();
LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator);
if (userName != null && userPassword != null
&& !userName.trim().isEmpty()
&& !userPassword.trim().isEmpty()) {
final List<GrantedAuthority> grantedAuths = getAuthorities(userName);
final UserDetails principal = new User(userName, userPassword,
grantedAuths);
final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(
principal, userPassword, grantedAuths);
authentication = ldapAuthenticationProvider.authenticate(finalAuthentication);
if (groupsFromUGI) {
authentication = getAuthenticationWithGrantedAuthorityFromUGI(authentication);
}
return authentication;
} else {
LOG.error("AD Authentication Failed userName or userPassword is null or empty");
return null;
}
} catch (Exception e) {
LOG.error("AD Authentication Failed:", e);
return null;
}
}