Skip to content
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

[refact] 리프레시 토큰 발급 로직 수정 #186

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main/java/ceos/backend/domain/admin/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ceos.backend.global.config.user.AdminDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -38,9 +39,10 @@ public void signUp(@RequestBody @Valid SignUpRequest signUpRequest) {

@Operation(summary = "로그인")
@PostMapping("/signin")
public TokenResponse signIn(@RequestBody @Valid SignInRequest signInRequest) {
public TokenResponse signIn(HttpServletRequest request, @RequestBody @Valid SignInRequest signInRequest) {
log.info("로그인");
return adminService.signIn(signInRequest);
String device = request.getHeader("User-Agent").contains("mobile") ? "mobile" : "web";
return adminService.signIn(device, signInRequest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 이런 식으로 모바일과 웹을 구분할 수 있군용👀👀

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum 쓰는게 나을 듯 싶긴한데.... 생각해보면 여기서 밖에 안쓰이니 굳이 안넣어도 되겠군요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum에 대한 의견은 "mobile", "web" 값을 관리하기 위함인가요? 그렇다면 말씀해주셨던 것처럼 여기서만 쓰이고 있으니 상수로 관리해주는 것도 괜찮은 방법일 것 같습니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용 범위가 좁아서 그냥 문자열로 사용했는데 상수로 관리하는 것도 좋은 것 같네요!
바꾸겠습니다🥰 좋은 의견 감사해용

}

@Operation(summary = "아이디 찾기")
Expand Down Expand Up @@ -68,9 +70,10 @@ public void resetPwd(

@Operation(summary = "로그아웃")
@PostMapping("/logout")
public void logout(@AuthenticationPrincipal AdminDetails adminUser) {
public void logout(HttpServletRequest request, @AuthenticationPrincipal AdminDetails adminUser) {
log.info("로그아웃");
adminService.logout(adminUser);
String device = request.getHeader("User-Agent").contains("mobile") ? "mobile" : "web";
adminService.logout(device, adminUser);
}

@Operation(summary = "토큰 재발급")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ public void signUp(SignUpRequest signUpRequest) {
}

@Transactional
public TokenResponse signIn(SignInRequest signInRequest) {
public TokenResponse signIn(String device, SignInRequest signInRequest) {

final Admin admin = adminHelper.findForSignIn(signInRequest);
final Authentication authentication = adminHelper.adminAuthorizationInput(admin);

adminHelper.checkRole(admin);

String redisKey = admin.getId().toString() + ":" + device;

// 토큰 발급
final String accessToken = tokenProvider.createAccessToken(admin.getId(), authentication);
final String refreshToken = tokenProvider.createRefreshToken(admin.getId(), authentication);
final String refreshToken = tokenProvider.createRefreshToken(admin.getId(), authentication, redisKey);

return adminMapper.toTokenResponse(accessToken, refreshToken);
}
Expand Down Expand Up @@ -97,11 +99,13 @@ public void resetPwd(ResetPwdRequest resetPwdRequest, AdminDetails adminUser) {
}

@Transactional
public void logout(AdminDetails adminUser) {
public void logout(String device, AdminDetails adminUser) {
final Admin admin = adminUser.getAdmin();

String redisKey = admin.getId().toString() + ":" + device;

// 레디스 삭제
tokenProvider.deleteRefreshToken(admin.getId());
tokenProvider.deleteRefreshToken(redisKey);
}

@Transactional
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/ceos/backend/global/config/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public RedisConnectionFactory redisConnectionFactory() {

@Bean
public RedisTemplate<String, String> redisTemplate() {
// redisTemplate를 받아와서 set, get, delete를 사용
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
// setKeySerializer, setValueSerializer 설정
// redis-cli을 통해 직접 데이터를 조회 시 알아볼 수 없는 형태로 출력되는 것을 방지
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public String createAccessToken(Long id, Authentication authentication) {
.compact();
}

public String createRefreshToken(Long id, Authentication authentication) {
public String createRefreshToken(Long id, Authentication authentication, String redisKey) {
String authorities =
authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
Expand All @@ -107,13 +107,13 @@ public String createRefreshToken(Long id, Authentication authentication) {

redisTemplate
.opsForValue()
.set(id.toString(), refreshToken, refreshExpirationTime, TimeUnit.SECONDS);
.set(redisKey, refreshToken, refreshExpirationTime, TimeUnit.SECONDS);

return refreshToken;
}

public void deleteRefreshToken(Long id) {
redisTemplate.delete(id.toString());
public void deleteRefreshToken(String redisKey) {
redisTemplate.delete(redisKey);
}

public String getTokenUserId(String token) {
Expand Down