使用 LDAP Auth 的 Springboot 未找到搜索结果,基本:''

发布于 2025-01-10 06:16:35 字数 3508 浏览 4 评论 0原文

我正在尝试将我的 SpringBoot 应用程序连接到 ldap 服务器。 (未嵌入) 我尝试连接时的问题是:

try auth
2022-02-26 20:31:12.593  INFO 19692 --- [nio-8080-exec-2] o.s.ldap.core.LdapTemplate               : No results found for search, base: ''; filter: '([email protected])'.
auth FAIL

我不明白为什么基“”是空的,因为我在属性中指定了它。 我不知道这是否是唯一的问题,如果可以的话请告诉我。谢谢!

  ldap:
    urls: ldap://dig.intra.company.fr:389
    base: OU=UTILISATEURS,DC=dig,DC=intra,DC=company,DC=fr
    username: CN=S_BELUGA,CN=Users,DC=dig,DC=intra,DC=company,DC=fr
    password: Password2022
    anonymous-read-only: false

端点

@Autowired
    private AuthenticationManager authenticationManager;
    
    @Operation(summary = "Authentification LDAP")
    @PostMapping(value = "/ldapAuth", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public void ldap(@Valid @RequestBody UserAuthentificationDTO userAuth) {
    
        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(userAuth.getEmail(),
            userAuth.getPassword()));
      
    }

WebSecurityConfig

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  private JwtTokenProvider jwtTokenProvider;
  private OpenLdapAuthenticationProvider openLdapAuthenticationProvider;

  public WebSecurityConfig(OpenLdapAuthenticationProvider openLdapAuthenticationProvider,
      JwtTokenProvider jwtTokenProvider) {
    this.openLdapAuthenticationProvider = openLdapAuthenticationProvider;
    this.jwtTokenProvider = jwtTokenProvider;
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {  
    auth.authenticationProvider(openLdapAuthenticationProvider);
  }

OpenLdapAuthenticationProvider

@Component
public class OpenLdapAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private LdapTemplate ldapTemplate;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        System.out.println("try auth");
        Filter filter = new EqualsFilter("uid", authentication.getName());
        Boolean authenticate = ldapTemplate.authenticate(LdapUtils.emptyLdapName(), filter.encode(),
                authentication.getCredentials().toString());
        if (authenticate) {
            System.out.println("utilisateur authentifié avec ldap");
            List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
            grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
            UserDetails userDetails = new User(authentication.getName(), authentication.getCredentials().toString(),
                    grantedAuthorities);
            Authentication auth = new UsernamePasswordAuthenticationToken(userDetails,
                    authentication.getCredentials().toString(), grantedAuthorities);
            return auth;

        } else {
            System.out.println("auth FAIL");
            return null;
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}

I'm trying to connect my SpringBoot app to the ldap server. (not embedded)
The problem while i'm trying to connect is :

try auth
2022-02-26 20:31:12.593  INFO 19692 --- [nio-8080-exec-2] o.s.ldap.core.LdapTemplate               : No results found for search, base: ''; filter: '([email protected])'.
auth FAIL

I don't understand why the base '' is empty because i specified it in the properties..
I don't know if it is the only problem let me know if you can. Thanks!

  ldap:
    urls: ldap://dig.intra.company.fr:389
    base: OU=UTILISATEURS,DC=dig,DC=intra,DC=company,DC=fr
    username: CN=S_BELUGA,CN=Users,DC=dig,DC=intra,DC=company,DC=fr
    password: Password2022
    anonymous-read-only: false

Endpoint

@Autowired
    private AuthenticationManager authenticationManager;
    
    @Operation(summary = "Authentification LDAP")
    @PostMapping(value = "/ldapAuth", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public void ldap(@Valid @RequestBody UserAuthentificationDTO userAuth) {
    
        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(userAuth.getEmail(),
            userAuth.getPassword()));
      
    }

WebSecurityConfig

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  private JwtTokenProvider jwtTokenProvider;
  private OpenLdapAuthenticationProvider openLdapAuthenticationProvider;

  public WebSecurityConfig(OpenLdapAuthenticationProvider openLdapAuthenticationProvider,
      JwtTokenProvider jwtTokenProvider) {
    this.openLdapAuthenticationProvider = openLdapAuthenticationProvider;
    this.jwtTokenProvider = jwtTokenProvider;
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {  
    auth.authenticationProvider(openLdapAuthenticationProvider);
  }

OpenLdapAuthenticationProvider

@Component
public class OpenLdapAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private LdapTemplate ldapTemplate;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        System.out.println("try auth");
        Filter filter = new EqualsFilter("uid", authentication.getName());
        Boolean authenticate = ldapTemplate.authenticate(LdapUtils.emptyLdapName(), filter.encode(),
                authentication.getCredentials().toString());
        if (authenticate) {
            System.out.println("utilisateur authentifié avec ldap");
            List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
            grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
            UserDetails userDetails = new User(authentication.getName(), authentication.getCredentials().toString(),
                    grantedAuthorities);
            Authentication auth = new UsernamePasswordAuthenticationToken(userDetails,
                    authentication.getCredentials().toString(), grantedAuthorities);
            return auth;

        } else {
            System.out.println("auth FAIL");
            return null;
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔书几行 2025-01-17 06:16:35

问题出在“uid”上。应将其替换为“mail”属性。

The problem was with "uid". It should be replaced by "mail" attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文