Spring Boot安全性LDAP身份验证

时间:2016-09-23 20:48:26

标签: java spring spring-security spring-boot ldap

我正在尝试使用自定义登录页面进行LDAP身份验证,但下面不能使用我的安全性和ldap配置

@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.httpBasic().and().authorizeRequests().antMatchers("/**").permitAll()
            .anyRequest().authenticated()
            .and().formLogin().loginPage("/login")
            .usernameParameter("username")
            .passwordParameter("password")
            .failureUrl("/login?error");

    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
                .userDnPatterns("uid={0},ou=people")
                .groupSearchBase("ou=groups")
                .contextSource().ldif("classpath:test-server.ldif");
    }
}

以下是放置在资源文件夹

中的示例LDif文件

dn:uid = bob,ou = people,dc = springframework,dc = org objectclass:顶部 对象类:人 objectclass:organizationalPerson objectclass:inetOrgPerson cn:鲍勃汉密尔顿 sn:汉密尔顿 uid:bob userPassword:bobspassword

我正在寻找只有有效用户可以访问应用程序中的其他页面。

配置是否有任何问题,并会感谢您的回答。

1 个答案:

答案 0 :(得分:-1)

您应该将代码更改为以下内容:

   @Override
   protected void configure(HttpSecurity http) throws Exception {
   http.httpBasic().and().authorizeRequests()
   .anyRequest().authenticated()
   .and().formLogin().loginPage("/login")
   .usernameParameter("username")
   .passwordParameter("password")
   .failureUrl("/login?error");

.antMatchers("/**").permitAll()

您只需允许每个用户访问每个页面而无需任何身份验证。