Skip to content

Commit 10f0b8f

Browse files
committed
Apply filter for all configs
1 parent 3cfd738 commit 10f0b8f

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ protected AbstractAuthSecurityConfig() {
66

77
}
88

9-
public static final String INDEX_HTML = "/static/index.html";
10-
119
protected static final String[] AUTH_WHITELIST = {
1210
/* STATIC */
1311
"/index.html",

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

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

33
import io.kafbat.ui.util.EmptyRedirectStrategy;
4+
import io.kafbat.ui.util.StaticFileWebFilter;
45
import java.net.URI;
56
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
78
import org.springframework.context.annotation.Bean;
89
import org.springframework.context.annotation.Configuration;
910
import org.springframework.http.HttpMethod;
1011
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
12+
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
1113
import org.springframework.security.config.web.server.ServerHttpSecurity;
1214
import org.springframework.security.web.server.SecurityWebFilterChain;
1315
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
@@ -20,7 +22,6 @@
2022
@Slf4j
2123
public class BasicAuthSecurityConfig extends AbstractAuthSecurityConfig {
2224

23-
public static final String LOGIN_URL = "/auth";
2425
public static final String LOGOUT_URL = "/auth?logout";
2526

2627
@Bean
@@ -33,19 +34,20 @@ public SecurityWebFilterChain configure(ServerHttpSecurity http) {
3334
final var logoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
3435
logoutSuccessHandler.setLogoutSuccessUrl(URI.create(LOGOUT_URL));
3536

36-
37-
return http.authorizeExchange(spec -> spec
37+
var builder = http.authorizeExchange(spec -> spec
3838
.pathMatchers(AUTH_WHITELIST)
3939
.permitAll()
4040
.anyExchange()
4141
.authenticated()
4242
)
43-
.formLogin(spec -> spec.loginPage(LOGIN_URL).authenticationSuccessHandler(authHandler))
4443
.logout(spec -> spec
4544
.logoutSuccessHandler(logoutSuccessHandler)
4645
.requiresLogout(ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/logout")))
47-
.csrf(ServerHttpSecurity.CsrfSpec::disable)
48-
.build();
46+
.csrf(ServerHttpSecurity.CsrfSpec::disable);
47+
48+
builder.addFilterAt(new StaticFileWebFilter(), SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
49+
50+
return builder.build();
4951
}
5052

5153
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import io.kafbat.ui.service.rbac.AccessControlService;
66
import io.kafbat.ui.service.rbac.extractor.RbacLdapAuthoritiesExtractor;
7+
import io.kafbat.ui.util.StaticFileWebFilter;
78
import java.util.Collection;
89
import java.util.List;
910
import java.util.Optional;
@@ -23,6 +24,7 @@
2324
import org.springframework.security.authentication.ReactiveAuthenticationManagerAdapter;
2425
import org.springframework.security.config.Customizer;
2526
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
27+
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
2628
import org.springframework.security.config.web.server.ServerHttpSecurity;
2729
import org.springframework.security.core.GrantedAuthority;
2830
import org.springframework.security.core.userdetails.UserDetails;
@@ -121,16 +123,19 @@ public SecurityWebFilterChain configureLdap(ServerHttpSecurity http) {
121123
log.info("Active Directory support for LDAP has been enabled.");
122124
}
123125

124-
return http.authorizeExchange(spec -> spec
126+
var builder = http.authorizeExchange(spec -> spec
125127
.pathMatchers(AUTH_WHITELIST)
126128
.permitAll()
127129
.anyExchange()
128130
.authenticated()
129131
)
130132
.formLogin(Customizer.withDefaults())
131133
.logout(Customizer.withDefaults())
132-
.csrf(ServerHttpSecurity.CsrfSpec::disable)
133-
.build();
134+
.csrf(ServerHttpSecurity.CsrfSpec::disable);
135+
136+
builder.addFilterAt(new StaticFileWebFilter(), SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
137+
138+
return builder.build();
134139
}
135140

136141
private static class UserDetailsMapper extends LdapUserDetailsMapper {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1818
import org.springframework.context.annotation.Bean;
1919
import org.springframework.context.annotation.Configuration;
20-
import org.springframework.core.io.ClassPathResource;
2120
import org.springframework.security.config.Customizer;
2221
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
2322
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
@@ -64,8 +63,7 @@ public SecurityWebFilterChain configure(ServerHttpSecurity http, OAuthLogoutSucc
6463
.csrf(ServerHttpSecurity.CsrfSpec::disable);
6564

6665

67-
builder.addFilterAt(new StaticFileWebFilter("/login", new ClassPathResource(INDEX_HTML)),
68-
SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
66+
builder.addFilterAt(new StaticFileWebFilter(), SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
6967

7068
return builder.build();
7169
}

api/src/main/java/io/kafbat/ui/util/StaticFileWebFilter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717

1818
public class StaticFileWebFilter implements WebFilter {
1919

20+
private static final String INDEX_HTML = "/static/index.html";
21+
2022
private final ServerWebExchangeMatcher matcher;
2123
private final String contents;
2224

25+
public StaticFileWebFilter() {
26+
this("/login", new ClassPathResource(INDEX_HTML));
27+
}
28+
2329
public StaticFileWebFilter(String path, ClassPathResource resource) {
2430
this.matcher = ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, path);
2531

0 commit comments

Comments
 (0)