加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

具有LDAP和数据库角色的Spring Security

发布时间:2020-12-15 01:43:59 所属栏目:大数据 来源:网络整理
导读:在我们的新保险项目中,我试图用Ldap active-directory实现spring-security. 一旦用户在AD中找到,我想在AD上检查用户名/密码.我想从用户表(app授权用户)授权他在数据库中具有访问级别.有人可以提供样品/指出我的资源. 最佳答案 现在实现这一目标的最简单方法(

在我们的新保险项目中,我试图用Ldap active-directory实现spring-security.

一旦用户在AD中找到,我想在AD上检查用户名/密码.我想从用户表(app授权用户)授权他在数据库中具有访问级别.有人可以提供样品/指出我的资源.

最佳答案
现在实现这一目标的最简单方法(Spring Security 3.2.5.RELEASE)是通过实现自定义LdapAuthoritiesPopulator,它使用自定义JdbcDaoImpl从数据库中获取权限.

假设您使用的是the default database schema,并且您在LDAP中使用相同的用户名进行身份验证,并且在权限表中使用外键,则只需要:

package demo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;

import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;

/*
 * You need to extend JdbcDaoImpl to expose the protected method loadUserAuthorities.
 */
public class CustomJdbcUserDetailsService extends JdbcDaoImpl {

    @Override
    public List

现在唯一剩下的就是配置LDAP身份验证提供程序以使用CustomLdapAuthoritiesPopulator.

Java配置

在GlobalConethodSecurityConfiguration或WebSecurityConfigurerAdapter的@Configuration注释子类中(根据您的情况而定),添加以下内容:

@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 */
}

有关工作示例,请参阅https://github.com/pfac/howto-spring-security.

XML配置

免责声明:我一直专注于Java配置,所以谨慎行事,可能会有一些错误.

与使用LDAP进行身份验证的其他配置不同,似乎没有漂亮的XML标记来自定义LdapAuthoritiesPopulator.所以,它必须手动完成.假设已定义bean contextSource配置与LDAP服务器的连接,请将以下内容添加到Spring XML配置中:

资料来源:http://spapas.github.io/2013/10/14/spring-ldap-custom-authorities/#spring-security-ldap-with-custom-authorities

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读