Skip to content

Commit

Permalink
πŸ› CORS 였λ₯˜ μˆ˜μ •
Browse files Browse the repository at this point in the history
- 배포 μ‹œ λ°œμƒν•˜λŠ” CORS 였λ₯˜λ₯Ό μˆ˜μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
  • Loading branch information
Ganghee-Lee-0522 committed Jan 12, 2024
1 parent ff40441 commit 8bf8a2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 47 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
Expand All @@ -15,14 +16,14 @@
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Collections;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final CorsConfig corsConfig;
private final JwtFilter jwtFilter;
private final OAuth2SuccessHandler successHandler;
private final CustomOAuth2UserService customOAuth2UserService;
Expand All @@ -46,24 +47,27 @@ public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

CorsConfigurationSource corsConfigurationSource() {
return request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedOriginPatterns(Collections.singletonList("http://localhost:3000"));
config.setAllowedOriginPatterns(Collections.singletonList("https://kidari.site"));
config.setAllowedOriginPatterns(Collections.singletonList("https://kidari.site:3000"));
config.setAllowCredentials(true);
return config;
};
@Bean
public CorsConfigurationSource corsConfigurationSource() {

CorsConfiguration config = new CorsConfiguration();
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.addAllowedOriginPattern("http://localhost:3000");
config.addAllowedOriginPattern("https://kidari.site");
config.addAllowedOriginPattern("https://kidari.site:3000");
config.setAllowCredentials(true);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}

@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.httpBasic(HttpBasicConfigurer::disable)
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource()))
.cors(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.authorizeHttpRequests((authorizeRequests) -> {
Expand Down

0 comments on commit 8bf8a2f

Please sign in to comment.