1
1
package io .kafbat .ui .config .auth ;
2
2
3
3
import io .kafbat .ui .service .rbac .AccessControlService ;
4
+ import io .kafbat .ui .service .rbac .extractor .RbacActiveDirectoryAuthoritiesExtractor ;
4
5
import io .kafbat .ui .service .rbac .extractor .RbacLdapAuthoritiesExtractor ;
5
6
import io .kafbat .ui .util .StaticFileWebFilter ;
6
7
import java .util .Collection ;
7
8
import java .util .List ;
8
9
import java .util .Optional ;
9
10
import lombok .RequiredArgsConstructor ;
10
11
import lombok .extern .slf4j .Slf4j ;
12
+ import org .springframework .beans .factory .annotation .Autowired ;
11
13
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
12
14
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
13
15
import org .springframework .context .ApplicationContext ;
17
19
import org .springframework .ldap .core .DirContextOperations ;
18
20
import org .springframework .ldap .core .support .BaseLdapPathContextSource ;
19
21
import org .springframework .ldap .core .support .LdapContextSource ;
20
- import org .springframework .security .authentication .AuthenticationManager ;
21
22
import org .springframework .security .authentication .ProviderManager ;
22
23
import org .springframework .security .authentication .ReactiveAuthenticationManager ;
23
24
import org .springframework .security .authentication .ReactiveAuthenticationManagerAdapter ;
29
30
import org .springframework .security .ldap .authentication .AbstractLdapAuthenticationProvider ;
30
31
import org .springframework .security .ldap .authentication .BindAuthenticator ;
31
32
import org .springframework .security .ldap .authentication .LdapAuthenticationProvider ;
33
+ import org .springframework .security .ldap .authentication .NullLdapAuthoritiesPopulator ;
32
34
import org .springframework .security .ldap .authentication .ad .ActiveDirectoryLdapAuthenticationProvider ;
35
+ import org .springframework .security .ldap .authentication .ad .DefaultActiveDirectoryAuthoritiesPopulator ;
33
36
import org .springframework .security .ldap .search .FilterBasedLdapUserSearch ;
34
37
import org .springframework .security .ldap .search .LdapUserSearch ;
35
- import org .springframework .security .ldap .userdetails .DefaultLdapAuthoritiesPopulator ;
36
38
import org .springframework .security .ldap .userdetails .LdapAuthoritiesPopulator ;
37
39
import org .springframework .security .ldap .userdetails .LdapUserDetailsMapper ;
38
40
import org .springframework .security .web .server .SecurityWebFilterChain ;
@@ -49,39 +51,51 @@ public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
49
51
private final LdapProperties props ;
50
52
51
53
@ Bean
52
- public ReactiveAuthenticationManager authenticationManager (LdapContextSource ldapContextSource ,
53
- LdapAuthoritiesPopulator authoritiesExtractor ,
54
- AccessControlService acs ) {
54
+ public ReactiveAuthenticationManager authenticationManager (AbstractLdapAuthenticationProvider authProvider ) {
55
+ return new ReactiveAuthenticationManagerAdapter (new ProviderManager (List .of (authProvider )));
56
+ }
57
+
58
+ @ Bean
59
+ public AbstractLdapAuthenticationProvider authenticationProvider (LdapAuthoritiesPopulator authoritiesExtractor ,
60
+ @ Autowired (required = false ) BindAuthenticator ba ,
61
+ AccessControlService acs ) {
55
62
var rbacEnabled = acs .isRbacEnabled ();
63
+
64
+ AbstractLdapAuthenticationProvider authProvider ;
65
+
66
+ if (!props .isActiveDirectory ()) {
67
+ authProvider = new LdapAuthenticationProvider (ba , authoritiesExtractor );
68
+ } else {
69
+ authProvider = new ActiveDirectoryLdapAuthenticationProvider (props .getActiveDirectoryDomain (),
70
+ props .getUrls ());
71
+ authProvider .setUseAuthenticationRequestCredentials (true );
72
+ ((ActiveDirectoryLdapAuthenticationProvider ) authProvider ).setAuthoritiesPopulator (authoritiesExtractor );
73
+ }
74
+
75
+ if (rbacEnabled ) {
76
+ authProvider .setUserDetailsContextMapper (new RbacUserDetailsMapper ());
77
+ }
78
+
79
+ return authProvider ;
80
+ }
81
+
82
+ @ Bean
83
+ @ ConditionalOnProperty (value = "oauth2.ldap.activeDirectory" , havingValue = "false" )
84
+ public BindAuthenticator ldapBindAuthentication (LdapContextSource ldapContextSource ) {
56
85
BindAuthenticator ba = new BindAuthenticator (ldapContextSource );
86
+
57
87
if (props .getBase () != null ) {
58
88
ba .setUserDnPatterns (new String [] {props .getBase ()});
59
89
}
90
+
60
91
if (props .getUserFilterSearchFilter () != null ) {
61
92
LdapUserSearch userSearch =
62
93
new FilterBasedLdapUserSearch (props .getUserFilterSearchBase (), props .getUserFilterSearchFilter (),
63
94
ldapContextSource );
64
95
ba .setUserSearch (userSearch );
65
96
}
66
97
67
- AbstractLdapAuthenticationProvider authenticationProvider ;
68
- if (!props .isActiveDirectory ()) {
69
- authenticationProvider = rbacEnabled
70
- ? new LdapAuthenticationProvider (ba , authoritiesExtractor )
71
- : new LdapAuthenticationProvider (ba );
72
- } else {
73
- authenticationProvider = new ActiveDirectoryLdapAuthenticationProvider (props .getActiveDirectoryDomain (),
74
- props .getUrls ()); // TODO Issue #3741
75
- authenticationProvider .setUseAuthenticationRequestCredentials (true );
76
- }
77
-
78
- if (rbacEnabled ) {
79
- authenticationProvider .setUserDetailsContextMapper (new UserDetailsMapper ());
80
- }
81
-
82
- AuthenticationManager am = new ProviderManager (List .of (authenticationProvider ));
83
-
84
- return new ReactiveAuthenticationManagerAdapter (am );
98
+ return ba ;
85
99
}
86
100
87
101
@ Bean
@@ -95,24 +109,27 @@ public LdapContextSource ldapContextSource() {
95
109
}
96
110
97
111
@ Bean
98
- public DefaultLdapAuthoritiesPopulator ldapAuthoritiesExtractor (ApplicationContext context ,
99
- BaseLdapPathContextSource contextSource ,
100
- AccessControlService acs ) {
101
- var rbacEnabled = acs != null && acs .isRbacEnabled ();
112
+ public LdapAuthoritiesPopulator authoritiesExtractor (ApplicationContext ctx ,
113
+ BaseLdapPathContextSource ldapCtx ,
114
+ AccessControlService acs ) {
115
+ if (!props .isActiveDirectory ()) {
116
+ if (!acs .isRbacEnabled ()) {
117
+ return new NullLdapAuthoritiesPopulator ();
118
+ }
102
119
103
- DefaultLdapAuthoritiesPopulator extractor ;
120
+ var extractor = new RbacLdapAuthoritiesExtractor ( ctx , ldapCtx , props . getGroupFilterSearchBase ()) ;
104
121
105
- if (rbacEnabled ) {
106
- extractor = new RbacLdapAuthoritiesExtractor (context , contextSource , props .getGroupFilterSearchBase ());
122
+ Optional .ofNullable (props .getGroupFilterSearchFilter ()).ifPresent (extractor ::setGroupSearchFilter );
123
+ extractor .setRolePrefix ("" );
124
+ extractor .setConvertToUpperCase (false );
125
+ extractor .setSearchSubtree (true );
126
+
127
+ return extractor ;
107
128
} else {
108
- extractor = new DefaultLdapAuthoritiesPopulator (contextSource , props .getGroupFilterSearchBase ());
129
+ return acs .isRbacEnabled ()
130
+ ? new RbacActiveDirectoryAuthoritiesExtractor (ctx )
131
+ : new DefaultActiveDirectoryAuthoritiesPopulator ();
109
132
}
110
-
111
- Optional .ofNullable (props .getGroupFilterSearchFilter ()).ifPresent (extractor ::setGroupSearchFilter );
112
- extractor .setRolePrefix ("" );
113
- extractor .setConvertToUpperCase (false );
114
- extractor .setSearchSubtree (true );
115
- return extractor ;
116
133
}
117
134
118
135
@ Bean
@@ -142,7 +159,7 @@ public SecurityWebFilterChain configureLdap(ServerHttpSecurity http) {
142
159
return builder .build ();
143
160
}
144
161
145
- private static class UserDetailsMapper extends LdapUserDetailsMapper {
162
+ private static class RbacUserDetailsMapper extends LdapUserDetailsMapper {
146
163
@ Override
147
164
public UserDetails mapUserFromContext (DirContextOperations ctx , String username ,
148
165
Collection <? extends GrantedAuthority > authorities ) {
0 commit comments