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

如何从Spring Security获得与Ldap一起运行的Spring Boot执行器LdapHealthIndicator?

宁良平
2023-03-14

我正在使用spring Security开发一个spring Boot2.3应用程序。通过spring security针对广告进行身份验证和授权。所以我使用spring-security-ldap和下面的代码。

public class SecurityConfiguration extends WebSecurityConfigurerAdapter  {
...
    public AuthenticationProvider adAuthenticationProvider() {

        ActiveDirectoryLdapAuthenticationProvider adProvider =
            new ActiveDirectoryLdapAuthenticationProvider(ldapDomain, ldapUrl);
        adProvider.setSearchFilter(ldapSearchFilter);

        adProvider.setAuthoritiesMapper(authorities -> {
            Collection<GrantedAuthority> gaCollection = new ArrayList<>();
            for (GrantedAuthority authority : authorities) {
                if ("admin".equals(authority.getAuthority())) {
                    gaCollection.add(new SimpleGrantedAuthority(Role.ADMIN));
                }
            }
            return gaCollection;
        });
        return adProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.authenticationProvider(adAuthenticationProvider());
        auth.eraseCredentials(false);
    }

}

相关的依赖关系应该是:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
...
    </dependencyManagement>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-juli</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- End Spring -->
...
    </dependencies>

这个很管用。

CONDITIONS EVALUATION REPORT (only LDAP lines)

positive matches:
   LdapAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.ldap.core.ContextSource' (OnClassCondition)

   LdapAutoConfiguration#ldapContextSource matched:
      - @ConditionalOnMissingBean (types: org.springframework.ldap.core.support.LdapContextSource; SearchStrategy: all) did not find any beans (OnBeanCondition)

   LdapAutoConfiguration#ldapTemplate matched:
      - @ConditionalOnMissingBean (types: org.springframework.ldap.core.LdapOperations; SearchStrategy: all) did not find any beans (OnBeanCondition)

   LdapHealthContributorAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.ldap.core.LdapOperations' (OnClassCondition)
      - @ConditionalOnEnabledHealthIndicator management.health.ldap.enabled is true (OnEnabledHealthIndicatorCondition)
      - @ConditionalOnBean (types: org.springframework.ldap.core.LdapOperations; SearchStrategy: all) found bean 'ldapTemplate' (OnBeanCondition)

   LdapHealthContributorAutoConfiguration#ldapHealthContributor matched:
      - @ConditionalOnMissingBean (names: ldapHealthIndicator,ldapHealthContributor; SearchStrategy: all) did not find any beans (OnBeanCondition)

negative matches:
   EmbeddedLdapAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'com.unboundid.ldap.listener.InMemoryDirectoryServer' (OnClassCondition)

   LdapRepositoriesAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.springframework.data.ldap.repository.LdapRepository' (OnClassCondition)


o.s.b.actuate.ldap.LdapHealthIndicator   : LDAP health check failed
org.springframework.ldap.CommunicationException: localhost:389; nested exception is 
javax.naming.CommunicationException: localhost:389 
[Root exception is java.net.ConnectException: Connection refused: connect]

共有1个答案

孙风畔
2023-03-14

可能还有其他问题;但是,主要问题似乎是POM中缺少spring-ldap-core依赖项:

<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
</dependency>

包括它将把它放在类路径上。结合正确的属性,Spring Boot的LDAP自动配置将会使用。

此外,参考文档声明属性为spring.ldap.urls,因此我认为应该改为该属性。

 类似资料:
  • 我已经定制了我的执行器/信息endpoint,并且我想使用来自头的信息授权对另一个服务的调用。 我在这里实现了InfoContributor:https://www.baeldung.com/spring-boot-info-acture-custom 我想接受方法中的请求头。对于任何用户定义的RESTendpoint,我都可以定义参数并访问头。 但不幸的是,的方法只采用一个参数。 如何访问方法中

  • 我读过这篇文章,它说数据记录被组织成称为碎片的组,这些碎片可以被Lambda函数并行地消费和处理。我还从AWS webindar中找到了这些幻灯片,在幻灯片22中,您还可以看到Lambda函数并行使用不同的碎片。但是,我无法实现单个函数的并行执行。我创建了一个运行一分钟的简单lambda函数。然后,我开始在DynamoDB中创建大量的项目,希望获得大量的流记录。尽管如此,我的功能还是一个接一个地启

  • 在我运行mongo恢复后,mongo服务无法自动启动,但如果我打开终端并运行monstar,服务运行完美。如果我关闭终端,我得到。有什么建议吗? 错误:无法连接到服务器127.0.0.1 shell/mongo。js:79 当我运行mon神时,我得到: MongoDB启动:pid=1875 port=27017 dbpath=/data/db/64位周四25 12:16:40db version

  • 我正在用Apache Camel路由为Spring Boot应用程序创建一些单元测试,使用Spock作为测试框架,我需要模拟来自另一个应用程序的响应。我为此制作了一个模拟控制器,但我需要将测试正在运行的端口注入一个属性。有没有方法获得测试正在运行的端口? 我试过 另外,是否有方法在文件中注入该随机端口?理想情况下,我需要在文件中执行以下操作: 其中端口是运行测试的随机端口。

  • 我们什么时候应该使用Spring boot执行器。如果包括在内,它对应用程序内存和CPU使用有多大影响? 我目前正在使用Spring Boot 2. x。

  • 我在一个布局中使用了fab,但在运行时给出了以下错误:08-30 22:01:35.548 262 95-26295/? E/AndroidRuntime:致命异常:main process:com.example.ahr.a1000funnysms,pid:26295 Android.view.filflateException:二进制XML文件第30行:错误inflating类Android.