Code Monkey home page Code Monkey logo

spring-cors's Introduction

*If you would like to support these tutorials, you can contribute to my Patreon account

Spring CORS

Configure CORS in a Spring Boot REST application to make it accessible from a frontend application such as React or Angular.

This is the resource server of this exercise.

Client side is available at https://github.com/czetsuya/spring-cors-nextjs.

What is CORS and why do we need CORS?

CORS or Cross-Origin Resource Sharing is a browser security feature that restricts accessing the resource of server b from an HTTP request initiated by a script coming from server a.

Normally, we have our API server running on the backend such as api.czetsuyatech.com. This is where actual data fetch and manipulation happens.

On the other side, we have our frontend client which is normally powered by either React, Angular, or Vue. For example, czetsuya.com.

How CORS Work?

CORS flow

This is a sequence diagram when calling GET /books endpoint.

  1. Perform a CORS preflight request
  2. If CORS headers from the server match the client proceed, otherwise throw a CORS error
  3. Return response with data.a

Different Ways of Configuring CORS

CrossOrigin

CrossOrigin is a Spring annotation that we can use on any Java type or method to configure CORS. This is good if you just have one or two endpoints.

I normally use it to override the global configuration when testing a particular endpoint.

Globally via Spring WebMvcConfigurer

In practice, we would normally use this approach as often we have a defined set of clients who will access the resource server in the same way. And having the configuration in the same place makes maintenance easier.

Here's an example configuration:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
        .allowedMethods("*")
        .allowedHeaders("*")
        .allowedOrigins("http://192.168.1.9:8080", "http://192.168.1.9:3000")
        .allowCredentials(false)
        .maxAge(-1);
  }
}
  • "*" means it will accept any allowable value for that field. For example, allowedMethods will accept GET, POST, PUT, DELETE, OPTION, etc. @see org.springframework.http.HttpMethod.
  • allowedOrigins, tells Spring to only accept Ajax requests from these URL patterns.
  • maxAge - indicates how long to cache the result of a CORS preflight request.

Deployment and Testing

To see how CORS work clone the following GIT repositories:

Requirements

You must have the following components installed on your local machine:

In the root of the spring-cors project execute: mvn spring-boot:run

In the root of spring-cors-nextjs project execute: yarn install && yarn dev

Fire up your browser, and open http://:3000.

References

spring-cors's People

Contributors

czetsuya avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.