SpringBoot 2.x 集成LDAP

一、依赖引用,在pom.xml加入以下依赖:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
</dependency>

 

二、获取LDAP配置信息:

 

application.yml增加如下配置:

ldap.urls=ldap://ldap.example.com:389/ou=test,dc=example,dc=local 
ldap.username=cn=read-only-admin,dc=example,dc=local 
ldap.password=password 
ldap.user.searchfilter=sAMAccountName={0}

 

推荐使用LDAP BrowserLDAP Admin Tool,第二个收费,试用30天,我自己使用的LDAP ADMIN TOOL;

使用先使用工具验证上面的配置信息是否正常。

具体如何使用自动研究,比较简单,这具是连接后的效果。

 

三、增加SecurityConfig配置信息,代码如下:

@Configuration
public class LDAPSecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${ldap.urls}")
    private String ldapUrls;

    @Value("${ldap.base.dn}")
    private String ldapBaseDn;

    @Value("${ldap.username}")
    private String ldapSecurityPrincipal;

    @Value("${ldap.password}")
    private String ldapPrincipalPassword;

    @Value("${ldap.user.searchfilter}")
    private String ldapUserSearchfilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/login**").anonymous()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/assets/**").permitAll()
                .antMatchers("/").authenticated()
                .antMatchers("/home").authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll()
                .and()
                .csrf()
                .disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .ldapAuthentication()
                .contextSource()
                .url(ldapUrls + ldapBaseDn)
                .managerDn(ldapSecurityPrincipal)
                .managerPassword(ldapPrincipalPassword)
                .and()
                .userSearchFilter(ldapUserSearchfilter);
    }
}

 

四、Controller代码可以自己实现了,测试是否可以使用域账号正常登录。

 

五、问题点:

 

报 : [RMI TCP Connection(1)-192.168.16.26] WARN o.s.b.a.l.LdapHealthIndicator - LDAP health check failed

java.lang.NullPointerException: null

关闭健康检测

management:
  health:
    ldap:
      enabled: false

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值