Skip to content

Commit

Permalink
feat: SecurityUtil CurrentMember method (#11)
Browse files Browse the repository at this point in the history
현재 사용자의 정보를 가져오는 SecurityUtil, currentMember method 생성
  • Loading branch information
toychip committed Dec 14, 2023
1 parent f614881 commit 38747b4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public enum ErrorType {


// ------------------------------------------ USER ------------------------------------------
_USER_NOT_FOUND(NOT_FOUND, "USER_4040", "제공된 토큰으로 사용자를 찾을 수 없습니다.")

_USER_NOT_FOUND_BY_TOKEN(NOT_FOUND, "USER_4040", "제공된 토큰으로 사용자를 찾을 수 없습니다."),
_UNAUTHORIZED(UNAUTHORIZED, "USER_4010", "로그인되지 않은 상태입니다."),
_USER_NOT_FOUND_DB(NOT_FOUND, "USER_4041", "존재하지 않는 회원입니다.")
;

private final HttpStatus status;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/api/TaveShot/global/jwt/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static com.api.TaveShot.global.constant.OauthConstant.ACCESS_TOKEN_VALID_TIME;
import static com.api.TaveShot.global.exception.ErrorType._JWT_EXPIRED;
import static com.api.TaveShot.global.exception.ErrorType._JWT_PARSING_ERROR;
import static com.api.TaveShot.global.exception.ErrorType._USER_NOT_FOUND;
import static com.api.TaveShot.global.exception.ErrorType._USER_NOT_FOUND_BY_TOKEN;

import com.api.TaveShot.domain.Member.repository.MemberRepository;
import com.api.TaveShot.global.exception.ApiException;
Expand Down Expand Up @@ -87,7 +87,7 @@ public void getAuthenticationFromToken(final String jwtToken) {
private void getGitLoginId(final String jwtToken) {
Long userId = Long.valueOf(getUserIdFromToken(jwtToken));
memberRepository.findById(userId)
.orElseThrow(() -> new ApiException(_USER_NOT_FOUND));
.orElseThrow(() -> new ApiException(_USER_NOT_FOUND_BY_TOKEN));
}

// 토큰에서 유저 아이디 얻기
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public Collection<? extends GrantedAuthority> getAuthorities() {
public String getName() {
return member.getGitLoginId();
}

public Member getMember() {
return this.member;
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/api/TaveShot/global/util/SecurityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.api.TaveShot.global.util;

import com.api.TaveShot.domain.Member.domain.Member;
import com.api.TaveShot.global.exception.ApiException;
import com.api.TaveShot.global.exception.ErrorType;
import com.api.TaveShot.global.security.oauth2.CustomOauth2User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {

public static Member getCurrentMember() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication == null || !authentication.isAuthenticated()) {
throw new ApiException(ErrorType._UNAUTHORIZED);
}

Object principal = authentication.getPrincipal();

if (principal instanceof CustomOauth2User) {
return ((CustomOauth2User) principal).getMember();
}

throw new ApiException(ErrorType._USER_NOT_FOUND_DB);
}
}

0 comments on commit 38747b4

Please sign in to comment.