-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
018c8e9
commit 7b55c68
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/example/solutionchallenge/common/config/CorsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.solutionchallenge.common.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
import org.springframework.web.filter.CorsFilter; | ||
|
||
@Configuration | ||
public class CorsConfig { | ||
|
||
@Bean | ||
public CorsFilter corsFilter() { | ||
|
||
CorsConfiguration configuration = new CorsConfiguration(); | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
|
||
configuration.setAllowCredentials(true); // 서버 응답 시 json 자바스크립트에서 처리 허용 | ||
configuration.addAllowedOrigin("http://localhost:3000"); | ||
configuration.addAllowedHeader("*"); // 모든 header 응답 허용 | ||
configuration.addAllowedMethod("*"); // 모든 post, get, put, delete, patch 요청 허용 | ||
|
||
source.registerCorsConfiguration("/**", configuration); | ||
return new CorsFilter(source); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/example/solutionchallenge/common/config/HttpSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.example.solutionchallenge.common.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
@EnableWebSecurity | ||
public class HttpSecurityConfig { | ||
|
||
private final CorsConfig corsConfig; | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
http | ||
.csrf().disable() //token을 쓰는 방식이라 필요 없음 | ||
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
.and() | ||
.addFilter(corsConfig.corsFilter()) | ||
.formLogin().disable() //직접 만든 로그인 폼 쓸거라 필요없음 | ||
.httpBasic().disable() | ||
.authorizeRequests() | ||
.requestMatchers("/oauth2/**").permitAll() | ||
.anyRequest().authenticated() | ||
.and() | ||
.cors().configurationSource(corsConfigurationSource()); | ||
return http.build(); | ||
} | ||
|
||
@Bean | ||
public CorsConfigurationSource corsConfigurationSource() { | ||
CorsConfiguration configuration = new CorsConfiguration(); | ||
// 여기에 CORS 설정을 추가하세요. 예: | ||
configuration.addAllowedOrigin("*"); | ||
configuration.addAllowedMethod("*"); | ||
configuration.addAllowedHeader("*"); | ||
configuration.setAllowCredentials(true); | ||
|
||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
source.registerCorsConfiguration("/**", configuration); | ||
return source; | ||
} | ||
} |