How to use Spring Ldap Authentication

梦想与她 提交于 2019-12-19 03:39:14

问题


In my current project, I have to implement LDAP authentication. I am using JSF 2.2, primefaces and Spring 4.0 and spring-ldap-core 1.3.2 and spring-security-ldap-3.2.0. Below are the work till now I have done to achieve:

Spring-Ldap.xml

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
 <property name="url" value="ldap://mumcXXXXXXX" />
 <property name="base" value="dc=ad,dc=XXX,dc=com"/>
 <property name="userDn" value="XXXX@ad.XXX.com" />
 <property name="password" value="XXXX" />
 </bean>

 <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
    <constructor-arg ref="contextSource" />
</bean>

<bean id="ldapContact"
    class="com.csap.research.LDAPContactDAO">
    <property name="ldapTemplate" ref="ldapTemplate" />
</bean>

My LdapContactDao

public boolean login(String username, String password) {
        AndFilter filter = new AndFilter();
        ldapTemplate.setIgnorePartialResultException(true); 
        filter.and(new EqualsFilter("userPrincipalName", username+"@ad.cXXX.com"));
        return ldapTemplate.authenticate("", filter.toString(), password);
}

Here username and password are coming from Login screen as inputs. My problem is its very hardcoded. I dont want to hardcode username and password in Spring-Ldap.xml , So there was a suggestion to use Spring-security-Ldap here Spring LdapAuthentication and Load roles from local database but I was unable to understand it.

My question was how I can achieve dynamic integration of Ldap with spring and corse JSF i am using as a front-end controller. Any help would be great.


回答1:


I found these article helpful for setting up login form with spring security, however, they do not use jsf:

http://www.mkyong.com/spring-security/spring-security-hello-world-example/ http://www.mkyong.com/spring-security/spring-security-form-login-example/

and found this article helpful for using ldap as authentication provider, it does not use ldapTemplate, but uses the spring-security configurations (spring-security.xml in the article)

http://krams915.blogspot.com/2011/01/spring-security-mvc-using-ldap.html




回答2:


This is how I am using LDAP for authentication:

  1. Import Maven dependencies

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-ldap</artifactId>
      <version>4.0.2.RELEASE</version>
    </dependency>
    
  2. Write your implementation of WebSecurityConfigurerAdapter:

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    private static final String SSO_HEADER = "AUTH_USER";
    
    public static final String ADMIN = "ROLE_ADMIN";
    public static final String USER = "ROLE_USER";
    public static final String ANONYMOUS = "ROLE_ANONYMOUS";
    
    @Autowired
    Environment env;
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
        http.authorizeRequests()
                .antMatchers("/css/**","/js/**","/images/**","/fonts/**","/api/**","/sendRedirect/**","/test/**").permitAll()
                .anyRequest().fullyAuthenticated().and().formLogin().loginPage("/login")
                .failureUrl("/login?error").permitAll()
                .and()
                .logout()
                .deleteCookies("remove")
                .invalidateHttpSession(true)
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login?logout")
                .and()
            // Cross-site request forgery is turned off for RESTful API calls with the assumption that
            // authentication will be sufficient protection
            .csrf().ignoringAntMatchers("/api/**", "/space/{\\d+}/**", "/admin/**");
    }
    
    @Override
    public AuthenticationManager authenticationManagerBean()
        throws Exception
    {
        return authenticationManager();
    }
    
    @Configuration
    protected static class AuthenticationConfiguration extends
            GlobalAuthenticationConfigurerAdapter {
    
        @Autowired
        Environment env;
    
        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            auth.ldapAuthentication().userDnPatterns("cn={0}")
                    .contextSource(contextSource());
        }
    
        @Bean
        public LdapContextSource contextSource() {
            LdapContextSource contextSource = new LdapContextSource();
            contextSource.setUrl(env.getRequiredProperty("ldap.url"));
            contextSource.setBase(env.getRequiredProperty("ldap.base"));
            contextSource.setUserDn(env.getRequiredProperty("ldap.username"));
            contextSource.setPassword(env.getRequiredProperty("ldap.password"));
            return contextSource;
        }
    }
    
    }
    


来源:https://stackoverflow.com/questions/21555280/how-to-use-spring-ldap-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!