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 ;
29
27
import org .springframework .security .ldap .authentication .AbstractLdapAuthenticationProvider ;
30
28
import org .springframework .security .ldap .authentication .BindAuthenticator ;
31
29
import org .springframework .security .ldap .authentication .LdapAuthenticationProvider ;
30
+ import org .springframework .security .ldap .authentication .NullLdapAuthoritiesPopulator ;
32
31
import org .springframework .security .ldap .authentication .ad .ActiveDirectoryLdapAuthenticationProvider ;
32
+ import org .springframework .security .ldap .authentication .ad .DefaultActiveDirectoryAuthoritiesPopulator ;
33
33
import org .springframework .security .ldap .search .FilterBasedLdapUserSearch ;
34
34
import org .springframework .security .ldap .search .LdapUserSearch ;
35
- import org .springframework .security .ldap .userdetails .DefaultLdapAuthoritiesPopulator ;
36
35
import org .springframework .security .ldap .userdetails .LdapAuthoritiesPopulator ;
37
36
import org .springframework .security .ldap .userdetails .LdapUserDetailsMapper ;
38
37
import org .springframework .security .web .server .SecurityWebFilterChain ;
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 ) {
50
+ public ReactiveAuthenticationManager authenticationManager (AbstractLdapAuthenticationProvider authProvider ) {
51
+ return new ReactiveAuthenticationManagerAdapter (new ProviderManager (List .of (authProvider )));
52
+ }
53
+
54
+ @ Bean
55
+ public AbstractLdapAuthenticationProvider authenticationProvider (LdapAuthoritiesPopulator authoritiesExtractor ,
56
+ BindAuthenticator bindAuthenticator ,
57
+ AccessControlService acs ) {
54
58
var rbacEnabled = acs .isRbacEnabled ();
59
+
60
+ AbstractLdapAuthenticationProvider authProvider ;
61
+
62
+ if (!props .isActiveDirectory ()) {
63
+ authProvider = new LdapAuthenticationProvider (bindAuthenticator , authoritiesExtractor );
64
+ } else {
65
+ authProvider = new ActiveDirectoryLdapAuthenticationProvider (props .getActiveDirectoryDomain (),
66
+ props .getUrls ());
67
+ authProvider .setUseAuthenticationRequestCredentials (true );
68
+ ((ActiveDirectoryLdapAuthenticationProvider ) authProvider ).setAuthoritiesPopulator (authoritiesExtractor );
69
+ }
70
+
71
+ if (rbacEnabled ) {
72
+ authProvider .setUserDetailsContextMapper (new RbacUserDetailsMapper ());
73
+ }
74
+
75
+ return authProvider ;
76
+ }
77
+
78
+ @ Bean
79
+ public BindAuthenticator ldapBindAuthentication (LdapContextSource ldapContextSource ) {
55
80
BindAuthenticator ba = new BindAuthenticator (ldapContextSource );
81
+
56
82
if (props .getBase () != null ) {
57
83
ba .setUserDnPatterns (new String [] {props .getBase ()});
58
84
}
85
+
59
86
if (props .getUserFilterSearchFilter () != null ) {
60
87
LdapUserSearch userSearch =
61
88
new FilterBasedLdapUserSearch (props .getUserFilterSearchBase (), props .getUserFilterSearchFilter (),
62
89
ldapContextSource );
63
90
ba .setUserSearch (userSearch );
64
91
}
65
92
66
- var authenticationProvider = getAuthenticationProvider (authoritiesExtractor , rbacEnabled , ba );
67
-
68
- AuthenticationManager am = new ProviderManager (List .of (authenticationProvider ));
69
-
70
- return new ReactiveAuthenticationManagerAdapter (am );
71
- }
72
-
73
- private AbstractLdapAuthenticationProvider getAuthenticationProvider (LdapAuthoritiesPopulator authoritiesExtractor ,
74
- boolean rbacEnabled ,
75
- BindAuthenticator bindAuthenticator ) {
76
- AbstractLdapAuthenticationProvider authenticationProvider ;
77
-
78
- if (!props .isActiveDirectory ()) {
79
- authenticationProvider = rbacEnabled
80
- ? new LdapAuthenticationProvider (bindAuthenticator , authoritiesExtractor )
81
- : new LdapAuthenticationProvider (bindAuthenticator );
82
- } else {
83
- authenticationProvider = new ActiveDirectoryLdapAuthenticationProvider (props .getActiveDirectoryDomain (),
84
- props .getUrls ());
85
- authenticationProvider .setUseAuthenticationRequestCredentials (true );
86
- }
87
-
88
- if (rbacEnabled ) {
89
- authenticationProvider .setUserDetailsContextMapper (new UserDetailsMapper ());
90
- }
91
- return authenticationProvider ;
93
+ return ba ;
92
94
}
93
95
94
96
@ Bean
@@ -102,28 +104,27 @@ public LdapContextSource ldapContextSource() {
102
104
}
103
105
104
106
@ Bean
105
- public DefaultLdapAuthoritiesPopulator ldapAuthoritiesExtractor (ApplicationContext context ,
106
- BaseLdapPathContextSource contextSource ,
107
- AccessControlService acs ) {
108
- if (props .isActiveDirectory ()) {
109
- return null ;
110
- }
107
+ public LdapAuthoritiesPopulator authoritiesExtractor (ApplicationContext ctx ,
108
+ BaseLdapPathContextSource ldapCtx ,
109
+ AccessControlService acs ) {
110
+ if (!props .isActiveDirectory ()) {
111
+ if (!acs .isRbacEnabled ()) {
112
+ return new NullLdapAuthoritiesPopulator ();
113
+ }
111
114
112
- var rbacEnabled = acs != null && acs . isRbacEnabled ( );
115
+ var extractor = new RbacLdapAuthoritiesExtractor ( ctx , ldapCtx , props . getGroupFilterSearchBase () );
113
116
114
- DefaultLdapAuthoritiesPopulator extractor ;
117
+ Optional .ofNullable (props .getGroupFilterSearchFilter ()).ifPresent (extractor ::setGroupSearchFilter );
118
+ extractor .setRolePrefix ("" );
119
+ extractor .setConvertToUpperCase (false );
120
+ extractor .setSearchSubtree (true );
115
121
116
- if (rbacEnabled ) {
117
- extractor = new RbacLdapAuthoritiesExtractor (context , contextSource , props .getGroupFilterSearchBase ());
122
+ return extractor ;
118
123
} else {
119
- extractor = new DefaultLdapAuthoritiesPopulator (contextSource , props .getGroupFilterSearchBase ());
124
+ return acs .isRbacEnabled ()
125
+ ? new RbacActiveDirectoryAuthoritiesExtractor (ctx )
126
+ : new DefaultActiveDirectoryAuthoritiesPopulator ();
120
127
}
121
-
122
- Optional .ofNullable (props .getGroupFilterSearchFilter ()).ifPresent (extractor ::setGroupSearchFilter );
123
- extractor .setRolePrefix ("" );
124
- extractor .setConvertToUpperCase (false );
125
- extractor .setSearchSubtree (true );
126
- return extractor ;
127
128
}
128
129
129
130
@ Bean
@@ -145,7 +146,7 @@ public SecurityWebFilterChain configureLdap(ServerHttpSecurity http) {
145
146
.build ();
146
147
}
147
148
148
- private static class UserDetailsMapper extends LdapUserDetailsMapper {
149
+ private static class RbacUserDetailsMapper extends LdapUserDetailsMapper {
149
150
@ Override
150
151
public UserDetails mapUserFromContext (DirContextOperations ctx , String username ,
151
152
Collection <? extends GrantedAuthority > authorities ) {
0 commit comments