Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS 해결 #128

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/dbdr/global/configuration/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dbdr.global.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*") // 모든 Origin 허용
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS") // 허용할 HTTP 메서드
.allowedHeaders("Authorization", "Content-Type") // 허용할 헤더
.allowCredentials(true) // 인증 정보 포함 허용
.exposedHeaders("Authorization") // 노출할 헤더
.maxAge(3600); // 캐시 유지 시간 (초)
}
}
21 changes: 1 addition & 20 deletions src/main/java/dbdr/security/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
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.List;

@RequiredArgsConstructor
@Configuration
Expand All @@ -48,31 +43,17 @@ public BaseAuthenticationProvider baseAuthenticationProvider() {
return new BaseAuthenticationProvider(baseUserDetailsService, passwordEncoder());
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();

configuration.setAllowedOriginPatterns(List.of("*"));
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(List.of("*"));

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

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.cors(configurer -> configurer.configurationSource(corsConfigurationSource())) // CORS 설정 추가
.httpBasic(AbstractHttpConfigurer::disable)
.formLogin(AbstractAuthenticationFilterConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(new ExceptionHandlingFilter(), UsernamePasswordAuthenticationFilter.class)
.authenticationProvider(baseAuthenticationProvider())
.authorizeHttpRequests(authorize -> {
authorize
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() // 모든 OPTIONS 요청 허용
.requestMatchers("/line").permitAll()
.requestMatchers("/health").permitAll()
.requestMatchers("/v1/ocr/**").permitAll()
Expand Down