Skip to content

Commit

Permalink
Merge pull request #295 from MOONSHOT-Team/feature/#294
Browse files Browse the repository at this point in the history
[Refactor] #294 - Redis 유저 조회 로직 개선
  • Loading branch information
its-sky authored Jul 2, 2024
2 parents 11220ba + 4cd2b95 commit 5459509
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.moonshot.user.service;

import static org.moonshot.response.ErrorType.NOT_FOUND_USER;
import static org.moonshot.response.ErrorType.NOT_SUPPORTED_LOGIN_PLATFORM;
import static org.moonshot.user.service.validator.UserValidator.hasChange;
import static org.moonshot.user.service.validator.UserValidator.validateUserAuthorization;
import static org.moonshot.response.ErrorType.*;
import static org.moonshot.user.service.validator.UserValidator.*;

import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.moonshot.exception.BadRequestException;
import org.moonshot.exception.NotFoundException;
import org.moonshot.jwt.JwtTokenProvider;
Expand All @@ -24,6 +21,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional
Expand All @@ -50,25 +50,20 @@ public TokenResponse reissue(final String refreshToken) {
}

public void logout(final Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));
validateUserAuthorization(user.getId(), userId);

jwtTokenProvider.deleteRefreshToken(userId);
}

public void withdrawal(final Long userId) {
User user = userRepository.findById(userId)
User user = userRepository.findByIdWithCache(userId)
.orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));
validateUserAuthorization(user.getId(), userId);

user.setDeleteAt();
jwtTokenProvider.deleteRefreshToken(userId);
}

public void modifyProfile(final Long userId, final UserInfoRequest request) {
User user = userRepository.findById(userId)
User user = userRepository.findByIdWithCache(userId)
.orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));
validateUserAuthorization(user.getId(), userId);

if (hasChange(request.nickname())) {
user.modifyNickname(request.nickname());
Expand All @@ -79,13 +74,13 @@ public void modifyProfile(final Long userId, final UserInfoRequest request) {
}

public UserInfoResponse getMyProfile(final Long userId) {
User user = userRepository.findById(userId)
User user = userRepository.findByIdWithCache(userId)
.orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));
return UserInfoResponse.of(user);
}

public void updateUserProfileImage(final Long userId, final String imageUrl) {
User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));
User user = userRepository.findByIdWithCache(userId).orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));
user.modifyProfileImage(imageUrl);
}

Expand Down
44 changes: 17 additions & 27 deletions moonshot-auth/src/main/java/org/moonshot/jwt/JwtTokenProvider.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
package org.moonshot.jwt;

import static org.moonshot.response.ErrorType.DISCORD_LOG_APPENDER;
import static org.moonshot.response.ErrorType.EXPIRED_TOKEN;
import static org.moonshot.response.ErrorType.INVALID_REFRESH_TOKEN;
import static org.moonshot.response.ErrorType.UNKNOWN_TOKEN;
import static org.moonshot.response.ErrorType.UNSUPPORTED_TOKEN;
import static org.moonshot.response.ErrorType.WRONG_SIGNATURE_TOKEN;
import static org.moonshot.response.ErrorType.WRONG_TYPE_TOKEN;
import static org.moonshot.response.ErrorType.*;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import jakarta.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.crypto.SecretKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.moonshot.constants.JWTConstants;
import org.moonshot.exception.InternalServerException;
import org.moonshot.exception.UnauthorizedException;
import org.moonshot.security.UserAuthentication;
import org.moonshot.security.service.UserPrincipalDetailsService;
import org.moonshot.user.model.UserPrincipal;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Component
Expand Down Expand Up @@ -137,14 +132,9 @@ public JwtValidationType validateToken(String token) {
}

public void deleteRefreshToken(Long userId) {
if (redisTemplate.hasKey(String.valueOf(userId))) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
String refreshToken = valueOperations.get(String.valueOf(userId));
redisTemplate.delete(refreshToken);
} else {
throw new InternalServerException(DISCORD_LOG_APPENDER);
}
redisTemplate.delete(String.valueOf(userId));
}

private Claims getBody(final String token) {
return Jwts.parserBuilder()
.setSigningKey(getSigningKey())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.moonshot.security.service;

import lombok.RequiredArgsConstructor;
import org.moonshot.exception.UnauthorizedException;
import org.moonshot.user.model.UserPrincipal;
import org.moonshot.user.repository.UserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class UserPrincipalDetailsService implements UserDetailsService {
Expand All @@ -17,9 +17,7 @@ public class UserPrincipalDetailsService implements UserDetailsService {

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return userRepository.findByIdWithCache(Long.parseLong(username))
.map(UserPrincipal::new)
.orElseThrow(UnauthorizedException::new);
return new UserPrincipal(Long.parseLong(username));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

public class UserPrincipal implements UserDetails {

private final User user;
private final Long userId;
private final List<GrantedAuthority> grantedAuthorities;

public UserPrincipal(User user) {
this.user = user;
this.grantedAuthorities = user.getId() == null ?
public UserPrincipal(Long userId) {
this.userId = userId;
this.grantedAuthorities = this.userId == null ?
List.of(new SimpleGrantedAuthority("ANONYMOUS")) :
List.of(new SimpleGrantedAuthority("USER"));
}
Expand All @@ -30,7 +30,7 @@ public String getPassword() {

@Override
public String getUsername() {
return user.getName();
return String.valueOf(userId);
}

@Override
Expand All @@ -54,7 +54,7 @@ public boolean isEnabled() {
}

public Long getUserId() {
return user.getId();
return this.userId;
}

}

0 comments on commit 5459509

Please sign in to comment.