Skip to content

Commit

Permalink
Merge pull request #301 from MOONSHOT-Team/develop
Browse files Browse the repository at this point in the history
[Deploy] v1.1.3 운영서버 배포
  • Loading branch information
0lynny authored Jul 29, 2024
2 parents 7c5eb3c + bac53da commit c5f2a5f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 62 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.findById(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.findById(userId)
.orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));
validateUserAuthorization(user.getId(), userId);

if (hasChange(request.nickname())) {
user.modifyNickname(request.nickname());
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));
}

}

This file was deleted.

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.moonshot.constants.DiscordConstants;
import org.moonshot.discord.model.EmbedObject;
import org.moonshot.exception.InternalServerException;
import org.moonshot.util.MDCUtil;
import org.moonshot.util.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Slf4j
@Setter
@Component
@Profile("!local")
@RequiredArgsConstructor
public class DiscordAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {

@Value("${logging.discord.signin.webhook-uri}")
private String signInWebhookUrl;

private String discordWebhookUrl;
private String username;
private String avatarUrl;
Expand Down Expand Up @@ -119,7 +124,7 @@ protected void append(ILoggingEvent eventObject) {
}

public void signInAppend(Long totalUserCount, String name, String email, String socialPlatform, LocalDateTime createdAt, String imgUrl){
DiscordWebHook discordWebhook = new DiscordWebHook(DiscordConstants.signInWebhookUrl, username, avatarUrl, false);
DiscordWebHook discordWebhook = new DiscordWebHook(signInWebhookUrl, username, avatarUrl, false);

discordWebhook.addEmbed(new EmbedObject()
.setTitle("🚀[회원 가입] " + totalUserCount + "번째 유저가 가입하였습니다.🚀")
Expand All @@ -133,7 +138,7 @@ public void signInAppend(Long totalUserCount, String name, String email, String
);

try {
discordWebhook.executeSignIn(DiscordConstants.signInWebhookUrl);
discordWebhook.executeSignIn(signInWebhookUrl);
} catch (IOException ioException) {
throw new InternalServerException(DISCORD_LOG_APPENDER);
}
Expand Down

0 comments on commit c5f2a5f

Please sign in to comment.