gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:52:34 25 4
gpt4 key购买 nike

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

一旦用户在 AD 中找到,我想仅检查 AD 中的用户名/密码。我想从用户表(应用程序授权用户)中授权他具有数据库中的访问级别。有人可以提供示例/给我指出一个好的资源吗?

最佳答案

现在实现这一点的最简单方法(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

关于具有 LDAP 和数据库角色的 Spring Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com