Skip to content

Commit

Permalink
Merge pull request #30 from logostory/f#3-cors-fix
Browse files Browse the repository at this point in the history
F#3 cors fix
  • Loading branch information
seongbin9786 authored Dec 25, 2018
2 parents 891bfc4 + 5dc255d commit 2ad4ec7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.logostory.orderbook.backend.config;

import io.logostory.orderbook.backend.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
Expand All @@ -12,6 +13,11 @@
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import io.logostory.orderbook.backend.service.AccountService;

@Configuration
@EnableWebSecurity
Expand Down Expand Up @@ -42,4 +48,19 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
public void configure(WebSecurity web) throws Exception {
web.ignoring().mvcMatchers("/v2/api-docs", "/swagger-resources/**", "/swagger-ui.html**", "/webjars/**");
}

@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);

FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package io.logostory.orderbook.backend.config;

import org.modelmapper.ModelMapper;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

Expand All @@ -21,15 +17,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedMethods("*");
}


@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
Expand Down

2 comments on commit 2ad4ec7

@emarzate
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually working?

@seongbin9786
Copy link
Member Author

@seongbin9786 seongbin9786 commented on 2ad4ec7 Jan 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit late response for you @emarzate, This works fine. 👍 While you might find other ways to configure cors options working, I ended up with this one :) Rather, it's source got quite a lot of stars from other developers, so it would be worth giving a look at the original code

Explanation
The code overrides Spring's default CorsFilter bean using FilterRegistrationBean.

Please sign in to comment.