|
@@ -1,13 +1,18 @@
|
|
|
package com.provectus.kafka.ui.config.auth;
|
|
|
|
|
|
+import com.provectus.kafka.ui.service.rbac.extractor.RbacLdapAuthoritiesExtractor;
|
|
|
+import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+import org.springframework.ldap.core.DirContextOperations;
|
|
|
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
|
|
|
import org.springframework.ldap.core.support.LdapContextSource;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
@@ -16,12 +21,17 @@ import org.springframework.security.authentication.ReactiveAuthenticationManager
|
|
|
import org.springframework.security.authentication.ReactiveAuthenticationManagerAdapter;
|
|
|
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
|
|
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.security.ldap.authentication.AbstractLdapAuthenticationProvider;
|
|
|
import org.springframework.security.ldap.authentication.BindAuthenticator;
|
|
|
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
|
|
|
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
|
|
|
import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;
|
|
|
import org.springframework.security.ldap.search.LdapUserSearch;
|
|
|
+import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;
|
|
|
+import org.springframework.security.ldap.userdetails.LdapUserDetailsMapper;
|
|
|
+import org.springframework.security.ldap.userdetails.UserDetailsContextMapper;
|
|
|
import org.springframework.security.web.server.SecurityWebFilterChain;
|
|
|
|
|
|
@Configuration
|
|
@@ -31,7 +41,7 @@ import org.springframework.security.web.server.SecurityWebFilterChain;
|
|
|
@Slf4j
|
|
|
public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
|
|
|
|
|
|
- @Value("${spring.ldap.urls}")
|
|
|
+ @Value("${spring.ldap.urls}") // TODO properties
|
|
|
private String ldapUrls;
|
|
|
@Value("${spring.ldap.dn.pattern:#{null}}")
|
|
|
private String ldapUserDnPattern;
|
|
@@ -50,7 +60,8 @@ public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
|
|
|
private String activeDirectoryDomain;
|
|
|
|
|
|
@Bean
|
|
|
- public ReactiveAuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
|
|
|
+ public ReactiveAuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource,
|
|
|
+ ApplicationContext context) {
|
|
|
BindAuthenticator ba = new BindAuthenticator(contextSource);
|
|
|
if (ldapUserDnPattern != null) {
|
|
|
ba.setUserDnPatterns(new String[] {ldapUserDnPattern});
|
|
@@ -63,7 +74,8 @@ public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
|
|
|
|
|
|
AbstractLdapAuthenticationProvider authenticationProvider;
|
|
|
if (!isActiveDirectory) {
|
|
|
- authenticationProvider = new LdapAuthenticationProvider(ba);
|
|
|
+ authenticationProvider = new LdapAuthenticationProvider(ba, authoritiesPopulator(context));
|
|
|
+ authenticationProvider.setUserDetailsContextMapper(userDetailsContextMapper());
|
|
|
} else {
|
|
|
authenticationProvider = new ActiveDirectoryLdapAuthenticationProvider(activeDirectoryDomain, ldapUrls);
|
|
|
authenticationProvider.setUseAuthenticationRequestCredentials(true);
|
|
@@ -75,6 +87,7 @@ public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
+ @Primary
|
|
|
public BaseLdapPathContextSource contextSource() {
|
|
|
LdapContextSource ctx = new LdapContextSource();
|
|
|
ctx.setUrl(ldapUrls);
|
|
@@ -109,5 +122,43 @@ public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
+ @Bean
|
|
|
+ public UserDetailsContextMapper userDetailsContextMapper() {
|
|
|
+ return new LdapUserDetailsMapper() {
|
|
|
+ @Override
|
|
|
+ public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
|
|
|
+ UserDetails userDetails = super.mapUserFromContext(ctx, username, authorities);
|
|
|
+ return new RbacLdapUser(userDetails);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ LdapAuthoritiesPopulator authoritiesPopulator(ApplicationContext context) {
|
|
|
+ RbacLdapAuthoritiesExtractor extractor = new RbacLdapAuthoritiesExtractor(context);
|
|
|
+ extractor.setRolePrefix("");
|
|
|
+ return extractor;
|
|
|
+ }
|
|
|
+
|
|
|
+/* @Autowired
|
|
|
+ public void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
|
+ var a = auth
|
|
|
+ .ldapAuthentication()
|
|
|
+ .userDnPatterns("uid={0},ou=people")
|
|
|
+ .groupSearchBase("ou=groups")
|
|
|
+ .contextSource()
|
|
|
+ .url()
|
|
|
+ .managerDn()
|
|
|
+ .managerPassword()
|
|
|
+ .and()
|
|
|
+// .passwordCompare()
|
|
|
+// .passwordEncoder(new BCryptPasswordEncoder())
|
|
|
+// .passwordAttribute("userPassword");
|
|
|
+ ;
|
|
|
+ if (isActiveDirectory) {
|
|
|
+ a.authenticationProvider()
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
}
|
|
|
|