Search code examples
spring-securityspring-cloud-gateway

How to Route Spring Cloud Gateway with Spring Authorization Server Downstream


what would be a recommended way of configuring Spring Cloud Gateway that routes Spring Authorization Server with hostname route without colliding paths.

for example:

  • /login gets routed to the /login of the auth server, without having the gateway match is own local /login due to gateway's spring security.

Solution

  • When accessing http://dummy.traefik.me/login does not go through the security filter chain, if so, I think it can be done like this

     private ServerWebExchangeMatcher getSecurityMatcher() {
            return exchange -> {
                URI uri = exchange.getRequest().getURI();
                if (uri.getHost().equals("dummy.traefik.me")) {
                    return ServerWebExchangeMatcher.MatchResult.notMatch();
                }
                return ServerWebExchangeMatcher.MatchResult.match();
            };
        }
    
        @Bean
        SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
            http
                    .securityMatcher(getSecurityMatcher())
                    .authorizeExchange(authorizeRequests -> authorizeRequests
                            .anyExchange()
                            .authenticated()
                    )
                    .formLogin(withDefaults())
                    .logout(withDefaults());
    
            return http.build();
        }