Skip to content

Commit bba68eb

Browse files
committed
Fix LDAP, refactor
1 parent 00af933 commit bba68eb

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

api/src/main/java/io/kafbat/ui/config/auth/AbstractAuthSecurityConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package io.kafbat.ui.config.auth;
22

3+
import io.kafbat.ui.util.EmptyRedirectStrategy;
4+
import java.net.URI;
5+
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
6+
import org.springframework.security.web.server.authentication.logout.RedirectServerLogoutSuccessHandler;
7+
38
abstract class AbstractAuthSecurityConfig {
49

510
protected AbstractAuthSecurityConfig() {
611

712
}
813

14+
protected static final String LOGIN_URL = "/login";
15+
protected static final String LOGOUT_URL = "/auth?logout";
16+
917
protected static final String[] AUTH_WHITELIST = {
1018
/* STATIC */
1119
"/index.html",
@@ -30,4 +38,16 @@ protected AbstractAuthSecurityConfig() {
3038
"/api/authorization"
3139
};
3240

41+
protected RedirectServerAuthenticationSuccessHandler emptyRedirectSuccessHandler() {
42+
final var authHandler = new RedirectServerAuthenticationSuccessHandler();
43+
authHandler.setRedirectStrategy(new EmptyRedirectStrategy());
44+
return authHandler;
45+
}
46+
47+
protected RedirectServerLogoutSuccessHandler redirectLogoutSuccessHandler() {
48+
final var logoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
49+
logoutSuccessHandler.setLogoutSuccessUrl(URI.create(LOGOUT_URL));
50+
return logoutSuccessHandler;
51+
}
52+
3353
}

api/src/main/java/io/kafbat/ui/config/auth/BasicAuthSecurityConfig.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,10 @@
2222
@Slf4j
2323
public class BasicAuthSecurityConfig extends AbstractAuthSecurityConfig {
2424

25-
private static final String LOGIN_URL = "/login";
26-
private static final String LOGOUT_URL = "/auth?logout";
27-
2825
@Bean
2926
public SecurityWebFilterChain configure(ServerHttpSecurity http) {
3027
log.info("Configuring LOGIN_FORM authentication.");
3128

32-
final var authHandler = new RedirectServerAuthenticationSuccessHandler();
33-
authHandler.setRedirectStrategy(new EmptyRedirectStrategy());
34-
35-
final var logoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
36-
logoutSuccessHandler.setLogoutSuccessUrl(URI.create(LOGOUT_URL));
37-
3829
var builder = http.authorizeExchange(spec -> spec
3930
.pathMatchers(AUTH_WHITELIST)
4031
.permitAll()
@@ -43,10 +34,10 @@ public SecurityWebFilterChain configure(ServerHttpSecurity http) {
4334
)
4435
.formLogin(form -> form
4536
.loginPage(LOGIN_URL)
46-
.authenticationSuccessHandler(authHandler)
37+
.authenticationSuccessHandler(emptyRedirectSuccessHandler())
4738
)
4839
.logout(spec -> spec
49-
.logoutSuccessHandler(logoutSuccessHandler)
40+
.logoutSuccessHandler(redirectLogoutSuccessHandler())
5041
.requiresLogout(ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/logout")))
5142
.csrf(ServerHttpSecurity.CsrfSpec::disable);
5243

api/src/main/java/io/kafbat/ui/config/auth/LdapSecurityConfig.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.kafbat.ui.config.auth;
22

3-
import static io.kafbat.ui.config.auth.AbstractAuthSecurityConfig.AUTH_WHITELIST;
4-
53
import io.kafbat.ui.service.rbac.AccessControlService;
64
import io.kafbat.ui.service.rbac.extractor.RbacLdapAuthoritiesExtractor;
75
import io.kafbat.ui.util.StaticFileWebFilter;
@@ -15,14 +13,14 @@
1513
import org.springframework.context.ApplicationContext;
1614
import org.springframework.context.annotation.Bean;
1715
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.http.HttpMethod;
1817
import org.springframework.ldap.core.DirContextOperations;
1918
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
2019
import org.springframework.ldap.core.support.LdapContextSource;
2120
import org.springframework.security.authentication.AuthenticationManager;
2221
import org.springframework.security.authentication.ProviderManager;
2322
import org.springframework.security.authentication.ReactiveAuthenticationManager;
2423
import org.springframework.security.authentication.ReactiveAuthenticationManagerAdapter;
25-
import org.springframework.security.config.Customizer;
2624
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
2725
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
2826
import org.springframework.security.config.web.server.ServerHttpSecurity;
@@ -38,14 +36,15 @@
3836
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;
3937
import org.springframework.security.ldap.userdetails.LdapUserDetailsMapper;
4038
import org.springframework.security.web.server.SecurityWebFilterChain;
39+
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
4140

4241
@Configuration
4342
@EnableWebFluxSecurity
4443
@ConditionalOnProperty(value = "auth.type", havingValue = "LDAP")
4544
@EnableConfigurationProperties(LdapProperties.class)
4645
@RequiredArgsConstructor
4746
@Slf4j
48-
public class LdapSecurityConfig {
47+
public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
4948

5049
private final LdapProperties props;
5150

@@ -129,8 +128,13 @@ public SecurityWebFilterChain configureLdap(ServerHttpSecurity http) {
129128
.anyExchange()
130129
.authenticated()
131130
)
132-
.formLogin(Customizer.withDefaults())
133-
.logout(Customizer.withDefaults())
131+
.formLogin(form -> form
132+
.loginPage(LOGIN_URL)
133+
.authenticationSuccessHandler(emptyRedirectSuccessHandler())
134+
)
135+
.logout(spec -> spec
136+
.logoutSuccessHandler(redirectLogoutSuccessHandler())
137+
.requiresLogout(ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/logout")))
134138
.csrf(ServerHttpSecurity.CsrfSpec::disable);
135139

136140
builder.addFilterAt(new StaticFileWebFilter(), SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);

0 commit comments

Comments
 (0)