Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spring 지하철 경로 조회 - 1,2단계] 다니(이다은) 미션 제출합니다. #57

Merged
merged 25 commits into from
May 17, 2021

Conversation

da-nyee
Copy link

@da-nyee da-nyee commented May 12, 2021

안녕하세요, 게이츠! 다니입니다 🙌

페어 @perenok 와 함께 이번 미션 열심히 진행했습니다 !

인증, 인가에 대한 개념이 부족해서 테코톡과 학습 테스트로 먼저 공부하고 미션을 수행했어요 !
JWT, Vue.js도 처음이어서 초반에 많이 헤맸지만 페어 덕분에 잘 완료할 수 있었습니다 ~!

코드에서 부족한 부분 마구마구 지적해주세요!!!
피드백을 통해서 많이 성장하고 싶습니다 ㅎㅎㅎ

귀한 시간 내주셔서 감사합니다 🙇‍♀️❣️

Copy link

@serverwizard serverwizard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 😄 이번 미션을 함께 할 게이츠입니다.

구현 잘해주셨어요. 몇가지 코멘트 남겨 놓았으니 확인 부탁드려요
앞으로 과정 진행하면서 궁금한점 있으면 편하게 코멘트 또는 DM 남겨주세요.

다음 코드 기대하며 기다리고 있을게요. 🙇

return null;
// 유효한 로그인인 경우 LoginMember 만들어서 응답하기
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
String token = AuthorizationExtractor.extract(request);
Copy link

@serverwizard serverwizard May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중요한 건 아니지만, token 지역변수를 아래에서 한 번만 사용하기 때문에 굳이 선언해서 사용할 필요는 없을 것 같아요~

추가로, 이곳에서도 유효한 token인지 체크를 해주면 어떨까요?

 jwtTokenProvider.validateToken(token);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 의견에서 이어져서 Interceptor에서 validateToken을 하면 Resolver에서는 validateToken을 안 해도 되지 않을까요...?!
게이츠의 의견이 궁금합니다 !

import wooteco.subway.auth.infrastructure.JwtTokenProvider;
import wooteco.subway.exception.InvalidMemberException;
import wooteco.subway.member.dao.MemberDao;
import wooteco.subway.member.domain.Member;

@Service
public class AuthService {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서비스 layer를 사용한다면 트랜잭션도 적용해보면 좋을 것 같아요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

질문이 하나 있는데요!
AuthService/getPayload()는 DAO를 사용하지 않으므로 클래스 단위가 아닌 메소드 단위에 @Transactional을 적용했습니다.
이렇게 일부 메소드에서만 DAO를 사용하면 지금처럼 메소드 단위로 어노테이션을 붙이면 되는 건가요??
아니면 이 경우에도 클래스 단위로 어노테이션을 붙이면 될까요?!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실 말씀하신 것처럼 하셔도 되요~ ^^
다만, 메소드 단위로 @ Transactional을 걸다보면 누락할 위험이 있지 않을까해서 말씀드렸어요!

HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
String token = AuthorizationExtractor.extract(request);
Member member = authService.findMemberByToken(token);
return new LoginMember(member.getId(), member.getEmail());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/member/me");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 해당 인터셉터가 적용되고 있는 걸까요? (/api/member/me)

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = AuthorizationExtractor.extract(request);
jwtTokenProvider.validateToken(token);
return super.preHandle(request, response, handler);
Copy link

@serverwizard serverwizard May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 LoginInterceptor는 /members/me URI에만 적용되고 있는 것 같은데요.
그런데 해당 컨트롤러 메소드는 @AuthenticationPrincipal LoginMember loginMember를 인자를 받고 있기 때문에, AuthenticationPrincipalArgumentResolver에서 validateToken을 처리하게 된다면, 해당 Interceptor가 필요없지 않을까요?

해당 인터셉터를 무조건 제거해야 돼라기 보다, 다니의 의견을 물어보는 거니 편히 답변 부탁드려요 ^^

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요청이 들어오면 Interceptor에서 먼저 validateToken을 하고,
유효하면 Resolver로 넘기고 아니면 예외를 발생하면 되겠다 생각해서 Interceptor에서 validateToken을 하도록 구현했습니다!
만약 Interceptor가 아닌 Resolver에서 토큰 유효성 검증을 하면 미리 잡을 수 있는 예외를 한 단계 더 뒤에서 잡아야 할 거 같아요.
그런데, Interceptor가 없으면 Resolver만 있게 되니까 게이츠의 의견도 맞는 것 같습니다 !
한편으로는 /api/members/me 외에 다른 URI의 권한도 확인해야 하면 Interceptor에서 토큰 검증을 하는 게 필요할 거 같습니다 !
지금 요구사항 내에서는 Resolver만 사용해도 괜찮을까요?!

@da-nyee
Copy link
Author

da-nyee commented May 14, 2021

안녕하세요, 게이츠! 다니입니다 🙌

빠른 피드백 감사합니다 👍 참고해서 리팩토링 진행했습니다 !

리팩토링 하다가 궁금해진 점들은 피드백 아래에 👀 이모지와 함께 코멘트로 남겨뒀습니다!
확인하고 답변해주시면 감사하겠습니다 ~!

이번에도 코드 리뷰 기다리고 있겠습니다!
귀한 시간 내주셔서 감사합니다 🙇‍♀️❣️

@da-nyee
Copy link
Author

da-nyee commented May 16, 2021

📚 다니의 학습 로그

[Spring] HandlerMethodArgumentResolver - 4

내용

태그

{Spring}, {HandlerMethodArgumentResolver}, {interface}, {annotation}

Copy link

@serverwizard serverwizard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 다니

미션 잘 구현해 주셨네요 👍
그래서 이번 단계는 여기서 머지하도록 할게요. 😉

궁금한건 언제든 질문 주세요!

import wooteco.subway.auth.infrastructure.JwtTokenProvider;
import wooteco.subway.exception.InvalidMemberException;
import wooteco.subway.member.dao.MemberDao;
import wooteco.subway.member.domain.Member;

@Service
public class AuthService {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실 말씀하신 것처럼 하셔도 되요~ ^^
다만, 메소드 단위로 @ Transactional을 걸다보면 누락할 위험이 있지 않을까해서 말씀드렸어요!

@serverwizard serverwizard merged commit 215556d into woowacourse:da-nyee May 17, 2021
@da-nyee
Copy link
Author

da-nyee commented Jun 22, 2021

📚 다니의 학습 로그

[Spring] Interceptor - 4

내용

Interceptor?

  • Dispatcher Servlet과 Controller 사이에서 HttpServletRequest, HttpServletResponse를 가로채는 역할을 한다.
  • HandlerInterceptor 인터페이스
    • preHandle(): Controller 실행 전 호출 / View 생성되지 않은 상태 / boolean 반환
    • postHandle(): Controller 실행 후 호출 / View 생성되지 않은 상태 / void 반환
    • afterCompletion(): 모든 request 처리 완료 후 호출 / View 생성된 상태 / void 반환

Configuration

  • WebMvcConfigurer 인터페이스를 구현한 클래스에서 addInterceptors()를 오버라이딩해서 Interceptor를 사용한다.
    • addInterceptors(): Interceptor를 등록할 URI 경로를 지정한다.
    • excludeInterceptors(): Interceptor를 제외할 URI 경로를 지정한다.
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(subwayInterceptor)
            .addPathPatterns(
                    "/api/members/me/**",
                    "/api/stations/**",
                    "/api/lines/**",
                    "/api/paths"
            );
}

링크

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants