【问题标题】:Spring Security with LDAP and Database roles具有 LDAP 和数据库角色的 Spring Security
【发布时间】:2013-05-17 20:50:25
【问题描述】:

在我们的新保险项目中,我正在尝试使用 Ldap 实现

一旦用户在 AD 中找到,我想只检查用户名/密码与 AD。我想从具有数据库访问级别的用户表(应用程序授权用户)授权他。有人可以给我样品/点我一个好的资源。

【问题讨论】:

  • 这是一个很好的资源:@​​987654323@

标签: spring-security active-directory spring spring-security spring-ldap


【解决方案1】:

您很可能需要自定义UserDetailsServer,因为您通过 LDAP 进行身份验证,但通过数据库查询获取角色。 UserDetailsS​​ervice 是一个接口。您将实现该接口,然后将您的自定义实现添加到您的 Spring Security 配置中,执行以下操作:

<beans:bean id="userDetailsService" class="com.app.MyUserDetailsServiceImpl" />

<authentication-manager>
  <authentication-provider user-service-ref="userDetailsService">
    <password-encoder hash="plaintext" />
  </authentication-provider>
</authentication-manager>

在 loadUserByUsername() 中,您将创建一个UserDetails,设置用户名、密码和“权限”,即角色。

This Blog Post 有一个关于如何使用数据库的示例,您应该能够适应您的要求。

【讨论】:

  • LDAP AD的认证方法需要在哪里实现?
  • 您将从 ldap 获取用户 ID 和密码,从您的数据库中获取角色(称为“权限”),然后让 Spring Security 完成剩下的工作。配置中的密码编码器设置告诉 spring 底层系统中的密码是如何编码的,例如:MD5 或 SHA1。在上面的例子中,我们告诉 spring UserDetails 中的密码是纯文本的。
【解决方案2】:

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

代码

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

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<GrantedAuthority> loadUserAuthorities(String username) {
        return super.loadUserAuthorities(username);
    }
}


/*
 * Then, the only thing your populator needs to do is use the custom UserDetailsService above.
 */
public class CustomLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomLdapAuthoritiesPopulator.class);

    private CustomJdbcUserDetailsService service;

    public CustomLdapAuthoritiesPopulator(CustomJdbcUserDetailsService service) {
        this.service = service;
    }

    public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations user, String username) {
        return service.loadUserAuthorities(username);
    }

}

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

Java 配置

GlobalMethodSecurityConfigurationWebSecurityConfigurerAdapter@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。因此,必须手动完成。假设已经定义了一个配置与 LDAP 服务器连接的 bean contextSource,请将以下内容添加到您的 Spring XML 配置中:

<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>

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

【讨论】:

  • AD 连接详细信息在哪里?
猜你喜欢
  • 2015-02-18
  • 2016-07-09
  • 2011-10-08
  • 2017-07-21
  • 2011-09-15
  • 2011-08-07
  • 2013-06-12
  • 2015-04-29
  • 2020-08-25
相关资源
最近更新 更多