Skip to content

Commit 5694df4

Browse files
committed
hotfix : Guest API 접근 제한 security 로직 수정
1 parent 38d371a commit 5694df4

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/main/java/com/neighbors/tohero/common/ErrorResponseUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ErrorResponseUtil {
1313

1414
public static void setResponse(HttpServletResponse response, BaseResponseStatus responseStatus) throws IOException {
1515

16-
BaseResponse errorResponse = new BaseResponse(responseStatus, "JWT TOKEN 오류입니다.");
16+
BaseResponse errorResponse = new BaseResponse(responseStatus, "THIS API NEED AUTHORIZED JWT TOKEN (MAYBE NOT GUEST TOKEN)");
1717

1818
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
1919
response.setContentType("application/json");

src/main/java/com/neighbors/tohero/common/security/AuthenticationUtil.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.neighbors.tohero.common.enums.Role;
44
import com.neighbors.tohero.common.jwt.JwtProvider;
5+
import jakarta.annotation.PostConstruct;
56
import jakarta.servlet.http.HttpServletRequest;
67
import lombok.RequiredArgsConstructor;
78
import lombok.extern.slf4j.Slf4j;
@@ -10,14 +11,32 @@
1011
import org.springframework.stereotype.Component;
1112
import org.springframework.util.StringUtils;
1213

13-
import java.util.Optional;
14+
import java.util.*;
1415

1516
@Slf4j
1617
@Component
1718
@RequiredArgsConstructor
1819
public class AuthenticationUtil {
1920

2021
private final JwtProvider jwtProvider;
22+
private Map<String, List<String>> onlyUserRequest;
23+
24+
@PostConstruct
25+
private void initOnlyUserRequest() {
26+
onlyUserRequest = new HashMap<>();
27+
28+
// 초기화
29+
addToOnlyUserRequest("PUT", "/user/name");
30+
addToOnlyUserRequest("POST", "/user/signout");
31+
addToOnlyUserRequest("POST", "/user/logout");
32+
addToOnlyUserRequest("GET", "/letter");
33+
addToOnlyUserRequest("PUT", "/letter");
34+
addToOnlyUserRequest("GET", "/auth/refreshToken");
35+
}
36+
37+
private void addToOnlyUserRequest(String method, String url) {
38+
onlyUserRequest.computeIfAbsent(method, k -> new ArrayList<>()).add(url);
39+
}
2140

2241
public void setAuthenticationFromRequest(HttpServletRequest request) {
2342

@@ -42,9 +61,11 @@ private Optional<UserAuthentication> makeAuthentication(HttpServletRequest reque
4261

4362
if(isTokenValid(token)) {
4463
if (isRequestAvailableToGuest(token)) {
45-
log.info("[AuthenticationUtil.makeAuthentication : Guest 권한 부여]");
46-
String nickname = jwtProvider.getGuestJwtUserDetails(token).getNickname();
47-
authentication = UserAuthentication.makeGuestAuthentication(nickname);
64+
if(checkGuestAccessRequest(request)){
65+
log.info("[AuthenticationUtil.makeAuthentication : Guest 권한 부여]");
66+
String nickname = jwtProvider.getGuestJwtUserDetails(token).getNickname();
67+
authentication = UserAuthentication.makeGuestAuthentication(nickname);
68+
}
4869
}
4970
else {
5071
log.info("[AuthenticationUtil.makeAuthentication : User 권한 부여]");
@@ -59,6 +80,18 @@ private Optional<UserAuthentication> makeAuthentication(HttpServletRequest reque
5980
return Optional.ofNullable(authentication);
6081
}
6182

83+
private boolean checkGuestAccessRequest(HttpServletRequest request) {
84+
List<String> urls = onlyUserRequest.get(request.getMethod());
85+
if (urls != null) {
86+
for (String url : urls) {
87+
if (request.getRequestURI().contains(url)) {
88+
return false;
89+
}
90+
}
91+
}
92+
return true;
93+
}
94+
6295
private String getJwtFromRequest(HttpServletRequest request) {
6396
String bearerToken = request.getHeader("Authorization");
6497

0 commit comments

Comments
 (0)