Skip to content

Commit 72a37a0

Browse files
authored
Merge pull request #266 from CodIN-INU/develop
Fix(security): permitAll 경로에서도 토큰이 있으면 인증 세팅되도록 수정
2 parents b7ca3bd + a17eb6a commit 72a37a0

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

src/main/java/inu/codin/codin/common/security/filter/JwtAuthenticationFilter.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import lombok.RequiredArgsConstructor;
1010
import org.springframework.security.core.context.SecurityContextHolder;
1111
import org.springframework.util.AntPathMatcher;
12+
import org.springframework.util.StringUtils;
1213
import org.springframework.web.filter.OncePerRequestFilter;
1314

1415
import java.io.IOException;
@@ -36,10 +37,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
3637

3738
String requestURI = request.getRequestURI();
3839

39-
if (permitAllProperties.getUrls().stream().anyMatch(url -> pathMatcher.match(url, requestURI))) {
40-
filterChain.doFilter(request, response);
41-
return;
42-
}
40+
final boolean isPermitAll = permitAllProperties.getUrls().stream()
41+
.anyMatch(url -> pathMatcher.match(url, requestURI));
4342

4443
String token = null;
4544
if (Arrays.stream(SWAGGER_AUTH_PATHS).anyMatch(url -> pathMatcher.match(url, requestURI))) {
@@ -49,10 +48,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
4948
}
5049

5150
// Access Token이 있는 경우
52-
if (token != null) {
51+
if (StringUtils.hasText(token)) {
5352
jwtService.getUserDetailsAndSetAuthentication(token);
5453
} else {
5554
SecurityContextHolder.clearContext();
55+
56+
if (isPermitAll) {
57+
filterChain.doFilter(request, response);
58+
return;
59+
}
5660
}
5761

5862
filterChain.doFilter(request, response);

src/main/java/inu/codin/codin/common/security/service/JwtService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import lombok.RequiredArgsConstructor;
1717
import lombok.extern.slf4j.Slf4j;
1818
import org.springframework.beans.factory.annotation.Value;
19-
import org.springframework.http.server.ServletServerHttpRequest;
2019
import org.springframework.security.core.context.SecurityContextHolder;
2120
import org.springframework.security.core.userdetails.UserDetails;
2221
import org.springframework.stereotype.Service;
22+
import org.springframework.util.StringUtils;
2323

2424
/**
2525
* JWT 토큰 관련 비즈니스 로직을 처리하는 서비스
@@ -192,6 +192,11 @@ public void getUserDetailsAndSetAuthentication(String token) {
192192

193193
public String getAccessToken(HttpServletRequest request) {
194194
String accessToken = jwtUtils.getAccessToken(request);
195+
196+
if (!StringUtils.hasText(accessToken)) {
197+
return null;
198+
}
199+
195200
if (!jwtTokenProvider.validType(accessToken, "access")) {
196201
log.error("[getAccessToken] Access Token이 아닙니다.");
197202
throw new JwtException(SecurityErrorCode.INVALID_TYPE, "Access Token이 아닙니다.");

src/main/java/inu/codin/codin/domain/post/domain/hits/entity/HitsEntity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1515
public class HitsEntity {
1616

17-
@Id @NotBlank
17+
@Id
1818
private ObjectId _id;
1919

20-
@NotBlank
2120
private ObjectId userId;
2221

23-
@NotBlank
2422
private ObjectId postId;
2523

2624
@Builder

src/main/java/inu/codin/codin/domain/post/service/PostInteractionService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public void deletePostImageInternal(PostEntity post, String imageUrl) {
4747
}
4848

4949
// [HitsService] - 조회수 증가 처리
50+
// 비로그인(null) → 무조건 증가, 로그인 → 중복 아닐 때만 증가
5051
public void increaseHits(PostEntity post, ObjectId userId) {
51-
if (!hitsService.validateHits(post.get_id(), userId)) {
52+
if (userId==null || !hitsService.validateHits(post.get_id(), userId)) {
5253
hitsService.addHits(post.get_id(), userId);
5354
log.info("조회수 업데이트. PostId: {}, UserId: {}", post.get_id(), userId);
5455
}

0 commit comments

Comments
 (0)