PartialResultException when authenticating with Spring Security and JavaConfig

后端 未结 1 1385
既然无缘
既然无缘 2020-12-01 15:00

I am currently creating a new web application using Spring Boot and began the process of integrating Spring Security for authentication. After successfully following the Sp

相关标签:
1条回答
  • 2020-12-01 16:01

    I had the feeling I'd need to use an instance of LdapContextSource to make this happen (since it conveniently has a setReferral method), but I struggled a bit with the details. A forum post on spring.io gave me enough to go on, and it looks like I now have things working.

    It's not clear to me if there are any significant flaws with what I'm doing here, but it seems to work, so hopefully this will be helpful to someone else in the future:

    @Configuration
    @EnableWebMvcSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/css/**").permitAll().anyRequest()
                    .fullyAuthenticated().and().formLogin();
        }
    
        @Configuration
        protected static class AuthenticationConfiguration extends
                GlobalAuthenticationConfigurerAdapter {
    
            @Override
            public void init(AuthenticationManagerBuilder auth) throws Exception {              
                DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://<url>");
                contextSource.setUserDn("<username>");
                contextSource.setPassword("<password>");
                contextSource.setReferral("follow"); 
                contextSource.afterPropertiesSet();
    
                LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth.ldapAuthentication();
    
                ldapAuthenticationProviderConfigurer
                    .userSearchFilter("(&(cn={0}))")
                    .userSearchBase("")
                    .contextSource(contextSource);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题