Skip to content

Commit 5edeb9e

Browse files
authored
Merge pull request #455 from TaskFlow-CLAP/release
Release-Develop 병합
2 parents ce4b7c7 + 0235534 commit 5edeb9e

File tree

15 files changed

+97
-86
lines changed

15 files changed

+97
-86
lines changed

src/main/java/clap/server/adapter/inbound/security/filter/LoginAttemptFilter.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package clap.server.adapter.inbound.security.filter;
22

33
import clap.server.application.port.inbound.auth.CheckAccountLockStatusUseCase;
4-
import clap.server.application.service.auth.LoginAttemptService;
54
import clap.server.exception.AuthException;
65
import jakarta.servlet.FilterChain;
76
import jakarta.servlet.ServletException;
@@ -14,8 +13,10 @@
1413
import org.springframework.security.core.context.SecurityContextHolder;
1514
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
1615
import org.springframework.web.filter.OncePerRequestFilter;
16+
import org.springframework.web.util.ContentCachingRequestWrapper;
1717

1818
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
1920
import java.util.ArrayList;
2021

2122
import static clap.server.adapter.inbound.security.WebSecurityUrl.LOGIN_ENDPOINT;
@@ -33,9 +34,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
3334
throws ServletException, IOException {
3435
try {
3536
if (request.getRequestURI().equals(LOGIN_ENDPOINT)) {
36-
String clientIp = getClientIp(request);
37-
38-
checkAccountLockStatusUseCase.checkAccountIsLocked(clientIp);
37+
String nickname = request.getParameter("nickname");
38+
checkAccountLockStatusUseCase.checkAccountIsLocked(nickname);
3939

4040
}
4141
} catch (AuthException e) {
@@ -54,4 +54,14 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
5454
filterChain.doFilter(request, response);
5555
}
5656

57+
private String getRequestBody(HttpServletRequest request) {
58+
try {
59+
ContentCachingRequestWrapper cachingRequest = (ContentCachingRequestWrapper) request;
60+
byte[] content = cachingRequest.getContentAsByteArray();
61+
return new String(content, StandardCharsets.UTF_8);
62+
} catch (Exception e) {
63+
return "요청 바디의 내용을 읽을 수 없음";
64+
}
65+
}
66+
5767
}

src/main/java/clap/server/adapter/inbound/web/auth/AuthController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.swagger.v3.oas.annotations.Parameter;
1313
import io.swagger.v3.oas.annotations.tags.Tag;
1414
import jakarta.servlet.http.HttpServletRequest;
15+
import jakarta.validation.constraints.NotBlank;
1516
import lombok.RequiredArgsConstructor;
1617
import lombok.extern.slf4j.Slf4j;
1718
import org.springframework.http.ResponseEntity;
@@ -32,11 +33,11 @@ public class AuthController {
3233
@LogType(LogStatus.LOGIN)
3334
@Operation(summary = "로그인 API")
3435
@PostMapping("/login")
35-
public ResponseEntity<LoginResponse> login(
36+
public ResponseEntity<LoginResponse> login(@RequestParam @NotBlank String nickname,
3637
@RequestBody LoginRequest request,
3738
HttpServletRequest httpRequest) {
3839
String clientIp = getClientIp(httpRequest);
39-
LoginResponse response = loginUsecase.login(request.nickname(), request.password(), clientIp);
40+
LoginResponse response = loginUsecase.login(nickname, request.password(), clientIp);
4041
return ResponseEntity.ok(response);
4142
}
4243

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package clap.server.adapter.inbound.web.dto.auth.request;
22

3-
import jakarta.validation.constraints.NotNull;
3+
import jakarta.validation.constraints.NotBlank;
44

55
public record LoginRequest(
6-
@NotNull
7-
String nickname,
8-
@NotNull
6+
@NotBlank
97
String password
108
) {
119
}

src/main/java/clap/server/adapter/outbound/infrastructure/redis/log/LoginLogAdapter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public void deleteById(String clientIp) {
2525
loginLogRepository.deleteById(clientIp);
2626
}
2727

28-
public Optional<LoginLog> findByClientIp(String clientIp) {
29-
return loginLogRepository.findById(clientIp).map(loginLogMapper::toDomain);
28+
@Override
29+
public Optional<LoginLog> findByNickname(String nickname) {
30+
return loginLogRepository.findById(nickname).map(loginLogMapper::toDomain);
3031
}
3132
}

src/main/java/clap/server/adapter/outbound/infrastructure/redis/log/LoginLogEntity.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
import java.time.LocalDateTime;
1515

1616
@Getter
17-
@RedisHash("loginLog")
17+
@RedisHash(value = "loginLog", timeToLive = 3600)
1818
@Builder
19-
@ToString(of = {"clientIp", "attemptNickname", "lastAttemptAt", "failedCount", "isLocked"})
20-
@EqualsAndHashCode(of = {"clientIp"})
19+
@ToString(of = {"nickname", "clientIp", "lastAttemptAt", "failedCount", "isLocked"})
20+
@EqualsAndHashCode(of = {"nickname"})
2121
public class LoginLogEntity {
2222
@Id
23-
private String clientIp;
23+
private String nickname;
2424

25-
private String attemptNickname;
25+
private String clientIp;
2626

2727
@JsonSerialize(using = ToStringSerializer.class)
2828
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package clap.server.application.port.inbound.auth;
22

33
public interface CheckAccountLockStatusUseCase {
4-
void checkAccountIsLocked(String clientIp);
4+
void checkAccountIsLocked(String nickname);
55
}

src/main/java/clap/server/application/port/outbound/auth/loginLog/LoadLoginLogPort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
import java.util.Optional;
66

77
public interface LoadLoginLogPort {
8-
Optional<LoginLog> findByClientIp(String clientIp);
8+
Optional<LoginLog> findByNickname(String nickname);
99
}

src/main/java/clap/server/application/service/auth/AuthService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public LoginResponse login(String nickname, String password, String clientIp) {
4646

4747
CustomJwts jwtTokens = manageTokenService.issueTokens(member);
4848
refreshTokenService.saveRefreshToken(manageTokenService.issueRefreshToken(member.getMemberId()));
49-
loginAttemptService.resetFailedAttempts(clientIp);
49+
loginAttemptService.resetFailedAttempts(nickname);
5050
return AuthResponseMapper.toLoginResponse(jwtTokens.accessToken(), jwtTokens.refreshToken());
5151
}
5252

@@ -71,14 +71,14 @@ private void deleteAccessToken(Long memberId, String accessToken) {
7171
private Member getMember(String inputNickname, String clientIp) {
7272
return loadMemberPort.findByNickname(inputNickname).orElseThrow(() ->
7373
{
74-
loginAttemptService.recordFailedAttempt(clientIp, inputNickname);
74+
loginAttemptService.recordFailedAttempt(inputNickname, clientIp);
7575
return new AuthException(AuthErrorCode.LOGIN_REQUEST_FAILED);
7676
});
7777
}
7878

7979
private void validatePassword(String inputPassword, String encodedPassword, String inputNickname, String clientIp) {
8080
if (!passwordEncoder.matches(inputPassword, encodedPassword)) {
81-
loginAttemptService.recordFailedAttempt(clientIp, inputNickname);
81+
loginAttemptService.recordFailedAttempt(inputNickname, clientIp);
8282
throw new AuthException(AuthErrorCode.LOGIN_REQUEST_FAILED);
8383
}
8484
}

src/main/java/clap/server/application/service/auth/LoginAttemptService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public class LoginAttemptService implements CheckAccountLockStatusUseCase {
2222
private static final int MAX_FAILED_ATTEMPTS = 5;
2323
private static final long LOCK_TIME_DURATION = 30 * 60 * 1000; // 30분 (밀리초)
2424

25-
public void recordFailedAttempt(String clientIp, String attemptNickname) {
26-
LoginLog loginLog = loadLoginLogPort.findByClientIp(clientIp).orElse(null);
25+
public void recordFailedAttempt(String nickname, String clientIp) {
26+
LoginLog loginLog = loadLoginLogPort.findByNickname(nickname).orElse(null);
2727
if (loginLog == null) {
28-
loginLog = LoginLog.createLoginLog(clientIp, attemptNickname);
28+
loginLog = LoginLog.createLoginLog(nickname, clientIp);
2929
} else {
3030
int attemptCount = loginLog.recordFailedAttempt();
3131
if (attemptCount >= MAX_FAILED_ATTEMPTS) {
@@ -38,8 +38,8 @@ public void recordFailedAttempt(String clientIp, String attemptNickname) {
3838
}
3939

4040
@Override
41-
public void checkAccountIsLocked(String clientIp) {
42-
LoginLog loginLog = loadLoginLogPort.findByClientIp(clientIp).orElse(null);
41+
public void checkAccountIsLocked(String nickname) {
42+
LoginLog loginLog = loadLoginLogPort.findByNickname(nickname).orElse(null);
4343
if (loginLog == null) {
4444
return;
4545
}
@@ -53,12 +53,12 @@ public void checkAccountIsLocked(String clientIp) {
5353
if (minutesSinceLastAttemptInMillis <= LOCK_TIME_DURATION) {
5454
throw new AuthException(AuthErrorCode.ACCOUNT_IS_LOCKED);
5555
}
56-
else commandLoginLogPort.deleteById(clientIp);
56+
else commandLoginLogPort.deleteById(nickname);
5757
}
5858
}
5959

6060

61-
public void resetFailedAttempts(String clientIp) {
62-
commandLoginLogPort.deleteById(clientIp);
61+
public void resetFailedAttempts(String nickname) {
62+
commandLoginLogPort.deleteById(nickname);
6363
}
6464
}

src/main/java/clap/server/application/service/log/LogService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void createMemberLog(HttpServletRequest request, int statusCode, String c
3535
}
3636

3737
public void createLoginFailedLog(HttpServletRequest request, int statusCode, String customCode, LogStatus logStatus, String requestBody, String nickName) {
38-
LoginLog loginLog = loadLoginLogPort.findByClientIp(ClientIpParseUtil.getClientIp(request)).orElse(null);
38+
LoginLog loginLog = loadLoginLogPort.findByNickname(nickName).orElse(null);
3939
String responseBody = loginLog != null ? loginLog.toSummaryString() : null;
4040
AnonymousLog anonymousLog = AnonymousLog.createAnonymousLog(request, statusCode,customCode, logStatus, responseBody, requestBody, nickName);
4141
commandLogPort.saveAnonymousLog(anonymousLog);

0 commit comments

Comments
 (0)