Dev4792
Dev4792

Reputation: 11

Cors with Spring Securtiy

Where do I configure CORS with form login in Spring Security? I would like to do it using CorsConfigurationSource bean, but I keep seeing conflicting sources and none of them work.

Upvotes: 0

Views: 52

Answers (1)

nsnan9
nsnan9

Reputation: 161

After some back and forth with a colleague, I found out the easiest way to do this is using the permit default values. Of course this would depend on what you want to allow cors for. The simplest way to enable is the following which should be in your config class that extends WebSecurityConfigurerAdapter.

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        final CorsConfiguration configuration = new CorsConfiguration();
        configuration.applyPermitDefaultValues();
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

Quick tip, be a little more specific next time so other users may relate to your issue and use the same correct answer. Let me know if you need a cors policy with custom properties. Would be happy to explain.

Upvotes: 2

Related Questions