【问题标题】:Spring Security WebFlux and LDAPSpring Security WebFlux 和 LDAP
【发布时间】:2018-11-03 12:45:03
【问题描述】:

为了使用 LDAP 保护反应式 Spring Boot 应用程序,需要进行哪些自定义?到目前为止,我看到的示例都是基于 Spring MVC 的,而用于保护 WebFlux 的示例仅显示了一个带有内存映射的简单响应式示例。

【问题讨论】:

    标签: spring-boot spring-security spring-ldap reactive


    【解决方案1】:

    这是我想出并测试过的一种解决方案。

    值得特别注意的是此类中的以下信息:ReactiveAuthenticationManagerAdapter。在那里,它指出:

    使 AuthenticationManager 适应反应式 API。这有点 必要的,因为存储凭据的许多方式(即 JDBC、LDAP 等)没有反应式实现。更重要的是 通常认为将密码存储在哈希中是最佳实践 这是故意缓慢的,这会阻止任何请求的到来 除非它被放在另一个线程上。

    首先,创建一个配置类。这将处理与 LDAP 的连接。

    @Configuration
    public class ReactiveLdapAuthenticationConfig {
    
        // Set this in your application.properties, or hardcode if you want.
        @Value("${spring.ldap.urls}")
        private String ldapUrl;
    
        @Bean
        ReactiveAuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
    
            BindAuthenticator ba = new BindAuthenticator(contextSource);
            ba.setUserDnPatterns(new String[] { "cn={0},ou=people" } );
    
            LdapAuthenticationProvider lap = new LdapAuthenticationProvider(ba);
    
            AuthenticationManager am = new ProviderManager(Arrays.asList(lap));
    
            return new ReactiveAuthenticationManagerAdapter(am);
    
        }
    
        @Bean
        BaseLdapPathContextSource contextSource() {
            LdapContextSource ctx = new LdapContextSource();
            ctx.setUrl(ldapUrl);
            ctx.afterPropertiesSet();
            return ctx;
        }
    
    }
    

    之后,您需要按照here 模式配置您的安全性。最基本的链配置大概是这样的:

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http
            .authorizeExchange()
                .anyExchange().authenticated()
                .and()
            .httpBasic();
    
        return http.build();
    }
    

    为了完整起见,您需要确保拥有这些:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
    </dependency>
    

    其他参考资料

    【讨论】:

      【解决方案2】:

      以上示例不适用于我使用 Windows Active Directory。我可以让 LDAP 身份验证在独立(非 Spring)Java 中工作,但上述解决方案总是给我错误 52e(用户已知,但密码无效)。

      根据上面的示例,我使用了相同的 pom.xml@EnableWebFluxSecurity ... SecurityWebFilterChain(...),但使用了以下内容;

      @Configuration
      public class ReactiveLdapAuthenticatoinConfig {
      
      @Bean
      ReactiveAuthenticationManager authenticationManager() {
      
         ActiveDirectoryLdapAuthenticationProvider adlap = 
            new ActiveDirectoryLdapAuthenticationProvider(
               "{my.domain}",
               "ldap://{my.ldap.server}.{my.domain}"
            );
      
         AuthenticationManager am = new ProviderManager(Arrays.asList(adlap));
         return new ReactiveAuthenticationManagerAdapter(am);
      }
      }
      

      为了返回登录用户,可以使用类似的东西;

      @GetMapping(value = '/user')
      public Mono<String> getUser(Mono<Principal> principal) {
         return principal.map(Principal::getName);
      }
      

      【讨论】:

        猜你喜欢
        • 2018-04-12
        • 2015-10-12
        • 1970-01-01
        • 2020-04-02
        • 2018-04-15
        • 2014-09-04
        • 2012-10-15
        • 2011-09-23
        • 2012-05-19
        相关资源
        最近更新 更多