사용자 정보 가지고 오기

Seung jun Cha·2023년 2월 24일
0
  1. @AuthenticationPrincipal
    Authentication 객체 속에 들어있는 principal 객체를 가져와서 주입한다. 엔티티를 직접 가지고 오지 말고 어댑터 클래스를 따로 만들어서 가지고 오는 것이 좋다
  2. @EnableGlobalMethodSecurity(다양한 옵션)
    2-1 PrePostEnabled의 속성은 Spring Security 사전 / 사후 어노테이션을 수 있다.
    2-2 securedEnabled 경우 생성 속성이 결정 @Secured 어노테이션을 사용가능하도록 한다.
    2-3 @Secured 어노테이션은하는 컨트롤러에서 역할 List을 지정하는 데 사용됩니다. 따라서 사용자는 지정된 역할 중 하나 이상이 있는 경우에만 해당 방법에 액세스할 수 있다. config 파일에서 권한을 설정하는 것이 아니라 controller에서 권한 설정이 가능하며 2개 이상의 권한 설정도 가능하다.
antMatchers("/api/v1/test/auth").hasRole("AUTH")

@Secured("ROLE_AUTH")
    @GetMapping("/auth")
    public Object getTest2(@AuthenticationPrincipal SecurityUser securityUser) 
    		                                                  throws Exception 
  1. UserDetails : Spring Security에서 사용자의 정보를 담는 인터페이스이다. 기본적으로 구현해야하는 메서드이다.
    UserDetails를 상속한 클래스는 사용자 정보와 권한 정보를 가지게 된다

  2. UserDetailsService : DB에서 유저 정보를 가져오는 인터페이스파라미터로 username(유저를 식별하는 수 있는 ID, 꼭 username일 필요는 없다)을 받고 리턴값으로 UserDetails를 돌려준다.

  • 사용자가 입력한 아이디와 비밀번호를 가지고 있는 UsernamePasswordAuthenticationToken 객체 생성

AuthenticationManager는 ProviderManager에게 인증을 위임(일반적으로 DaoAuthenticationProvider에게 위임)

DaoAuthenticationProvider는 데이터베이스를 사용하여 사용자 정보를 조회하여 인증을 처리합니다.

  1. 사용자가 아이디와 비밀번호 입력

  2. 클라이언트가 전송한 인증 정보는 UsernamePasswordAuthenticationToken 객체로 생성(UsernamePasswordAuthenticationToken는 Authentication인터페이스를 구현한 객체이다)

  3. UsernamePasswordAuthenticationToken 객체는 AuthenticationManager에 전달됨

  4. AuthenticationManager는 ProviderManager에게 인증을 위임

  5. ProviderManager가 UserDetailsService 인터페이스를 구현한 클래스의 loadUserByUsername 메서드를 사용해서 사용자이름(아이디)를 기반으로 사용자의 정보를 데이터베이스에서 가져옵니다. 여기선 아이디만 가져가서 사용자 정보를 가져오고
    이렇게 가져온 사용자 정보에는 아이디, 비밀번호 등이 있습니다. 이걸 UserDetails 객체로 만들어서 반환합니다.

  6. AuthenticationProvider는 UserDetails 객체와 클라이언트가 제공한 비밀번호를 비교하여 사용자를 인증. 암호화 되었다면 일반적으로 PasswordEncoder를 사용해서 비교(UserDetails 정보와 UsernamePasswordAuthenticationToken 정보를 비교)

  7. 인증에 성공하면 AuthenticationProvider는 인증된 사용자를 나타내는 Authentication 객체를 생성합니다. (인증된 사용자의 정보가 포함되어 있습니다.)

  8. 이 정보는 Spring Security의 SecurityContextHolder에 설정됩니다.

DaoAuthenticationProvider는 loadUserByUsername 메서드에서 반환된 UserDetails 객체와 클라이언트가 제공한 비밀번호를 비교하여 사용자를 인증
=> 클라이언트가 제공한 비밀번호가 UserDetails에 있는 비밀번호와 일치하면 인증이 성공합니다.

AuthenticationProvider는 인증된 사용자를 나타내는 Authentication 객체를 생성합니다. ( 인증된 사용자의 정보가 포함되어 있습니다.)

이 정보는 Spring Security의 SecurityContextHolder에 설정됩니다.

  1. grantedAuthorities : Spring Security에서 권한을 나타내는 GrantedAuthority 객체들의 컬렉션
 public static final String AUTHORITIES_DELIMITER = "<?>";
 
private static String toString(final Collection<? extends GrantedAuthority> grantedAuthorities) { 
	return grantedAuthorities.stream()
                .map(GrantedAuthority::getAuthority) 
         .collect(Collectors.joining(AUTHORITIES_DELIMITER));
    }
    
 만약 권한이 ROLE_USER , ROLE_ADMIN 이렇게 있다면 
 "ROLE_USER<?>ROLE_ADMIN" 이 문자열로 반환된다.

3번에 있는 UserDetails.getAuthorities()메서드를 사용해서 권한을 가져와도 되지만 리턴값은 객체이다.

 // 권한 가져오기
Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();

        // 각 권한 출력
        for (GrantedAuthority authority : authorities) {
            System.out.println(authority.getAuthority());
        }

권한은 문자열로 바꿔서 반환하는 것이 일반적이다.

0개의 댓글