close

How to configure CORS in a Spring Boot + Spring Security application?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to configure CORS in a Spring Boot + Spring Security application? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to configure CORS in a Spring Boot + Spring Security application ?

  1. How to configure CORS in a Spring Boot + Spring Security application ?

    to configure CORS in a Spring Boot + Spring Security application Kotlin solution
    ... http.cors().configurationSource { CorsConfiguration().applyPermitDefaultValues() } ...

  2. How to configure CORS in a Spring Boot + Spring Security application ?

    to configure CORS in a Spring Boot + Spring Security application Kotlin solution
    ... http.cors().configurationSource { CorsConfiguration().applyPermitDefaultValues() } ...

Method 1

If you use JDK 8+, there is a one line lambda solution:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
}

Method 2

Kotlin solution

...
http.cors().configurationSource {
  CorsConfiguration().applyPermitDefaultValues()
}
...

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment