在 Spring Security(spring-boot 项目)中使用 ldap 凭据进行 Http 基本身份验证以保护休息服务调用

Posted

技术标签:

【中文标题】在 Spring Security(spring-boot 项目)中使用 ldap 凭据进行 Http 基本身份验证以保护休息服务调用【英文标题】:Http basic authentication using ldap credentials in spring security(spring-boot project) to secure rest service calls 【发布时间】:2014-08-14 06:07:00 【问题描述】:

使用 Spring Security 基本身份验证设置适用于一些硬编码的用户名密码,如下所示:

http://thoughtfulsoftware.wordpress.com/2013/12/08/adding-security-to-spring-guides-rest-service/

所以尝试扩展它以使用 LDAP。

使用我们的 LDAP 服务器完成了 LDAP 身份验证设置

现在,当我尝试通过 REST 控制台插件调用我的 REST 服务时,会不断弹出授权窗口以输入用户名密码。如果我取消它授权失败,不知道我哪里出错了

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter 
@Override
    protected void configure(HttpSecurity http) throws Exception http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .csrf().disable()
        .httpBasic();


    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception 
        authManagerBuilder
            .ldapAuthentication()
            .userSearchFilter("(uid=0)").userSearchBase("ou=people,dc=zzz,dc=xxxx,dc=yyy")
            .groupRoleAttribute("cn").groupSearchFilter("(member=0)")
                //.userDnPatterns("uid=0,ou=people")
                //.groupSearchBase("ou=groups")
                .contextSource().url("ldaps://ldap.xxxx.yyy:636/cn=cw-grpreader,ou=people,dc=xxx,dc=xxxx,dc=xxx")
                .managerDn("cn=xx-xxr,ou=people,dc=med,dc=xxxx,dc=xxx")
                .managerPassword("#$%^^");
    

这是我尝试的一种方法,它会弹出重复的身份验证 如果我取消我得到的弹出窗口 HTTP 状态 401 - [LDAP:错误代码 49 - NDS 错误:身份验证失败 (-669)];即使凭据正确也会出错 链接到一些教程将非常有帮助

【问题讨论】:

发布您的配置。 听起来很有效。您可以在对话框中输入您的 ldap 凭据并进行身份验证吗? (发布您的配置。) @Dave 我正在输入凭据,但它又回来了 添加配置 至少看起来您正在连接到服务器。你有没有分别测试 url 和 DN?我怀疑只是 LDAP 配置有问题(尽管我建议将其拉到单独的 GlobalAuthenticationConfigurerAdapter 中)。 @Dave 我现在按照建议使用 GlobalAuthenticationConfigurerAdapter。似乎 httpbasic 身份验证仍然无法针对 LDAP 服务进行身份验证(HTTP Status 401 - Bad credentials with status report This request requires HTTP authentication) 【参考方案1】:

这是由于您设置了 httpbasic 身份验证。 Spring Boot 会生成一个随机密码,该密码显示在您可以使用的控制台上。

要禁用该弹出窗口,只需将其添加到您的 application.properties 文件中即可。

security.basic.enabled: false

【讨论】:

以上是关于在 Spring Security(spring-boot 项目)中使用 ldap 凭据进行 Http 基本身份验证以保护休息服务调用的主要内容,如果未能解决你的问题,请参考以下文章

Spring security:Spring security 如何在 SessionRegistry 中注册新会话?

Spring 中的 spring-security-oauth2 与 spring-security-oauth2-core

spring boot 整合spring security中spring security版本升级的遇到的坑

仅使用 Spring-Security(或)Spring 进行授权

使用 spring-session 和 spring-cloud-security 时,OAuth2ClientContext (spring-security-oauth2) 不会保留在 Redis

spring-security