-
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.
Merge pull request #130 from yooonwodyd/weekly
refactor:[#84]- refact Security
- Loading branch information
Showing
2 changed files
with
53 additions
and
12 deletions.
There are no files selected for viewing
23 changes: 11 additions & 12 deletions
23
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,19 +1,18 @@ | ||
package com.helpmeCookies.global.config; | ||
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("http://1.618.s3-website.ap-northeast-2.amazonaws.com/") // 허용할 도메인 (모든 도메인 허용: "*") | ||
.allowedMethods("*") // 허용할 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") | ||
.allowCredentials(true); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
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 { | ||
|
||
// 요청 헤더 로깅 | ||
String origin = request.getHeader("Origin"); | ||
if (origin != null) { | ||
logger.info("Request Origin Header: {}", origin); | ||
} | ||
|
||
// 필터 체인을 통해 요청 처리 | ||
filterChain.doFilter(request, response); | ||
|
||
// 응답 헤더 로깅 | ||
logger.info("Response Headers:"); | ||
response.getHeaderNames().forEach(headerName -> { | ||
String headerValue = response.getHeader(headerName); | ||
logger.info("{}: {}", headerName, headerValue); | ||
}); | ||
} | ||
} |