Skip to content

Commit

Permalink
refactor : 로그인 횟수 5번 초과 한 유저 중 30분 지난 유저의 부분만 초기화 및 리팩토링
Browse files Browse the repository at this point in the history
- batch 시간 0분 마다
- 매 1분마다 체크시 마지막 로그인 시도 30분 지났는 지 확인하는 filter 추가
- errorCode 비밀번호 변경 -> 30분 후 다시 시도
- LocalDate -> LocalDateTime
  • Loading branch information
chwangmin committed May 22, 2024
1 parent 78735f5 commit 9ef3fe0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest, Http

TokenResponse tokenResponse = memberService.login(loginRequest.getEmail(), loginRequest.getPassword());

if(tokenResponse == null){
throw new AuthenticationException(ErrorCode.MEMBER_NOT_MATCH);
}

response.addHeader(JwtTokenProvider.AUTHORIZATION_HEADER, tokenResponse.getAccessToken());

Cookie cookie = new Cookie("refreshToken", tokenResponse.getRefreshToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
Expand Down Expand Up @@ -103,7 +106,7 @@ public TokenResponse login(String email, String password) {

} catch (AuthenticationException e){
member.getLoginAttempt().updateCount();
throw new AuthenticationException(ErrorCode.MEMBER_NOT_MATCH);
e.printStackTrace();
} catch (Exception e) {
member.getLoginAttempt().updateCount();
e.printStackTrace();
Expand Down Expand Up @@ -183,9 +186,13 @@ public void sendPassword(String email) {
}

@Transactional
public void initAttempt(){
loginAttemptRepository.findAll().stream()
public void initAttempt() {
LocalDateTime thirtyMinutesAgo = LocalDateTime.now().minusMinutes(30);
List<LoginAttempt> staleAttempts = loginAttemptRepository.findAll().stream()
.filter(loginAttempt -> loginAttempt.getCount() >= 5)
.forEach(LoginAttempt::initCount);
.filter(loginAttempt -> loginAttempt.getLoginRecentAttemp().isBefore(thirtyMinutesAgo))
.toList();

staleAttempts.forEach(LoginAttempt::initCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
@NoArgsConstructor
Expand All @@ -27,7 +27,7 @@ public class LoginAttempt {

@LastModifiedDate
@Column(name = "login_recent_attemp", columnDefinition = "datetime default CURRENT_TIMESTAMP")
private LocalDate loginRecentAttemp;
private LocalDateTime loginRecentAttemp;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
Expand All @@ -39,7 +39,7 @@ public LoginAttempt(Member member) {
}

public void updateCount(){
this.count++;
this.count = this.count + 1;
}

public void initCount(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@NoArgsConstructor
Expand Down Expand Up @@ -41,7 +42,7 @@ public class Member {

@LastModifiedDate
@Column(name = "modify_date", columnDefinition = "datetime default CURRENT_TIMESTAMP")
private LocalDate modifyDate;
private LocalDateTime modifyDate;

@Column(name = "is_deleted")
@ColumnDefault("false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class ScheduledJobConfiguration {
private final MemberService memberService;

@Scheduled(cron ="0 30 * * * *", zone = "Asia/Seoul")
@Scheduled(cron ="0 * * * * *", zone = "Asia/Seoul")
public void scheduledEndForm() {
memberService.initAttempt();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum ErrorCode {
INVALID_MEMBER_TYPE(HttpStatus.BAD_REQUEST, "M-001", "잘못된 회원 타입 입니다.(memberType : KAKAO)"),
ALREADY_REGISTERED_MEMBER(HttpStatus.BAD_REQUEST, "M-002", "이미 가입된 회원 입니다."),
MEMBER_NOT_EXISTS(HttpStatus.BAD_REQUEST, "M-003", "해당 회원은 존재하지 않습니다."),
MEMBER_COUNT_OUT(HttpStatus.BAD_REQUEST, "M-004", "해당 회원 로그인 시도 횟수가 초과되었습니다. (비밀번호 변경이 필요합니다.)"),
MEMBER_COUNT_OUT(HttpStatus.BAD_REQUEST, "M-004", "해당 회원 로그인 시도 횟수가 초과되었습니다. 30분 후 다시 시도하세요!"),
MEMBER_NOT_MATCH(HttpStatus.BAD_REQUEST, "M-005", " 아이디(로그인 전용 아이디) 또는 비밀번호를 잘못 입력했습니다.\n" +
"입력하신 내용을 다시 확인해주세요."),

Expand Down

0 comments on commit 9ef3fe0

Please sign in to comment.