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

7주차 과제 - Spring Security 인가(Authorization) 구현하기 #74

Merged
merged 32 commits into from Sep 25, 2022

Conversation

jdalma
Copy link

@jdalma jdalma commented Sep 19, 2022

TODO

  1. 사용자 정보는 자신의 것만 수정 가능하게
  2. 사용자 수정,삭제는 관리자만 가능하게
  3. 상품 정보는 자신의 것만 수정 가능하게
  4. 상품 수정,삭제는 관리자만 가능하게
  5. userId에 따라서 권한을 따로 부여

로그인이 필요없는 API

  • 로그인 - POST /session
  • 회원 생성하기 - POST /users
  • 고양이 장난감 목록 얻기 - GET /products
  • 고양이 장난감 상세 조회하기 - GET /products/{id}

로그인이 필요한 API

  • 고양이 장난감 등록하기 - POST /products
  • 고양이 장난감 수정하기 - PATCH /products/{id}
  • 고양이 장난감 삭제하기 - DELETE /products/{id}
  • 회원 수정하기 - POST /users/{id}
  • 회원 삭제하기 - DELETE /users/{id}

스프링 시큐리티를 처음 접해봐서 중요한 포인트나 키워드를 던져주신다면 열심히 조사해보겠습니다!
한 주간 잘 부탁드립니다!!

Copy link
Collaborator

@johngrib johngrib left a comment

Choose a reason for hiding this comment

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

오래간만입니다!

Copy link
Collaborator

@johngrib johngrib left a comment

Choose a reason for hiding this comment

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

잘 해나가고 계신 것 같습니다. 화이팅!

import java.io.IOException;
import java.util.Set;

public class JwtAuthenticationFilter extends BasicAuthenticationFilter {
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 클래스의 용도와 기능을 JavaDoc으로 작성해 주세요.

Copy link
Author

Choose a reason for hiding this comment

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

d803878 이렇게 작성해 봤습니다 ㅎㅎ

현재 과제에서는 Bearer scheme과 JWT를 사용하고 있는데 BasicAuthenticationFilter클래스를 확장하고 있습니다

요약하면 이 필터는 인증 체계가 Basic 이고 Base64로 인코딩된 username:password 토큰이 있는 
Authorization 의 HTTP 요청 헤더가 있는 모든 요청을 처리합니다.

BasicAuthenticationFilter클래스를 확장하기 보단 다른 필터를 찾아 확장하거나 구현하는게 맞는걸까요??
이해하기로는 Authorization scheme은 인증 체계를 알려주기만 하는거라고 이해했습니다 ㅎㅎ

Copy link
Collaborator

Choose a reason for hiding this comment

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

설명이 필요 이상으로 자세한 것 같아요. 이 클래스의 "책임"만 간단하게 작성해 보세요.


private static List<GrantedAuthority> authorities() {
List<GrantedAuthority> authorities = new ArrayList<>();
// TODO : userId에 따라서 권한을 따로 부여
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 TODO는 언제 실행할 예정인가요? 아니면 이미 끝난 작업인가요?

Copy link
Author

@jdalma jdalma Sep 22, 2022

Choose a reason for hiding this comment

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

어떻게 적용해야 할지 몰라서 스프링 문서 뒤져가면서 찾고 있었습니다!
계속 작업 중 입니다 !! 9e54f15

제가 생각하는 흐름은

  1. userId -1을 관리자로 지정
  2. User 엔티티에 enum role 필드 추가
  3. 사용자 생성 시 기본 사용자는 USER로 저장
  4. JWT 페이로드에 userIdrole 정보를 같이 인코딩
  5. 사용자 또는 상품 도메인 접근 시 JWT에 담겨 있는 role정보를 확인해서 인가가 가능한지 구분

일단 이렇게 생각하고 3번까지 작업된 것 같습니다
시큐리티 설정에 문제가 있는지 401이 반환돼서 확인 작업 중에 있습니다!!

Comment on lines 49 to 52
@Nested
@DisplayName("식별자에 해당하는 사용자가 존재하고 인증을 통과한다면")
class Context_ExistedIdAndValidToken{

Copy link
Collaborator

Choose a reason for hiding this comment

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

사용자가 존재하는 상황을 Context_ExistedIdAndValidToken 에서 명시해 주세요. 이 컨텍스트가 표현하고자 하는 것을 이 컨텍스트에서 코딩하는 것입니다.

Copy link
Author

Choose a reason for hiding this comment

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

Describe_Context_It을 무의식적으로 작성한 것 같네요 ㅠㅠ
문맥별로 더 쪼개보니 반대 상황이 더 잘보이는 것 같습니다 ㅎㅎ
ef37c86

Copy link
Collaborator

@johngrib johngrib left a comment

Choose a reason for hiding this comment

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

테스트가 한결 보기 좋습니다. 그러고보니 벌써 금요일이군요.

import java.io.IOException;
import java.util.Set;

public class JwtAuthenticationFilter extends BasicAuthenticationFilter {
Copy link
Collaborator

Choose a reason for hiding this comment

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

설명이 필요 이상으로 자세한 것 같아요. 이 클래스의 "책임"만 간단하게 작성해 보세요.

@jdalma
Copy link
Author

jdalma commented Sep 25, 2022

이번 주 과제는 조금 아쉽게 끝나는 것 같네요 ㅠ
스프링 시큐리티를 처음 사용해서 많이 헤맸네요 ㅎㅎ
그리고 종립님이 모킹을 안 쓰는 테스트 코드에 적응을 먼저 하는게 좋다 라고 말씀하신 게 이번에 와닿았습니다
격리된 테스트 코드를 짜는게 쉽지가 않네요 ㅠㅠ
그래도 더 공부해야할 키워드들도 많이 나왔고 많이 배웠습니다!!
한 주간 감사했습니다!!

회고

Copy link
Collaborator

@johngrib johngrib left a comment

Choose a reason for hiding this comment

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

한 주간 고생 많으셨습니다!

public User updateUser(Long id, UserModificationData modificationData) {
public User updateUser(Long id, UserModificationData modificationData , Long userId) throws AccessDeniedException {
if(!Objects.equals(id, userId)){
throw new AccessDeniedException("자원 접근이 거부되었습니다.");
Copy link
Collaborator

Choose a reason for hiding this comment

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

만약 여러 나라의 언어로 서비스를 한다면 어떻게 대응하면 좋을지에 대해서 생각해 보세요.

class Context_ValidToken{

@Nested
@DisplayName("사용자 권한이라면")
Copy link
Collaborator

Choose a reason for hiding this comment

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

관리자 권한이 아닌 경우로 표현할 수도 있겠습니다.

@johngrib johngrib merged commit b488d15 into CodeSoom:jdalma Sep 25, 2022
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

2 participants