spring-cloud-gateway

Spring Cloud Gateway SaveSession Filter appears to have vanished


I am investigating the use of Spring Cloud Gateway to implement the BFF pattern, with the gateway between an Angular SPA (Public Client) and a set of backend Spring Boot REST APIs, with Keycloak as the Authorization Server and IdP. As per the BFF pattern, the Gateway is handling the OAuth2 tokens and maintaining a session with the Angular frontend.

Most of the configuration samples I have seen (e.g. with Spring Cloud 2023.0.1) contain configuration similar to the following:

spring.cloud.gateway.server.webmvc:
[...]
filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
- TokenRelay=
- SaveSession
- StripPrefix=1

However, with the latest versions (Spring Cloud 2025.0.0) my application fails to start up with the following error:

Caused by: java.lang.IllegalArgumentException: Unable to find operation interface org.springframework.web.servlet.function.HandlerFilterFunction for saveSession with args {}
    at org.springframework.cloud.gateway.server.mvc.config.RouterFunctionHolderFactory.translate(RouterFunctionHolderFactory.java:315) ~[spring-cloud-gateway-server-mvc-4.3.0.jar:4.3.0]
    at org.springframework.cloud.gateway.server.mvc.config.RouterFunctionHolderFactory.lambda$getRouterFunction$5(RouterFunctionHolderFactory.java:287) ~[spring-cloud-gateway-server-mvc-4.3.0.jar:4.3.0]

If I remove the SaveSession, everything appears to work nicely. My pom.xml contains:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-server-webmvc</artifactId>
        </dependency>

If I peek inside spring-cloud-gateway-server-mvc-4.3.0.jar I cannot find anything related to SaveSession, and I noticed that it's also been removed from the documentation. https://docs.spring.io/spring-cloud-gateway/reference/spring-cloud-gateway-server-webmvc/gateway-handler-filter-functions.html .

Can anyone shed any light on why this has been removed, and what the alternatives are? I understood that it was necessary when using Spring Session or some other session cache, which I may explore in future.


Solution

  • The SaveSession filter is for spring-cloud-starter-gateway-server (the WebFlux version of Spring Cloud Gateway), not for spring-cloud-starter-gateway-server-webmvc (the servlet version). Remove it from your conf.

    By the way, if you've not already, you might read this Baeldung article I wrote. It provides a working solution for what you are implementing.