From cc1015ffdbf20f8517206d3c624993c1f51f659f Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 13:37:09 +0900 Subject: [PATCH 1/4] KL-163/feat: set timezone to Asia/Seoul --- src/main/resources/application-h2.yaml | 2 ++ src/main/resources/application-mysql.yaml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application-h2.yaml b/src/main/resources/application-h2.yaml index 6a45b206..35ce5ba8 100644 --- a/src/main/resources/application-h2.yaml +++ b/src/main/resources/application-h2.yaml @@ -12,6 +12,8 @@ spring: hibernate: show_sql: true format_sql: true + jdbc: + time_zone: ${TZ} datasource: driver-class-name: org.h2.Driver url: jdbc:h2:mem:klkldb;MODE=MySQL diff --git a/src/main/resources/application-mysql.yaml b/src/main/resources/application-mysql.yaml index 8a0e0ef7..8fada97c 100644 --- a/src/main/resources/application-mysql.yaml +++ b/src/main/resources/application-mysql.yaml @@ -10,11 +10,11 @@ spring: jpa: hibernate: ddl-auto: update + defer-datasource-initialization: true properties: hibernate: show_sql: true format_sql: true - defer-datasource-initialization: true database-platform: org.hibernate.dialect.MySQLDialect sql: init: From 9298a2d827a59f85f22c916287a31ea1dfa451fb Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 14:04:43 +0900 Subject: [PATCH 2/4] KL-163/fix: add optional token auth endpoint --- .../config/security/SecurityEndpoint.java | 18 +++++++- .../security/TokenAuthenticationFilter.java | 44 +++++++++++++++---- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java index 3bddcc5d..06ff08a3 100644 --- a/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java +++ b/src/main/java/taco/klkl/global/config/security/SecurityEndpoint.java @@ -1,11 +1,15 @@ package taco.klkl.global.config.security; +import java.util.Arrays; + import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.security.web.util.matcher.RequestMatcher; +import jakarta.servlet.http.HttpServletRequest; import lombok.Getter; import lombok.RequiredArgsConstructor; + @Getter @RequiredArgsConstructor public enum SecurityEndpoint { @@ -28,7 +32,6 @@ public enum SecurityEndpoint { new AntPathRequestMatcher("/v1/login/**"), new AntPathRequestMatcher("/v1/oauth2/**"), new AntPathRequestMatcher("/v1/members/**"), - new AntPathRequestMatcher("/v1/products/**"), new AntPathRequestMatcher("/v1/regions/**"), new AntPathRequestMatcher("/v1/countries/**"), new AntPathRequestMatcher("/v1/cities/**"), @@ -43,7 +46,20 @@ public enum SecurityEndpoint { new AntPathRequestMatcher("/v1/notifications/**"), new AntPathRequestMatcher("/v1/logout/**"), }), + BOTH(new RequestMatcher[]{ + new AntPathRequestMatcher("/v1/products/**"), + }), ; private final RequestMatcher[] matchers; + + public static boolean isBothEndpoint(HttpServletRequest request) { + return Arrays.stream(BOTH.getMatchers()) + .anyMatch(matcher -> matcher.matches(request)); + } + + public static boolean isPublicEndpoint(HttpServletRequest request) { + return Arrays.stream(PUBLIC.getMatchers()) + .anyMatch(matcher -> matcher.matches(request)); + } } diff --git a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java index b0f6ee25..74713730 100644 --- a/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java +++ b/src/main/java/taco/klkl/global/config/security/TokenAuthenticationFilter.java @@ -1,7 +1,6 @@ package taco.klkl.global.config.security; import java.io.IOException; -import java.util.Arrays; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -18,6 +17,7 @@ import taco.klkl.domain.token.exception.TokenExpiredException; import taco.klkl.domain.token.exception.TokenInvalidException; import taco.klkl.domain.token.service.TokenProvider; +import taco.klkl.global.error.exception.CustomException; import taco.klkl.global.util.ResponseUtil; import taco.klkl.global.util.TokenUtil; @@ -32,8 +32,8 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter { @Override protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException { if ("GET".equalsIgnoreCase(request.getMethod())) { - return Arrays.stream(SecurityEndpoint.PUBLIC.getMatchers()) - .anyMatch(matcher -> matcher.matches(request)); + return SecurityEndpoint.isPublicEndpoint(request) + && !SecurityEndpoint.isBothEndpoint(request); } return false; } @@ -44,24 +44,28 @@ protected void doFilterInternal( HttpServletResponse response, FilterChain filterChain ) throws ServletException, IOException { + final String accessToken = tokenUtil.resolveToken(request); + + if (accessToken == null && SecurityEndpoint.isBothEndpoint(request)) { + proceedWithoutAuthentication(request, response, filterChain); + return; + } + try { - String accessToken = tokenUtil.resolveToken(request); if (tokenProvider.validateToken(accessToken)) { setAuthentication(accessToken); } else { - String reissueAccessToken = tokenProvider.reissueAccessToken(accessToken); + final String reissueAccessToken = tokenProvider.reissueAccessToken(accessToken); if (StringUtils.hasText(reissueAccessToken)) { setAuthentication(reissueAccessToken); tokenUtil.addAccessTokenCookie(response, reissueAccessToken); } } } catch (TokenInvalidException | TokenExpiredException e) { - SecurityContextHolder.clearContext(); - responseUtil.sendErrorResponse(response, e); + handleTokenException(request, response, filterChain, e); return; } catch (Exception e) { - SecurityContextHolder.clearContext(); - responseUtil.sendErrorResponse(response, new UnauthorizedException()); + handleTokenException(request, response, filterChain, new UnauthorizedException()); return; } @@ -72,4 +76,26 @@ private void setAuthentication(final String accessToken) { Authentication authentication = tokenProvider.getAuthentication(accessToken); SecurityContextHolder.getContext().setAuthentication(authentication); } + + private void handleTokenException( + HttpServletRequest request, + HttpServletResponse response, + FilterChain filterChain, + CustomException ex + ) throws IOException, ServletException { + SecurityContextHolder.clearContext(); + if (SecurityEndpoint.isBothEndpoint(request)) { + proceedWithoutAuthentication(request, response, filterChain); + } else { + responseUtil.sendErrorResponse(response, ex); + } + } + + private void proceedWithoutAuthentication( + HttpServletRequest request, + HttpServletResponse response, + FilterChain filterChain + ) throws IOException, ServletException { + filterChain.doFilter(request, response); + } } From 6aa35fd823f37ce614a366830190a3bf11a11e1a Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 14:12:29 +0900 Subject: [PATCH 3/4] KL-163/refactor: apply same util method --- .../taco/klkl/domain/product/service/ProductServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/taco/klkl/domain/product/service/ProductServiceImpl.java b/src/main/java/taco/klkl/domain/product/service/ProductServiceImpl.java index b1f12b5f..86ae00ab 100644 --- a/src/main/java/taco/klkl/domain/product/service/ProductServiceImpl.java +++ b/src/main/java/taco/klkl/domain/product/service/ProductServiceImpl.java @@ -138,7 +138,7 @@ public ProductDetailResponse createProduct(final ProductCreateUpdateRequest crea Set tags = createTagsByTagIds(createRequest.tagIds()); product.addTags(tags); } - return ProductDetailResponse.from(product, false); + return productUtil.createProductDetailResponse(product); } @Override From b36c15e83018ad01abfc28e1cc92498469241742 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 14:30:37 +0900 Subject: [PATCH 4/4] KL-163/test: fix test errors --- .../klkl/domain/product/service/ProductServiceImplTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/taco/klkl/domain/product/service/ProductServiceImplTest.java b/src/test/java/taco/klkl/domain/product/service/ProductServiceImplTest.java index 04bb6454..c0f7af47 100644 --- a/src/test/java/taco/klkl/domain/product/service/ProductServiceImplTest.java +++ b/src/test/java/taco/klkl/domain/product/service/ProductServiceImplTest.java @@ -425,6 +425,9 @@ void testCreateProduct() { ReflectionTestUtils.setField(savedProduct, "id", 1L); return savedProduct; }); + ProductDetailResponse mockResponse = mock(ProductDetailResponse.class); + when(mockResponse.id()).thenReturn(1L); + when(productUtil.createProductDetailResponse(any(Product.class))).thenReturn(mockResponse); // When ProductDetailResponse result = productService.createProduct(productCreateUpdateRequest);