Skip to content

Commit

Permalink
Merge pull request #251 from Yanabada/develop
Browse files Browse the repository at this point in the history
Security AuthFilter에서 MemberRepository 의존성 제거
  • Loading branch information
tjdtn0219 authored Jan 31, 2024
2 parents 2147991 + 90726bd commit 3cfb0d5
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 23 deletions.
Binary file modified README.md
Binary file not shown.
Binary file added image/Yanabada-ERD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/데일리 스크럼.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/백엔드 인프라 아키텍처.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/소개이미지.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/파이널 api 명세서.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/프로젝트 파이프라인.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@
import kr.co.fastcampus.yanabada.common.exception.MemberNotFoundException;
import kr.co.fastcampus.yanabada.common.exception.TokenNotExistAtCacheException;
import kr.co.fastcampus.yanabada.common.exception.TokenNotValidatedException;
import kr.co.fastcampus.yanabada.common.jwt.service.TokenService;
import kr.co.fastcampus.yanabada.common.jwt.util.JwtProvider;
import kr.co.fastcampus.yanabada.common.security.PrincipalDetails;
import kr.co.fastcampus.yanabada.domain.member.entity.Member;
import kr.co.fastcampus.yanabada.domain.member.entity.ProviderType;
import kr.co.fastcampus.yanabada.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
Expand All @@ -32,8 +25,6 @@
public class JwtAuthFilter extends OncePerRequestFilter {

private final JwtProvider jwtProvider;
private final MemberRepository memberRepository;
private final TokenService tokenService;

@Override
protected void doFilterInternal(
Expand All @@ -51,30 +42,20 @@ protected void doFilterInternal(
String email = jwtProvider.getEmail(token);
String provider = jwtProvider.getProvider(token);

if (!tokenService.isExistToken(email, provider)) {
if (jwtProvider.isLoggedOut(email, provider)) {
/* 로그아웃 된 토큰 사용 */
throw new TokenNotExistAtCacheException();
}

try {
Member findMember = memberRepository
.getMember(email, ProviderType.valueOf(provider));
PrincipalDetails principalDetails = PrincipalDetails.of(findMember);

// SecurityContext에 인증 객체를 등록
Authentication auth = getAuthentication(principalDetails);
SecurityContextHolder.getContext().setAuthentication(auth);
jwtProvider
.saveAuthInContextHolder(email, ProviderType.valueOf(provider));
} catch (MemberNotFoundException e) {
throw new TokenNotValidatedException();
}
filterChain.doFilter(request, response);
}

public Authentication getAuthentication(PrincipalDetails principal) {
return new UsernamePasswordAuthenticationToken(
principal, "", principal.getAuthorities()
);
}

private String extractTokenFromRequest(HttpServletRequest request) {
String token = request.getHeader(AUTHORIZATION_HEADER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
import java.util.Date;
import kr.co.fastcampus.yanabada.common.exception.ClaimParseFailedException;
import kr.co.fastcampus.yanabada.common.exception.TokenExpiredException;
import kr.co.fastcampus.yanabada.common.jwt.constant.JwtConstant;
import kr.co.fastcampus.yanabada.common.jwt.dto.TokenIssueResponse;
import kr.co.fastcampus.yanabada.common.jwt.service.TokenService;
import kr.co.fastcampus.yanabada.common.security.PrincipalDetails;
import kr.co.fastcampus.yanabada.domain.member.entity.Member;
import kr.co.fastcampus.yanabada.domain.member.entity.ProviderType;
import kr.co.fastcampus.yanabada.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Slf4j
Expand All @@ -27,6 +33,7 @@
public class JwtProvider {

private final TokenService tokenService;
private final MemberRepository memberRepository;

@Value("${jwt.secretKey}")
private String secretKeyPlain;
Expand Down Expand Up @@ -100,4 +107,26 @@ private Claims parseClaims(String accessToken) {
throw new ClaimParseFailedException();
}
}

public boolean isLoggedOut(String email, String provider) {
return !tokenService.isExistToken(email, provider);
}

public void saveAuthInContextHolder(
String email, ProviderType providerType
) {
Member findMember = memberRepository
.getMember(email, providerType);
PrincipalDetails principalDetails = PrincipalDetails.of(findMember);

// SecurityContext에 인증 객체를 등록
Authentication auth = getAuthentication(principalDetails);
SecurityContextHolder.getContext().setAuthentication(auth);
}

private Authentication getAuthentication(PrincipalDetails principal) {
return new UsernamePasswordAuthenticationToken(
principal, "", principal.getAuthorities()
);
}
}

0 comments on commit 3cfb0d5

Please sign in to comment.