Created
January 31, 2018 09:31
-
-
Save RICH0423/d027d3e29d03565c87cb8e243c41fa86 to your computer and use it in GitHub Desktop.
CORS filter in Spring Security, ref: https://docs.spring.io/spring-security/site/docs/current/reference/html/cors.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@EnableWebSecurity | |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http | |
// by default uses a Bean by the name of corsConfigurationSource | |
.cors().and() | |
... | |
} | |
@Bean | |
CorsConfigurationSource corsConfigurationSource() { | |
CorsConfiguration configuration = new CorsConfiguration(); | |
configuration.setAllowedOrigins(ImmutableList.of("*")); | |
configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH")); | |
configuration.setAllowCredentials(true); | |
configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type")); | |
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | |
source.registerCorsConfiguration("/**", configuration); | |
return source; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment