fra*_*rva 8 spring-security spring-ldap spring-boot reactive
为了使用 LDAP 保护 Reactive Spring Boot 应用程序,需要进行哪些自定义?到目前为止,我看到的示例都是基于 Spring MVC 的,而保护 WebFlux 的示例仅显示了一个带有内存映射的简单反应式示例。
这是我提出并测试的一种解决方案。
值得特别注意的是此类中的此信息: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;
}
}
Run Code Online (Sandbox Code Playgroud)
之后,您需要按照此处的模式配置您的安全性。最基本的链配置大概是这样的:
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.anyExchange().authenticated()
.and()
.httpBasic();
return http.build();
}
Run Code Online (Sandbox Code Playgroud)
为了完整起见,您需要确保您拥有这些:
<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>
Run Code Online (Sandbox Code Playgroud)
其他参考
| 归档时间: |
|
| 查看次数: |
2925 次 |
| 最近记录: |