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

GTB-46 [feat] 사용자 인증 로직 변경 #46

Merged
merged 26 commits into from
Jul 8, 2024
Merged

GTB-46 [feat] 사용자 인증 로직 변경 #46

merged 26 commits into from
Jul 8, 2024

Conversation

jwnnoh
Copy link
Member

@jwnnoh jwnnoh commented Jul 8, 2024

1. 무슨 이유로 코드를 변경했나요?

  • AuthenticationPrincipal을 통한 인증 로직을 구현하기 위해 코드를 변경하였습니다.
  • 또한, 리프레시토큰을 통한 액세스 토큰의 재발급 로직을 구현하기 위함입니다.

2. 어떤 위험이나 장애를 발견했나요?

  • 특이사항 없습니다.

3. 관련 스크린샷을 첨부해주세요.

  • 유저 액세스 토큰 재발급
스크린샷 2024-07-08 오후 8 59 43
  • 관리자 액세스 토큰 재발급
스크린샷 2024-07-08 오후 8 59 02

4. 완료 사항


5. 추가 사항

인증 플로우

AuthenticationPrincipal

  • AuthenticationPrincipal을 사용하기 위한 어노테이션입니다.
  • 쉽게 말해, 로그인한 사용자의 커스텀 객체를 손쉽게 가져올 수 있는 방법입니다!
  • 해당 어노테이션의 동작 과정은 구현체인 'AuthenticationPrincipalArgumentResolver'에서 확인할 수 있습니다.

image

  1. SecurityContextHolder로부터 Authentication 객체를 가져오고, 해당 객체에서 Object principal 객체를 가져옵니다.
  2. @AuthenticationPrincipal 어노테이션이 적용된 파라미터를 확인합니다.
  3. 해당 부분은 AuthenticationPrincipal 어노테이션의 속성 값에 따른 처리 부분으로 default false인 상태에서는 적용되지 않습니다.
  4. principal 인스턴스를 반환합니다.

CustonUserDetails

UserDetails를 상속하여 만든 커스텀 로그인 객체입니다.

  • 엔티티와 시큐리티의 정보 제공 책임을 분리하고자, 해당 프로젝트 내에서는 별도의 커스텀 로그인 객체를 생성하였습니다.
  • 해당 클래스 내부에는 시스템 로직에 필요한 값들을 선언하였습니다!
@Getter
@AllArgsConstructor
public class AuthDetails implements UserDetails {
    private final UUID uuid;
    private final String username;
    private final Role role;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singletonList(new SimpleGrantedAuthority(role.getRole()));
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}

CustomUserDetailsService

Admin, User 각각의 커스텀 로그인 객체를 반환하기 위해 두 개의 클래스로 구현했습니다.
loadUserByUsername() 메서드를 통해 커스텀 로그인 객체를 반환합니다.

    @Override
    public AuthDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = adminRepository.findByUsername(username)
                .orElseThrow(AdminNotFoundException::new);
        return new AuthDetails(admin.getAdminId(), admin.getUsername(), Role.ROLE_ADMIN);
    }

자세한 내용은 https://wildeveloperetrain.tistory.com/324 를 참고해주시면 더욱 좋을 듯 합니다!
감사합니다 😄

jwnnoh and others added 26 commits July 8, 2024 18:27
[GTB-46] chore: 미사용 import문 제거
@jwnnoh jwnnoh self-assigned this Jul 8, 2024
@yechan-kim
Copy link
Contributor

LGTM!

Copy link
Contributor

@rootTiket rootTiket left a comment

Choose a reason for hiding this comment

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

코드 자체에서 수정해야할 점이 발견되지는 못했습니다!
괜찮으시다면 어떤 플로우로 인증이 이루어지고, 인증이 필요한 기능의 경우 어떤 식으로 사용해야하는지 pr에 남겨주시면 더더더더 감사할거 같습니다.
많은 수정을 요했을텐데 구현하시느라 고생하셨습니다 :)

.orElseThrow(AdminNotFoundException::new)
.getPub();
return ResponseEntity.ok(getWaitings.excute(pub));
public ResponseEntity<PubWaitingListResponse> getWaiting(@AuthenticationPrincipal AuthDetails authDetails) {
Copy link
Contributor

Choose a reason for hiding this comment

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

해당 어노테이션에 대한 설명이 가능할까요! 처음 보는 어노테이션 이네요

Copy link
Member Author

Choose a reason for hiding this comment

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

해당 어노테이션은 시큐리티가 저장하고 있는 Authentication 객체를 참조하는 방법 중 하나입니다!
쉽게 말해, AuthDetails(통상적으로는 UserDetails)에 접근할 수 있는 어노테이션인 것이죠.
이를 통해 로그인한 사용자의 정보를 쉽게 가져올 수 있습니다!

Copy link
Contributor

Choose a reason for hiding this comment

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

배워갑니다 !! 고생하셨습니다

@jwnnoh jwnnoh merged commit 3a2d251 into develop Jul 8, 2024
@jwnnoh jwnnoh deleted the GTB-46 branch July 8, 2024 15:42
@jwnnoh jwnnoh added the ✨ feature 새로운 기능 label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ feature 새로운 기능
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GTB-46 [feat] 사용자 인증 로직 변경
3 participants