Skip to content

Commit

Permalink
๐Ÿ› Fix: logout ๋ฒ„๊ทธ ์ˆ˜์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
lej8924 committed Jul 24, 2024
1 parent 1b36c43 commit 6a4a4b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import lombok.Data;
import lombok.NoArgsConstructor;


public record JwtTokenResponse(String grantType,String accessToken,String refreshToken) {

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JwtTokenResponse {
private String grantType;
private String accessToken;
private String refreshToken;
}
22 changes: 14 additions & 8 deletions src/main/java/com/ticle/server/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -62,7 +63,7 @@ public JwtTokenResponse signIn(LoginRequest loginRequest){
// SecurityContextHolder.getContext().setAuthentication(authentication);

JwtTokenResponse jwtTokenResponse = jwtTokenProvider.generateToken(authentication);
redisDao.setRefreshToken(email, jwtTokenResponse.refreshToken(), ExpireTime.REFRESH_TOKEN_EXPIRE_TIME);
redisDao.setRefreshToken(email, jwtTokenResponse.getRefreshToken(), ExpireTime.REFRESH_TOKEN_EXPIRE_TIME);

return jwtTokenResponse;
}
Expand All @@ -79,14 +80,16 @@ public UserResponse signUp(JoinRequest joinRequest){
}


@CacheEvict(cacheNames = CacheNames.USERBYEMAIL, key = "#p1")
// @CacheEvict(cacheNames = CacheNames.USERBYEMAIL, key = "#p1")
@Transactional
public ResponseEntity logout(CustomUserDetails customUserDetails, HttpServletRequest request) {
String email = "";
String accessToken = jwtTokenProvider.resolveToken(request);
if(userRepository.findById(customUserDetails.getUserId()).isPresent()){
email = userRepository.findById(customUserDetails.getUserId()).get().getEmail();
}
log.info("์—ฌ๊ธฐ๊นŒ์ง„ ์‹คํ–‰ํ•จ!");

User user = userRepository.findById(customUserDetails.getUserId()).orElseThrow(()-> {
log.info("useruseruser");
return new UserNotFoundException(USER_NOT_FOUND);});
String email = user.getEmail();
// ๋ ˆ๋””์Šค์— accessToken ์‚ฌ์šฉ๋ชปํ•˜๋„๋ก ๋“ฑ๋ก
Long expiration = jwtTokenProvider.getExpiration(accessToken);
redisDao.setBlackList(accessToken, "logout", expiration);
Expand All @@ -95,6 +98,9 @@ public ResponseEntity logout(CustomUserDetails customUserDetails, HttpServletReq
} else {
throw new IllegalArgumentException("์ด๋ฏธ ๋กœ๊ทธ์•„์›ƒํ•œ ์œ ์ €์ž…๋‹ˆ๋‹ค.");
}

log.info("Logout successful for user with email: {}", email);

return ResponseEntity.ok("๋กœ๊ทธ์•„์›ƒ ์™„๋ฃŒ");
}

Expand All @@ -116,8 +122,8 @@ public JwtTokenResponse reissueAtk(CustomUserDetails customUserDetails,String re
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email,null,user.get().getAuthorities());
// Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
JwtTokenResponse jwtTokenResponse = jwtTokenProvider.generateToken(authenticationToken);
String newAccessToken = jwtTokenResponse.accessToken();
String newRefreshToken = jwtTokenResponse.refreshToken();
String newAccessToken = jwtTokenResponse.getAccessToken();
String newRefreshToken = jwtTokenResponse.getRefreshToken();

redisDao.setRefreshToken(email, newRefreshToken, ExpireTime.REFRESH_TOKEN_EXPIRE_TIME);

Expand Down

0 comments on commit 6a4a4b0

Please sign in to comment.