Skip to content

Commit 466ecc1

Browse files
committed
chore: CORS 설정 추가
1 parent 7370502 commit 466ecc1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main/java/com/ajou/hertz/common/config/SecurityConfig.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import static org.springframework.http.HttpMethod.*;
44

55
import java.util.HashMap;
6+
import java.util.List;
67
import java.util.Map;
78

9+
import org.springframework.beans.factory.annotation.Value;
810
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
911
import org.springframework.context.annotation.Bean;
1012
import org.springframework.context.annotation.Configuration;
@@ -18,6 +20,8 @@
1820
import org.springframework.security.crypto.password.PasswordEncoder;
1921
import org.springframework.security.web.SecurityFilterChain;
2022
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
23+
import org.springframework.web.cors.CorsConfiguration;
24+
import org.springframework.web.cors.CorsConfigurationSource;
2125

2226
import com.ajou.hertz.common.auth.JwtAccessDeniedHandler;
2327
import com.ajou.hertz.common.auth.JwtAuthenticationEntryPoint;
@@ -65,12 +69,32 @@ public class SecurityConfig {
6569
}
6670

6771
@Bean
68-
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
72+
public SecurityFilterChain securityFilterChain(
73+
HttpSecurity http,
74+
@Value("${hertz.web.url}") String hertzWebUrl,
75+
@Value("${hertz.server.url}") String hertzServerUrl
76+
) throws Exception {
6977
return http
7078
.csrf(CsrfConfigurer::disable)
7179
.httpBasic(HttpBasicConfigurer::disable)
7280
.formLogin(AbstractHttpConfigurer::disable)
7381
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
82+
.cors(corsconfigurer -> {
83+
CorsConfigurationSource corsConfigSrc = request -> {
84+
CorsConfiguration corsConfig = new CorsConfiguration();
85+
corsConfig.setAllowCredentials(true);
86+
corsConfig.setAllowedOrigins(List.of(
87+
"http://localhost", "http://localhost:8080",
88+
hertzWebUrl, hertzServerUrl
89+
));
90+
corsConfig.setAllowedMethods(
91+
List.of(GET.name(), POST.name(), PUT.name(), DELETE.name(), PATCH.name(), OPTIONS.name()));
92+
corsConfig.setAllowedHeaders(List.of("*"));
93+
corsConfig.setExposedHeaders(List.of("*"));
94+
return corsConfig;
95+
};
96+
corsconfigurer.configurationSource(corsConfigSrc);
97+
})
7498
.authorizeHttpRequests(auth -> {
7599
auth.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll();
76100
auth.requestMatchers(AUTH_WHITE_PATHS).permitAll();

src/main/resources/application.properties

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
hertz.app-version=0.0.1
22
hertz.user-default-profile-image-url=${USER_DEFAULT_PROFILE_IMAGE}
33

4+
hertz.web.url=${HERTZ_WEB_URL}
5+
hertz.server.url=${HERTZ_SERVER_URL}
6+
47
jwt.secret-key=${JWT_SECRET_KEY}
58

69
springdoc.swagger-ui.operations-sorter=method

0 commit comments

Comments
 (0)