1
1
package io .kafbat .ui .config .auth ;
2
2
3
- import static io .kafbat .ui .config .auth .AbstractAuthSecurityConfig .AUTH_WHITELIST ;
4
-
5
3
import io .kafbat .ui .service .rbac .AccessControlService ;
4
+ import io .kafbat .ui .service .rbac .extractor .RbacActiveDirectoryAuthoritiesExtractor ;
6
5
import io .kafbat .ui .service .rbac .extractor .RbacLdapAuthoritiesExtractor ;
7
6
import java .util .Collection ;
8
7
import java .util .List ;
17
16
import org .springframework .ldap .core .DirContextOperations ;
18
17
import org .springframework .ldap .core .support .BaseLdapPathContextSource ;
19
18
import org .springframework .ldap .core .support .LdapContextSource ;
20
- import org .springframework .security .authentication .AuthenticationManager ;
21
19
import org .springframework .security .authentication .ProviderManager ;
22
20
import org .springframework .security .authentication .ReactiveAuthenticationManager ;
23
21
import org .springframework .security .authentication .ReactiveAuthenticationManagerAdapter ;
30
28
import org .springframework .security .ldap .authentication .BindAuthenticator ;
31
29
import org .springframework .security .ldap .authentication .LdapAuthenticationProvider ;
32
30
import org .springframework .security .ldap .authentication .ad .ActiveDirectoryLdapAuthenticationProvider ;
31
+ import org .springframework .security .ldap .authentication .ad .DefaultActiveDirectoryAuthoritiesPopulator ;
33
32
import org .springframework .security .ldap .search .FilterBasedLdapUserSearch ;
34
33
import org .springframework .security .ldap .search .LdapUserSearch ;
35
34
import org .springframework .security .ldap .userdetails .DefaultLdapAuthoritiesPopulator ;
43
42
@ EnableConfigurationProperties (LdapProperties .class )
44
43
@ RequiredArgsConstructor
45
44
@ Slf4j
46
- public class LdapSecurityConfig {
45
+ public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
47
46
48
47
private final LdapProperties props ;
49
48
50
49
@ Bean
51
- public ReactiveAuthenticationManager authenticationManager (LdapContextSource ldapContextSource ,
52
- LdapAuthoritiesPopulator authoritiesExtractor ,
53
- AccessControlService acs ) {
54
- var rbacEnabled = acs .isRbacEnabled ();
55
- BindAuthenticator ba = new BindAuthenticator (ldapContextSource );
56
- if (props .getBase () != null ) {
57
- ba .setUserDnPatterns (new String [] {props .getBase ()});
58
- }
59
- if (props .getUserFilterSearchFilter () != null ) {
60
- LdapUserSearch userSearch =
61
- new FilterBasedLdapUserSearch (props .getUserFilterSearchBase (), props .getUserFilterSearchFilter (),
62
- ldapContextSource );
63
- ba .setUserSearch (userSearch );
64
- }
65
-
66
- var authenticationProvider = getAuthenticationProvider (authoritiesExtractor , rbacEnabled , ba );
67
-
68
- AuthenticationManager am = new ProviderManager (List .of (authenticationProvider ));
69
-
70
- return new ReactiveAuthenticationManagerAdapter (am );
50
+ public ReactiveAuthenticationManager authenticationManager (AbstractLdapAuthenticationProvider authProvider ) {
51
+ return new ReactiveAuthenticationManagerAdapter (new ProviderManager (List .of (authProvider )));
71
52
}
72
53
73
- private AbstractLdapAuthenticationProvider getAuthenticationProvider (LdapAuthoritiesPopulator authoritiesExtractor ,
74
- boolean rbacEnabled ,
75
- BindAuthenticator bindAuthenticator ) {
76
- AbstractLdapAuthenticationProvider authenticationProvider ;
54
+ @ Bean
55
+ public AbstractLdapAuthenticationProvider authenticationProvider (LdapAuthoritiesPopulator authoritiesExtractor ,
56
+ BindAuthenticator bindAuthenticator ,
57
+ AccessControlService acs ) {
58
+ var rbacEnabled = acs .isRbacEnabled ();
59
+
60
+ AbstractLdapAuthenticationProvider authProvider ;
77
61
78
62
if (!props .isActiveDirectory ()) {
79
- authenticationProvider = rbacEnabled
63
+ authProvider = rbacEnabled
80
64
? new LdapAuthenticationProvider (bindAuthenticator , authoritiesExtractor )
81
65
: new LdapAuthenticationProvider (bindAuthenticator );
82
66
} else {
83
- authenticationProvider = new ActiveDirectoryLdapAuthenticationProvider (props .getActiveDirectoryDomain (),
67
+ authProvider = new ActiveDirectoryLdapAuthenticationProvider (props .getActiveDirectoryDomain (),
84
68
props .getUrls ());
85
- authenticationProvider .setUseAuthenticationRequestCredentials (true );
69
+ authProvider .setUseAuthenticationRequestCredentials (true );
70
+ if (rbacEnabled ) {
71
+ ((ActiveDirectoryLdapAuthenticationProvider ) authProvider ).setAuthoritiesPopulator (authoritiesExtractor );
72
+ }
86
73
}
87
74
88
75
if (rbacEnabled ) {
89
- authenticationProvider .setUserDetailsContextMapper (new UserDetailsMapper ());
76
+ authProvider .setUserDetailsContextMapper (new RbacUserDetailsMapper ());
90
77
}
91
- return authenticationProvider ;
78
+
79
+ return authProvider ;
80
+ }
81
+
82
+ @ Bean
83
+ public BindAuthenticator ldapBindAuthentication (LdapContextSource ldapContextSource ) {
84
+ BindAuthenticator ba = new BindAuthenticator (ldapContextSource );
85
+
86
+ if (props .getBase () != null ) {
87
+ ba .setUserDnPatterns (new String [] {props .getBase ()});
88
+ }
89
+
90
+ if (props .getUserFilterSearchFilter () != null ) {
91
+ LdapUserSearch userSearch =
92
+ new FilterBasedLdapUserSearch (props .getUserFilterSearchBase (), props .getUserFilterSearchFilter (),
93
+ ldapContextSource );
94
+ ba .setUserSearch (userSearch );
95
+ }
96
+
97
+ return ba ;
92
98
}
93
99
94
100
@ Bean
@@ -102,28 +108,25 @@ public LdapContextSource ldapContextSource() {
102
108
}
103
109
104
110
@ Bean
105
- public DefaultLdapAuthoritiesPopulator ldapAuthoritiesExtractor (ApplicationContext context ,
106
- BaseLdapPathContextSource contextSource ,
107
- AccessControlService acs ) {
108
- if (props .isActiveDirectory ()) {
109
- return null ;
110
- }
111
-
112
- var rbacEnabled = acs != null && acs .isRbacEnabled ();
111
+ public LdapAuthoritiesPopulator authoritiesExtractor (ApplicationContext ctx ,
112
+ BaseLdapPathContextSource ldapCtx ,
113
+ AccessControlService acs ) {
114
+ if (!props .isActiveDirectory ()) {
115
+ DefaultLdapAuthoritiesPopulator extractor = acs .isRbacEnabled ()
116
+ ? new RbacLdapAuthoritiesExtractor (ctx , ldapCtx , props .getGroupFilterSearchBase ())
117
+ : new DefaultLdapAuthoritiesPopulator (ldapCtx , props .getGroupFilterSearchBase ());
113
118
114
- DefaultLdapAuthoritiesPopulator extractor ;
119
+ Optional .ofNullable (props .getGroupFilterSearchFilter ()).ifPresent (extractor ::setGroupSearchFilter );
120
+ extractor .setRolePrefix ("" );
121
+ extractor .setConvertToUpperCase (false );
122
+ extractor .setSearchSubtree (true );
115
123
116
- if (rbacEnabled ) {
117
- extractor = new RbacLdapAuthoritiesExtractor (context , contextSource , props .getGroupFilterSearchBase ());
124
+ return extractor ;
118
125
} else {
119
- extractor = new DefaultLdapAuthoritiesPopulator (contextSource , props .getGroupFilterSearchBase ());
126
+ return acs .isRbacEnabled ()
127
+ ? new RbacActiveDirectoryAuthoritiesExtractor (ctx )
128
+ : new DefaultActiveDirectoryAuthoritiesPopulator ();
120
129
}
121
-
122
- Optional .ofNullable (props .getGroupFilterSearchFilter ()).ifPresent (extractor ::setGroupSearchFilter );
123
- extractor .setRolePrefix ("" );
124
- extractor .setConvertToUpperCase (false );
125
- extractor .setSearchSubtree (true );
126
- return extractor ;
127
130
}
128
131
129
132
@ Bean
@@ -145,7 +148,7 @@ public SecurityWebFilterChain configureLdap(ServerHttpSecurity http) {
145
148
.build ();
146
149
}
147
150
148
- private static class UserDetailsMapper extends LdapUserDetailsMapper {
151
+ private static class RbacUserDetailsMapper extends LdapUserDetailsMapper {
149
152
@ Override
150
153
public UserDetails mapUserFromContext (DirContextOperations ctx , String username ,
151
154
Collection <? extends GrantedAuthority > authorities ) {
0 commit comments