简体   繁体   English

Spring Security with LDAP - 登录后出错

[英]Spring Security with LDAP - error after login

I've managed to make my Spring project work with Security and LDAP, but only with an LDIF file.我已经设法使我的 Spring 项目与安全性和 LDAP 一起工作,但只能使用 LDIF 文件。 Now I'm trying to make it work by configuring it with an LDAP server, but I only get this far - I get no error on my website by logging in with correct credentials, but I cannot get any further than that.现在我正在尝试通过使用 LDAP 服务器配置它来使其工作,但我只能做到这一点 - 通过使用正确的凭据登录我的网站上没有错误,但我无法获得更多信息。

I have been googling these errors, but I could not find anything similar/useful that would help.我一直在谷歌搜索这些错误,但我找不到任何类似/有用的东西。 Perhaps someone who's been working with Spring LDAP knows how to properly configure a project to work with an LDAP server?也许使用 Spring LDAP 的人知道如何正确配置项目以使用 LDAP 服务器?

Here's what the console is throwing:这是控制台抛出的内容:

2020-10-31 18:32:25.298 DEBUG 21100 --- [nio-8080-exec-4] w.a.UsernamePasswordAuthenticationFilter : Request is to process authentication
2020-10-31 18:32:25.298 DEBUG 21100 --- [nio-8080-exec-4] o.s.s.authentication.ProviderManager     : Authentication attempt using org.springframework.security.ldap.authentication.LdapAuthenticationProvider
2020-10-31 18:32:25.298 DEBUG 21100 --- [nio-8080-exec-4] o.s.s.l.a.LdapAuthenticationProvider     : Processing authentication request for user: MY_USERNAME
2020-10-31 18:32:25.302 DEBUG 21100 --- [nio-8080-exec-4] o.s.s.l.a.BindAuthenticator              : Attempting to bind as cn=MY_USERNAME,ou=MY_GROUP,dc=FOO,dc=FOO2
2020-10-31 18:32:25.302 DEBUG 21100 --- [nio-8080-exec-4] s.s.l.DefaultSpringSecurityContextSource : Removing pooling flag for user cn=MY_USERNAME,ou=MY_GROUP,dc=FOO,dc=FOO2
2020-10-31 18:32:25.384 DEBUG 21100 --- [nio-8080-exec-4] o.s.s.l.a.BindAuthenticator              : Retrieving attributes...
2020-10-31 18:32:25.408 DEBUG 21100 --- [nio-8080-exec-4] .s.s.l.u.DefaultLdapAuthoritiesPopulator : Getting authorities for user cn=MY_USERNAME,ou=MY_GROUP,dc=FOO,dc=FOO2
2020-10-31 18:32:25.408 DEBUG 21100 --- [nio-8080-exec-4] .s.s.l.u.DefaultLdapAuthoritiesPopulator : Searching for roles for user 'MY_USERNAME', DN = 'cn=MY_USERNAME,ou=MY_GROUP,dc=FOO,dc=FOO2', with filter (uniqueMember={0}) in search base ''
2020-10-31 18:32:25.409 DEBUG 21100 --- [nio-8080-exec-4] o.s.s.ldap.SpringSecurityLdapTemplate    : Using filter: (uniqueMember=cn=MY_USERNAME,ou=MY_GROUP,dc=FOO,dc=FOO2)
2020-10-31 18:32:25.490 DEBUG 21100 --- [nio-8080-exec-4] o.s.s.w.header.writers.HstsHeaderWriter  : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@18100b72
2020-10-31 18:32:25.490 DEBUG 21100 --- [nio-8080-exec-4] w.c.HttpSessionSecurityContextRepository : SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2020-10-31 18:32:25.490 DEBUG 21100 --- [nio-8080-exec-4] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
2020-10-31 18:32:25.494 ERROR 21100 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception

org.springframework.ldap.UncategorizedLdapException: Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - 00002020: Operation unavailable without authentication]; remaining name ''

Here's my application.properties:这是我的 application.properties:

spring.ldap.embedded.base-dn=dc=FOO,dc=FOO2
spring.ldap.embedded.port=8389
spring.ldap.urls=ldap://xx.x.x.xx:389/dc=FOO,dc=FOO2
spring.ldap.embedded.validation.enabled=false
spring.ldap.base=ou=MY_GROUP,dc=FOO,dc=FOO2

And the cofig class that extends WebSecurityConfigurerAdapter:以及扩展 WebSecurityConfigurerAdapter 的 cofig 类:

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth
                .ldapAuthentication()
                .userDnPatterns("cn={0},ou=MY_GROUP")
                .contextSource()
                .url("ldap://xx.x.x.xx:389/dc=FOO,dc=FOO2");
    }

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

Had the same issue - Your LDAP server needs bind authentication.有同样的问题 - 您的 LDAP 服务器需要绑定身份验证。 Adding below solved it:添加以下解决了它:

.managerDn("manager").managerPassword("password") .managerDn("manager").managerPassword("密码")

WebSecurityConfigurerAdapter (in bold): WebSecurityConfigurerAdapter (粗体):

auth .ldapAuthentication() .userDnPatterns("uid={0},ou=users,DC=company,DC=com") .groupSearchBase("ou=groups") .contextSource() .url("ldap://XXXX:389/DC=company,DC=com") .managerDn("manager").managerPassword("password") .and() .passwordCompare() .passwordEncoder(new BCryptPasswordEncoder()) .passwordAttribute("userPassword"); auth .ldapAuthentication() .userDnPatterns("uid={0},ou=users,DC=company,DC=com") .groupSearchBase("ou=groups") .contextSource() .url("ldap://XXXX :389/DC=company,DC=com") .managerDn("manager").managerPassword("password") .and() .passwordCompare() .passwordEncoder(new BCryptPasswordEncoder()) .passwordAttribute("userPassword");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM