11package wonjun .stiky .global .config ;
22
3+ import java .util .Arrays ;
4+ import java .util .List ;
35import lombok .RequiredArgsConstructor ;
6+ import org .springframework .beans .factory .annotation .Value ;
47import org .springframework .context .annotation .Bean ;
58import org .springframework .context .annotation .Configuration ;
69import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1114import org .springframework .security .crypto .password .PasswordEncoder ;
1215import org .springframework .security .web .SecurityFilterChain ;
1316import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
17+ import org .springframework .web .cors .CorsConfiguration ;
18+ import org .springframework .web .cors .CorsConfigurationSource ;
19+ import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
1420import wonjun .stiky .auth .config .CustomOAuth2UserService ;
1521import wonjun .stiky .auth .config .CustomUserDetailsService ;
1622import wonjun .stiky .auth .config .JwtAuthenticationFilter ;
@@ -27,9 +33,13 @@ public class SecurityConfig {
2733 private final OAuth2SuccessHandler oAuth2SuccessHandler ;
2834 private final JwtTokenProvider jwtTokenProvider ;
2935
36+ @ Value ("${cors.allowed-origins}" )
37+ private List <String > allowedOrigins ;
38+
3039 @ Bean
3140 public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
3241 http
42+ .cors (cors -> cors .configurationSource (corsConfigurationSource ()))
3343 .csrf (AbstractHttpConfigurer ::disable )
3444 .formLogin (AbstractHttpConfigurer ::disable )
3545 .httpBasic (AbstractHttpConfigurer ::disable )
@@ -58,4 +68,20 @@ public PasswordEncoder passwordEncoder() {
5868 return new BCryptPasswordEncoder ();
5969 }
6070
71+ @ Bean
72+ public CorsConfigurationSource corsConfigurationSource () {
73+ CorsConfiguration configuration = new CorsConfiguration ();
74+
75+ configuration .setAllowedOrigins (allowedOrigins );
76+ configuration .setAllowedMethods (Arrays .asList ("GET" , "POST" , "PUT" , "DELETE" , "PATCH" , "OPTIONS" ));
77+ configuration .setAllowedHeaders (List .of ("*" ));
78+ configuration .setAllowCredentials (true );
79+ configuration .setExposedHeaders (List .of ("Authorization" ));
80+
81+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
82+ source .registerCorsConfiguration ("/**" , configuration );
83+
84+ return source ;
85+ }
86+
6187}
0 commit comments