下面列出了怎么用org.springframework.security.crypto.password.LdapShaPasswordEncoder的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.ldapAuthentication()
.userDnPatterns(ldapAuthStructure.getUserDnPattern())
.userSearchBase(ldapAuthStructure.getUserSearchBase())
.contextSource()
.url(ldapAuthStructure.getLdapUrl()+"/"+ldapAuthStructure.getLdapBase())
.managerDn(ldapAuthStructure.getLdapManagerDn())
.managerPassword(ldapAuthStructure.getLdapManagerPwd())
.and()
.passwordCompare()
.passwordEncoder(new LdapShaPasswordEncoder())
.passwordAttribute("userPassword");
logger.info("configure method is called to build Authentication manager ...");
}
/**
* 模拟数据库查询,判断尝试登陆用户是否满足认证条件
*
* @param auth AuthenticationManagerBuilder
* @throws Exception 异常
*/
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.userDnPatterns("uid={0},ou=users")
.groupSearchBase("ou=users")
.contextSource()
.url("ldap://172.22.6.12:10389/dc=dunwu,dc=github,dc=io")
.and()
.passwordCompare()
.passwordEncoder(new LdapShaPasswordEncoder())
.passwordAttribute("userPassword");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication().userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups").contextSource()
.url("ldap://localhost:8389/dc=springframework,dc=org").and()
.passwordCompare().passwordEncoder(new LdapShaPasswordEncoder())
.passwordAttribute("userPassword");
}
@Autowired
public void configureJdbc(AuthenticationManagerBuilder auth) throws Exception {
// Note that we can switch profiles on the fly in Ambari.
List<String> activeProfiles = Arrays.asList(environment.getActiveProfiles());
if (activeProfiles.contains(MetronRestConstants.LDAP_PROFILE)) {
LOG.info("Setting up LDAP authentication; url={}.", providerUrl);
LdapAuthenticationProviderConfigurer providerConf = auth
.ldapAuthentication()
.authoritiesMapper(authoritiesMapper)
.userDnPatterns(userDnPatterns)
.userSearchBase(userSearchBase)
.userSearchFilter(userSearchFilter)
.groupRoleAttribute(groupRoleAttribute)
.groupSearchFilter(groupSearchFilter)
.groupSearchBase(groupSearchBase)
.contextSource()
.url(providerUrl)
.managerDn(providerUserDn)
.managerPassword(providerPassword)
.and();
if(StringUtils.isNotBlank(passwordAttribute)) {
// if a password attribute is provided, use that for authentication
providerConf
.passwordCompare()
.passwordEncoder(new LdapShaPasswordEncoder())
.passwordAttribute(passwordAttribute);
} else {
// if no password attribute, set encoder to null which forces bind authentication
providerConf
.passwordCompare()
.passwordEncoder(null);
}
} else if (activeProfiles.contains(MetronRestConstants.DEV_PROFILE) ||
activeProfiles.contains(MetronRestConstants.TEST_PROFILE)) {
LOG.info("Setting up JDBC authentication with dev/test profiles");
auth.jdbcAuthentication()
.dataSource(dataSource)
.withUser("user").password("password").roles(SECURITY_ROLE_USER).and()
.withUser("user1").password("password").roles(SECURITY_ROLE_USER).and()
.withUser("user2").password("password").roles(SECURITY_ROLE_USER).and()
.withUser("admin").password("password").roles(SECURITY_ROLE_USER, SECURITY_ROLE_ADMIN);
} else {
LOG.debug("Setting up JDBC authentication");
auth.jdbcAuthentication().dataSource(dataSource);
}
}