【问题标题】:How to use Spring Ldap Authentication如何使用 Spring Ldap 身份验证
【发布时间】:2014-02-28 14:05:36
【问题描述】:

在我当前的项目中,我必须实现 LDAP 身份验证。 我正在使用 JSF 2.2、primefaces 和 Spring 4.0 以及 spring-ldap-core 1.3.2 和 spring-security-ldap-3.2.0。以下是我到目前为止所做的工作:

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>

我的 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);
}

这里的用户名和密码来自登录屏幕作为输入。我的问题是它的硬编码。我不想在 Spring-Ldap.xml 中硬编码 usernamepassword ,所以有人建议在这里使用 Spring-security-Ldap @ 987654321@但我看不懂。

我的问题是如何实现 Ldap 与我用作前端控制器的 spring 和 corse JSF 的动态集成。 任何帮助都会很棒。

【问题讨论】:

  • 动态集成是什么意思?您需要用户使用 LDAP 身份验证或其他方式登录吗?
  • @EmanueleRighetto 在某种意义上是动态的,我必须将管理器凭据硬编码在 Spring-Ldap.xml 文件中以连接 ldap-server。我想动态连接,我不想在 spring-ldap.xml 中使用硬编码凭据。
  • api 已弃用

标签: spring ldap spring-ldap


【解决方案1】:

我发现这些文章有助于使用 Spring Security 设置登录表单,但是它们不使用 jsf:

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

发现这篇文章对使用ldap作为身份验证提供者很有帮助,它没有使用ldapTemplate,而是使用spring-security配置(文章中的spring-security.xml)

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

【讨论】:

  • 感谢您的回答,但这不是我想要的,
【解决方案2】:

这就是我使用 LDAP 进行身份验证的方式:

  1. 导入 Maven 依赖项

    <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. 写下你对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;
        }
    }
    
    }
    

【讨论】:

    猜你喜欢
    • 2011-12-05
    • 1970-01-01
    • 2021-09-02
    • 2017-04-12
    • 1970-01-01
    • 2016-09-09
    • 2022-01-20
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多