gpt4 book ai didi

spring-boot - Spring Security WebFlux 和 LDAP

转载 作者:行者123 更新时间:2023-12-04 03:05:32 25 4
gpt4 key购买 nike

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

最佳答案

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

值得特别注意的是本类(class)中的以下信息:ReactiveAuthenticationManagerAdapter .在那里,它指出:

Adapts an AuthenticationManager to the reactive APIs. This is somewhat necessary because many of the ways that credentials are stored (i.e. JDBC, LDAP, etc) do not have reactive implementations. What's more is it is generally considered best practice to store passwords in a hash that is intentionally slow which would block ever request from coming in unless it was put on another thread.



首先,创建一个配置类。这将处理与 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>

其他引用
  • EnableReactiveMethodSecurity
  • BindAuthenticator
  • LdapAuthenticationProvider
  • ProviderManager
  • 关于spring-boot - Spring Security WebFlux 和 LDAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50506803/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com