GithubHelp home page GithubHelp logo

scg-error's Introduction

Spring Cloud Gateway Error Demo

A simple demo of Spring Cloud Gateway routing to a normal Spring WebMVC application. If the WebMVC application applies the default header cache control customizer, the 'Cache-Control' header is not properly interpreted by Spring Cloud Gateway's LocalResponseCache filter.

The WebMVC application is configured with:

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .headers(headers-> headers.cacheControl(Customizer.withDefaults()))
                .build();
    }

A controller method that doesn't want to be cached can then just return a normal ResponseEntity:

    @GetMapping(value = "/api/not-cached", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResponseDto> notCached() {
        return ResponseEntity
                .ok(new ResponseDto(UUID.randomUUID()));
    }

And the header should be:

cache-control: no-cache, no-store, max-age=0, must-revalidate

This is not parsed properly by Spring Cloud Gateway's LocalResponseCache filter. The response will be cached according to the default settings for its route.

Testing

Both the app and gateway modules can be started using gradle bootrun. The application is on port 8081. The gateway is on 8080.

You can curl to the application directly and verify its headers:

curl -v http://localhost:8081/api/not-cached

You can then curl to it through the gateway and see that it is being cached:

curl -v http://localhost:8080/app/api/not-cached

You can also run the same test again, but this time with cached as the endpoint. In this case, Spring Cloud Gateway will not use the max-age=1800 specified by the application.

scg-error's People

Contributors

danielshiplett avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.