当前位置: 首页 > 知识库问答 >
问题:

来自登录表单的Spring Security Ldap身份验证userDn和密码

东门航
2023-03-14

我试图实现一个Spring Security LDAP身份验证使用WebSecurityConfigrerAdapter。

到目前为止它工作正常,但我的问题是我不希望上下文的用户名和密码被硬编码。它必须是用户的登录名和密码,所以我的问题是如何从登录表单中构建用户名和密码的上下文和设置?

这是我正在使用的代码:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().fullyAuthenticated()
                .and()
            .formLogin();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
                .userSearchFilter("(sAMAccountName={0})")
                .contextSource(contextSource());
    }

    @Bean
    public BaseLdapPathContextSource contextSource() {
        LdapContextSource bean = new LdapContextSource();
        bean.setUrl("ldap://10.10.10.10:389");
        bean.setBase("DC=myDomaine,DC=com");
        //instead of this i want to put here the username and password provided by the user
        bean.setUserDn("myDomaine\\username");
        bean.setPassword("password");
        bean.setPooled(true);
        bean.setReferral("follow");
        bean.afterPropertiesSet();
        return bean;
    }
}

非常感谢。

共有3个答案

桂宏旷
2023-03-14

通过不重写自己的LdapAuthenticationProvider来节省时间,现有的ActiveDirectoryLdapAuthenticationProvider将使用收到的凭据对LDAP进行身份验证,如果您想做更多的事情,还可以添加搜索筛选器(例如,查看用户是否也属于特定组)

相关文档:

https://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ldap-活动目录

https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/ldap/authentication/ad/ActiveDirectoryLdapAuthenticationProvider.html

示例代码段:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private LdapProperties ldapProperties;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
    }

    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        ActiveDirectoryLdapAuthenticationProvider authenticationProvider =
                new ActiveDirectoryLdapAuthenticationProvider(ldapProperties.getDomain(), ldapProperties.getProviderUrl());

        authenticationProvider.setConvertSubErrorCodesToExceptions(true);
        authenticationProvider.setUseAuthenticationRequestCredentials(true);
        //if you're not happy on the default searchFilter, you can set your own. See https://docs.spring.io/spring-security/site/docs/4.2.18.RELEASE/apidocs/org/springframework/security/ldap/authentication/ad/ActiveDirectoryLdapAuthenticationProvider.html#setSearchFilter-java.lang.String-
        authenticationProvider.setSearchFilter("(&(objectClass=user)(cn={1}))");
        return authenticationProvider;
    }
...
}
应煌
2023-03-14

contextSource中的userDN和password参数是必需的参数。这就像管理员用户名和密码一样,您可以获取或创建到ldap服务器的初始连接。

以便您能够从登录表单中验证用户名和密码。您可以使用ldapTemplate:

        @Bean
    public BaseLdapPathContextSource contextSource() {
        LdapContextSource bean = new LdapContextSource();
        bean.setUrl("ldap://10.10.10.10:389");
        bean.setBase("DC=myDomaine,DC=com");
        //instead of this i want to put here the username and password provided by the user
        bean.setUserDn("myDomaine\\username");
        bean.setPassword("password");
        bean.setPooled(true);
        bean.setReferral("follow");
        bean.afterPropertiesSet();
        return bean;
    }



  @Bean
  public LdapTemplate ldapTemplate() {
      LdapTemplate template = new LdapTemplate(contextSource());

      return template;
  }

然后在服务类实现中使用:

@Service
public class LdapUserServiceImpl implements LdapUserService, BaseLdapNameAware {
    @Autowired
    protected ContextSource contextSource;
    @Autowired
    protected LdapTemplate ldapTemplate;

    @Override
    public boolean authenticate(String userDn, String credentials) {
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("sAMAccountName", userDn));

        return ldapTemplate.authenticate("", filter.toString(), credentials);
    }

}

然后调用此服务,通过登录表单传递用户名和密码,如下所示:

boolean isAuthenticated = ldapUserService.authenticate(loginForm.getUsername(), loginForm.getPassword());
古彦
2023-03-14

你的代码应该工作得很好。硬编码的用户名和密码仅用于创建与ldap服务器的绑定。登录表单中提供的用户名和密码仅使用您的代码进行身份验证。

我使用以下代码执行ldap身份验证。

public void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication().userSearchFilter("sAMAccountName={0}").contextSource().url(this.ldapUrl).managerDn(this.managerDn)
    .managerPassword(this.managerPassword);
}

其中,manager是用于创建与服务器绑定的ldap帐户。

 类似资料:
  • 在控制台中写入函数:anonymousUser true[ROLE_ANONYMOUS] 并且应该是user1 true[ROLE_USER]user1

  • 我所期望的是:当用户从表单登录时,它可以调用restful服务,而无需经过基本身份验证(因为它已经经过身份验证)。我的想法是,角色为'role_user'的用户也应该调用restful服务。但是,当我从表单登录后,我也被提示进行基本身份验证,试图从浏览器调用restful服务。 有没有得到我所期望的?

  • 我有一个Spring启动的应用程序,用户可以通过登录和密码或数字证书进行身份验证。我试图用用户和密码验证,但当我试图用证书验证时,chrome不显示窗口来选择我想在验证中使用的证书(我有几个证书,服务器是安全的,等等...) 有什么想法吗?我附上我的WebSecurity配置适配器的代码,以防我出错 配置属性包括: 服务器ssl。密钥存储=存储/密钥存储。jks服务器。ssl。密钥存储密码=更改I

  • 使用,https://github.com/firebase/FirebaseUI-Android/tree/master/codelabs/chat作为登录的参考,我在键入时似乎遇到了问题 我只能键入Auth的提供者,而不能键入,为什么会这样,它提示我键入社会提供者。

  • 我使用Laravel 5.2。我想问一个问题。如何更改重定向登录表单在laravel当我们已经做登录?我使用的是来自Php artisan make: auth的auth。 我已经登录并重定向到localhost:8000/admin 然后我再次打开localhost:8000/登录。它将重定向到“/”或localhost:8000/ 我的问题是如何将“/”更改为“/”管理员“?因此,即使我已经登

  • 我有spring boot应用程序和thymeleaf。我正在使用spring security formLogin方法来实现安全性,现在我只需要为一些API添加JWT。 注意:如果我更改订单并使formLogin@订单(1)再次生效,但当然不会生效。 我也试着把它们像这样结合起来,现在两者都很好,但是如果JWT身份验证错误将返回formlogin thymeleaf错误页面,则异常处理的问题: