Spring security 使用LDAP和数据库角色的Spring安全性
在我们的新保险项目中,我尝试使用Ldap实现Spring security 使用LDAP和数据库角色的Spring安全性,spring-security,active-directory,spring,spring-ldap,Spring Security,Active Directory,Spring,Spring Ldap,在我们的新保险项目中,我尝试使用Ldap实现 我想只要在AD中找到用户后,根据AD检查用户名/密码即可。我想从数据库中具有访问级别的用户表(应用程序授权用户)中授权他。有人能给我一个好的资源示例/点吗。您很可能需要进行自定义,因为您通过LDAP进行身份验证,但通过DB查询获取角色。UserDetailsService是一个接口。您可以实现该接口,然后将自定义实现添加到Spring安全配置中,操作如下: <beans:bean id="userDetailsService" class="c
我想只要在AD中找到用户后,根据AD检查用户名/密码即可。我想从数据库中具有访问级别的用户表(应用程序授权用户)中授权他。有人能给我一个好的资源示例/点吗。您很可能需要进行自定义,因为您通过LDAP进行身份验证,但通过DB查询获取角色。UserDetailsService是一个接口。您可以实现该接口,然后将自定义实现添加到Spring安全配置中,操作如下:
<beans:bean id="userDetailsService" class="com.app.MyUserDetailsServiceImpl" />
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="plaintext" />
</authentication-provider>
</authentication-manager>
,设置用户名、密码和“权限”,即角色
有一个关于如何使用数据库的示例,您应该能够适应您的需求。现在实现这一点的最简单方法(Spring Security 3.2.5.RELEASE)是实现自定义LdapAuthoritiesPopulator
,它使用自定义JdbcDaoImpl
从数据库获取权限
代码
假设您正在使用,并且在LDAP中使用相同的用户名进行身份验证,并且作为authorities
表中的外键,您只需要:
软件包演示;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.util.Collection;
导入java.util.List;
导入org.springframework.jdbc.core.RowMapper;
导入org.springframework.security.core.GrantedAuthority;
导入org.springframework.security.core.authority.AuthorityUtils;
导入org.springframework.security.core.authority.SimpleGrantedAuthority;
导入org.springframework.security.core.userdetails.User;
导入org.springframework.security.core.userdetails.userdetails;
导入org.springframework.security.core.userdetails.UsernameNotFoundException;
导入org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;
导入org.springframework.ldap.core.DirContextOperations;
导入org.springframework.security.core.GrantedAuthority;
导入org.springframework.security.ldap.userdetails.ldaAuthoritiesPopulator;
/*
*您需要扩展JdbcDaoImpl以公开受保护的方法LoadUserAuthories。
*/
公共类CustomJDBCUserDetails服务扩展了JdbcDaoImpl{
@凌驾
公共列表LoadUserAuthority(字符串用户名){
返回super.loadUserAuthories(用户名);
}
}
/*
*然后,您的populator只需要使用上面的自定义userdetails服务。
*/
公共类CustomLdapAuthoritiesPopulator实现了LdapAuthoriesPopulator{
私有静态最终记录器Logger=LoggerFactory.getLogger(CustomLdapAuthoritiesPopulator.class);
私有CustomJdbcUserDetailsService服务;
公共CustomLdapAuthoritiesPopulator(CustomJdbcUserDetailsService服务){
服务=服务;
}
PublicCollectionHere是一个很好的资源:我需要在哪里实现LDAP AD的身份验证方法?您可以从LDAP、角色(称为“权限”)中获取用户ID和密码从您的数据库,让Spring Security完成其余的工作。配置中的密码编码器设置告诉Spring底层系统中的密码是如何编码的,例如:MD5或SHA1。在上面的示例中,我们告诉Spring UserDetails中的密码将是纯文本。AD连接详细信息在哪里?
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
/* other authentication configurations you might have */
/*
* This assumes that the dataSource configuring
* the connection to the database has been Autowired
* into this bean.
*
* Adapt according to your specific case.
*/
CustomJdbcUserDetailsService customJdbcUserDetailsService = new CustomJdbcUserDetailsService();
customJdbcUserDetailsService.setDataSource(dataSource);
CustomLdapAuthoritiesPopulator customLdapAuthoritiesPopulator = new CustomLdapAuthoritiesPopulator(customJdbcUserDetailsService);
auth.ldapAuthentication().ldapAuthoritiesPopulator(customLdapAuthoritiesPopulator)/* other LDAP configurations you might have */;
/* yet more authentication configurations you might have */
}
<beans:bean id="customJdbcUserDetailsService" class="demo.CustomJdbcUserDetailsService" />
<beans:bean id="customLdapAuthoritiesPopulator" class="demo.CustomLdapAuthoritiesPopulator">
<beans:constructor-arg ref="customJdbcUserDetailsService" />
</beans:bean>
<beans:bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
<beans:constructor-arg>
<beans:bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
<beans:constructor-arg ref="contextSource" />
<!--
other configurations you might need
-->
</beans:bean>
</beans:constructor-arg>
<beans:constructor-arg ref="customLdapAuthoritiesPopulator" />
</beans:bean>
<security:authentication-manager>
<security:authentication-provider ref="ldapAuthProvider" />
</security:authentication-manager>