Skip to content

Spring Cloud Gateway Route Handling With WebFlux Security #2253

Description

@topdonn

Is it possible to use the Spring Cloud Gateway Route Handling in association with the WebFlux Security?

I am attempting to utilize the Spring Cloud Gateway to redirect my custom login page to http://localhost:8080/

My application uses ThymeLeaf to render the login page. I have my WebFlux security set us as follows:

@Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http)  {
        AuthenticationWebFilter passThroughFilter = new AuthenticationWebFilter(passThrough());
        return http
                .addFilterAfter(passThroughFilter, SecurityWebFiltersOrder.REACTOR_CONTEXT)
                .authorizeExchange(authorizeExchangeSpec ->
                        authorizeExchangeSpec.pathMatchers("/login", "/logout").permitAll()
                                .anyExchange().authenticated())
                .httpBasic().disable()
                .formLogin(formLogin -> formLogin.loginPage("/login")
                      .authenticationSuccessHandler(new WebFilterChainServerAuthenticationSuccessHandler()))
                .csrf(ServerHttpSecurity.CsrfSpec::disable)
                .build();
    }

The login page loads up fine but when I click on the submit on the login page it seems that the Gateway Routing never comes into effect. This is despite providing Routing predicates both in the application.yml and in a RouteLocator file as shown below.

@Configuration
public class SpringCloudGatewayConfig {
    @Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(p -> p
                        .path("/login")
                        .and()
                        .method(HttpMethod.POST)
                        .filters(f -> f.addRequestHeader("iv-user", "whatever"))
                        .uri("http://localhost:8080/"))
                .build();
    }
}

The WebSecurityConfig seems to have predecence and the SpringCloudGateway never seems to kick in.

Just for reference we had the Zuul Proxy before this and this works with the Zuul Proxy.

Is there a way around this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions