Cors Error when using CorsFilter and spring security

We Are Going To Discuss About Cors Error when using CorsFilter and spring security. So lets Start this Java Article.

Cors Error when using CorsFilter and spring security

  1. Cors Error when using CorsFilter and spring security

    This line has to be changed. You should list all servers, that should have access to your application.
    E.g. you use angular, so the development server for the frontend is http://localhost:4200. Your server in production is https://you.server.domain.com

  2. Cors Error when using CorsFilter and spring security

    This line has to be changed. You should list all servers, that should have access to your application.
    E.g. you use angular, so the development server for the frontend is http://localhost:4200. Your server in production is https://you.server.domain.com

Solution 1

Use config.setAllowedOriginPatterns("*") instead of config.setAllowedOrigins(Collections.singletonList("*"));

Original Author Of This Content

Solution 2

config.setAllowedOrigins(Collections.singletonList("*"));

This line has to be changed. You should list all servers, that should have access to your application.

E.g. you use angular, so the development server for the frontend is http://localhost:4200. Your server in production is https://you.server.domain.com

Then your config List should look like this

config.setAllowedOrigins(List.of("http://localhost:4200","https://you.server.domain.com"));

Original Author Of This Content

Solution 3

if project use port 4000, yml config

allowedOrigins:
  - "http://localhost:4000"

http://localhost:4000 replace *
spring document:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#cors-configuration

Original Author Of This Content

Solution 4

This cannot set to true when you want to have the wildcard “*” in your origin.

config.setAllowCredentials(true);

So just remove it

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment