-
Notifications
You must be signed in to change notification settings - Fork 0
feat(User,Token): 리프레시 토큰 중복 저장 로직 수정 #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The head ref may contain hidden characters: "feature/#69-\uB9AC\uD504\uB808\uC2DC\uD1A0\uD070\uC911\uBCF5\uC800\uC7A5\uB85C\uC9C1\uC218\uC815"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,12 @@ | ||
| package com.nowait.applicationadmin.user.serivce; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.Optional; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.ResponseCookie; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.authentication.AuthenticationProvider; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.Authentication; | ||
|
|
@@ -13,6 +19,8 @@ | |
| import com.nowait.applicationadmin.user.dto.ManagerLoginResponseDto; | ||
| import com.nowait.applicationadmin.user.dto.ManagerSignupRequestDto; | ||
| import com.nowait.applicationadmin.user.dto.ManagerSignupResponseDto; | ||
| import com.nowait.domaincorerdb.token.entity.Token; | ||
| import com.nowait.domaincorerdb.token.repository.TokenRepository; | ||
| import com.nowait.domaincorerdb.user.entity.MemberDetails; | ||
| import com.nowait.domaincorerdb.user.entity.User; | ||
| import com.nowait.domaincorerdb.user.repository.UserRepository; | ||
|
|
@@ -25,6 +33,7 @@ | |
| @Slf4j | ||
| public class UserService { | ||
| private final UserRepository userRepository; | ||
| private final TokenRepository tokenRepository; | ||
| private final PasswordEncoder passwordEncoder; | ||
| private final AuthenticationProvider authenticationProvider; | ||
| private final JwtUtil jwtUtil; | ||
|
|
@@ -54,19 +63,50 @@ private void validateNickNameDuplicated(String nickName) { | |
| ); | ||
| } | ||
| @Transactional | ||
| public ManagerLoginResponseDto login(ManagerLoginRequestDto managerLoginRequestDto) { | ||
| public ResponseEntity<ManagerLoginResponseDto> login(ManagerLoginRequestDto managerLoginRequestDto) { | ||
| Authentication authentication = authenticationProvider.authenticate( | ||
| new UsernamePasswordAuthenticationToken(managerLoginRequestDto.getEmail(), managerLoginRequestDto.getPassword()) | ||
| new UsernamePasswordAuthenticationToken( | ||
| managerLoginRequestDto.getEmail(), | ||
| managerLoginRequestDto.getPassword() | ||
| ) | ||
| ); | ||
| MemberDetails memberDetails = (MemberDetails) authentication.getPrincipal(); | ||
| User user = userRepository.getReferenceById(memberDetails.getId()); | ||
|
|
||
| long currentAccessTokenExpiration = accessTokenExpiration; | ||
| if (user.getRole() == com.nowait.common.enums.Role.SUPER_ADMIN) { | ||
| currentAccessTokenExpiration = 7L * 24 * 60 * 60 * 1000L; // 7일 | ||
| currentAccessTokenExpiration = 100L * 24 * 60 * 60 * 1000L; // 100일 | ||
| } | ||
|
|
||
| String accessToken = jwtUtil.createAccessToken("accessToken", user.getId(), String.valueOf(user.getRole()), currentAccessTokenExpiration); | ||
| return ManagerLoginResponseDto.fromEntity(user,accessToken); | ||
| String refreshToken = jwtUtil.createRefreshToken("refreshToken", user.getId(), 30L * 24 * 60 * 60 * 1000L); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 하드코딩된 만료 시간을 설정값으로 추출하세요. 30일 만료 시간이 여러 곳에 하드코딩되어 있습니다. 유지보수성을 위해 설정값으로 추출하는 것을 권장합니다. 설정 파일에 다음을 추가하세요: jwt.refresh-token-expiration-days=30그리고 다음과 같이 수정하세요: +@Value("${jwt.refresh-token-expiration-days}")
+private long refreshTokenExpirationDays;
-String refreshToken = jwtUtil.createRefreshToken("refreshToken", user.getId(), 30L * 24 * 60 * 60 * 1000L);
+String refreshToken = jwtUtil.createRefreshToken("refreshToken", user.getId(), refreshTokenExpirationDays * 24 * 60 * 60 * 1000L);
-token.updateRefreshToken(refreshToken, LocalDateTime.now().plusDays(30L));
+token.updateRefreshToken(refreshToken, LocalDateTime.now().plusDays(refreshTokenExpirationDays));
-expiredDate(LocalDateTime.now().plusDays(30L))
+expiredDate(LocalDateTime.now().plusDays(refreshTokenExpirationDays))
-maxAge(30L * 24 * 60 * 60)
+maxAge(refreshTokenExpirationDays * 24 * 60 * 60)Also applies to: 88-88, 94-94, 102-102 🤖 Prompt for AI Agents |
||
|
|
||
| // 기존 토큰 존재 확인 | ||
| Optional<Token> tokenOptional = tokenRepository.findByUserId(user.getId()); | ||
| if (tokenOptional.isPresent()) { | ||
| Token token = tokenOptional.get(); | ||
| token.updateRefreshToken(refreshToken, LocalDateTime.now().plusDays(30L)); // 엔티티에 update 메소드 구현 권장 | ||
| } else { | ||
| tokenRepository.save( | ||
| Token.builder() | ||
| .user(user) | ||
| .refreshToken(refreshToken) | ||
| .expiredDate(LocalDateTime.now().plusDays(30L)) | ||
| .build() | ||
| ); | ||
| } | ||
| ResponseCookie refreshTokenCookie = ResponseCookie.from("refreshToken", refreshToken) | ||
| .httpOnly(true) | ||
| .secure(true) // 운영환경에 맞게 | ||
| .path("/") | ||
| .maxAge(30L * 24 * 60 * 60) | ||
| .sameSite("Strict") | ||
| .build(); | ||
|
|
||
| return ResponseEntity.ok() | ||
| .header(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()) | ||
| .body(ManagerLoginResponseDto.fromEntity(user, accessToken)); | ||
|
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
SUPER_ADMIN 토큰 만료 시간이 너무 길 수 있습니다.
100일의 액세스 토큰 만료 시간은 보안상 위험할 수 있습니다. 더 짧은 기간(예: 7일)을 고려해보세요.
📝 Committable suggestion
🤖 Prompt for AI Agents