Skip to content

Commit

Permalink
Merge pull request #70 from taco-official/KL-163/프론트-요구사항-처리
Browse files Browse the repository at this point in the history
fix(KL-163): handle isLiked error
  • Loading branch information
ohhamma authored Oct 8, 2024
2 parents 65d3381 + b36c15e commit eb2dbcc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public ProductDetailResponse createProduct(final ProductCreateUpdateRequest crea
Set<Tag> tags = createTagsByTagIds(createRequest.tagIds());
product.addTags(tags);
}
return ProductDetailResponse.from(product, false);
return productUtil.createProductDetailResponse(product);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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/**"),
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application-h2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-mysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit eb2dbcc

Please sign in to comment.