|
3 | 3 | import static org.springframework.http.HttpMethod.*;
|
4 | 4 |
|
5 | 5 | import java.util.HashMap;
|
| 6 | +import java.util.List; |
6 | 7 | import java.util.Map;
|
7 | 8 |
|
| 9 | +import org.springframework.beans.factory.annotation.Value; |
8 | 10 | import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
|
9 | 11 | import org.springframework.context.annotation.Bean;
|
10 | 12 | import org.springframework.context.annotation.Configuration;
|
|
18 | 20 | import org.springframework.security.crypto.password.PasswordEncoder;
|
19 | 21 | import org.springframework.security.web.SecurityFilterChain;
|
20 | 22 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
| 23 | +import org.springframework.web.cors.CorsConfiguration; |
| 24 | +import org.springframework.web.cors.CorsConfigurationSource; |
21 | 25 |
|
22 | 26 | import com.ajou.hertz.common.auth.JwtAccessDeniedHandler;
|
23 | 27 | import com.ajou.hertz.common.auth.JwtAuthenticationEntryPoint;
|
@@ -65,12 +69,32 @@ public class SecurityConfig {
|
65 | 69 | }
|
66 | 70 |
|
67 | 71 | @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 { |
69 | 77 | return http
|
70 | 78 | .csrf(CsrfConfigurer::disable)
|
71 | 79 | .httpBasic(HttpBasicConfigurer::disable)
|
72 | 80 | .formLogin(AbstractHttpConfigurer::disable)
|
73 | 81 | .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 | + }) |
74 | 98 | .authorizeHttpRequests(auth -> {
|
75 | 99 | auth.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll();
|
76 | 100 | auth.requestMatchers(AUTH_WHITE_PATHS).permitAll();
|
|
0 commit comments