Skip to content

Commit

Permalink
Fix: cors
Browse files Browse the repository at this point in the history
  • Loading branch information
emost22 committed Jul 16, 2024
1 parent b51beaa commit d26b782
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 46 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/sirius/spurt/common/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
@EnableWebSecurity
Expand All @@ -33,6 +36,7 @@ public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.cors((cors) -> cors.configurationSource(corsConfigurationSource()))
.formLogin(AbstractHttpConfigurer::disable)
.logout(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable)
Expand Down Expand Up @@ -83,4 +87,19 @@ public OAuth2AuthenticationSuccessHandler oAuth2AuthenticationSuccessHandler() {
public OAuth2AuthenticationFailHandler oAuth2AuthenticationFailHandler() {
return new OAuth2AuthenticationFailHandler(objectMapper);
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOriginPattern("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addExposedHeader("Authorization");
corsConfiguration.addExposedHeader("RefreshToken");
corsConfiguration.setAllowCredentials(true);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}
}
46 changes: 0 additions & 46 deletions src/main/java/com/sirius/spurt/common/filter/CorsFilter.java

This file was deleted.

0 comments on commit d26b782

Please sign in to comment.