-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
4 changed files
with
121 additions
and
28 deletions.
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
20 changes: 10 additions & 10 deletions
20
src/main/java/com/helpmeCookies/global/config/WebConfig.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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package com.helpmeCookies.global.config; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class WebConfig implements WebMvcConfigurer { | ||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOriginPatterns("*") // 허용할 도메인 (모든 도메인 허용: "*") | ||
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE") // 허용할 HTTP 메서드 | ||
.allowedHeaders("*") // 허용할 헤더 | ||
.allowCredentials(true); // 인증 정보 허용 여부 | ||
} | ||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOrigins("http://1.618.s3-website.ap-northeast-2.amazonaws.com", "http://localhost:3000") | ||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") | ||
.allowedHeaders("Authorization", "Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers","cross-control-allow-origin") | ||
.allowCredentials(true); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/helpmeCookies/global/security/CorsLoggingFilter.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,46 @@ | ||
package com.helpmeCookies.global.security; | ||
|
||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@Component | ||
public class CorsLoggingFilter extends OncePerRequestFilter { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(CorsLoggingFilter.class); | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
// 요청 헤더 로깅, cors | ||
String origin = request.getHeader("Origin"); | ||
if (origin != null) { | ||
logger.info("Request Origin Header: {}", origin); | ||
} | ||
|
||
String requestMethod = request.getHeader("cross-control-allow-origin"); | ||
if (requestMethod != null) { | ||
logger.info("cross-control-allow-origin: {}", requestMethod); | ||
} | ||
|
||
// 필터 체인을 통해 요청 처리 | ||
filterChain.doFilter(request, response); | ||
|
||
// cors 관련 응답 헤더 로깅 | ||
String responseHeader = response.getHeader("Access-Control-Allow-Origin"); | ||
if (responseHeader != null) { | ||
logger.info("Response Access-Control-Allow-Origin Header: {}", responseHeader); | ||
} | ||
} | ||
} |
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