ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [스프링] 시큐리티 -5 (Authentication Manager)
    Spring 2020. 10. 10. 18:46

    - 참고 : 인프런 >>스프링 시큐리티 (백기선 강의)

     

    Authentication Manager

    • 사용자 아이디 / 비밀번호 인증을 처리하는 곳  -> 유효한 인증인지 확인
    • 인자로 받은 Authentication이 유효한 인증인지 확인하고,  "Authentication" 객체를 리턴

     

     

    유저의 요청 내에 담긴 "Authentication"Authentication Manager 에 넘겨주고, AuthenticationProvider에서 인증을 처리한다.

     

    -> Authentication Manager인자로 받은 Authentication Provider을 통해, 유효한지 처리하여, "Authentication" 객체를 리턴한다.

     

     

     

    * Authentication 

    • 인자로 받은 필요한 정보 (username, password , jwt 등..)로 만든 객체

     

     

     

    Authentication 객체 리턴

    • Principal : UserDetailsService에서 리턴한 객체(User)
    • Credentials
    • GrantedAuthorities : 권한

     

     

     

    [AuthenticationManager.java]

    public interface AuthenticationManager {
        Authentication authenticate(Authentication var1) throws AuthenticationException;
    }
    

     

    [AuthenticationProvider.java]

    public interface AuthenticationProvider {
        Authentication authenticate(Authentication var1) throws AuthenticationException;
    
        boolean supports(Class<?> var1);
    }
    

     

    AuthenticationProvider

    public interface AuthenticationProvider {
        Authentication authenticate(Authentication var1) throws AuthenticationException;
    
        boolean supports(Class<?> var1);
    }
    

     

    [ProviderManager.java] 

    public class ProviderManager implements AuthenticationManager, MessageSourceAware, InitializingBean {
        private static final Log logger = LogFactory.getLog(ProviderManager.class);
        private AuthenticationEventPublisher eventPublisher;
        private List<AuthenticationProvider> providers;
        protected MessageSourceAccessor messages;
        private AuthenticationManager parent;
        private boolean eraseCredentialsAfterAuthentication;
        
        
        //...
        
        
         //인자로 받은 유저의 정보 "Authentification"이 유효한지 확인
         public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            Class<? extends Authentication> toTest = authentication.getClass();
            AuthenticationException lastException = null;
            AuthenticationException parentException = null;
            Authentication result = null;
            Authentication parentResult = null;
            boolean debug = logger.isDebugEnabled();
            Iterator var8 = this.getProviders().iterator();
    
            while(var8.hasNext()) {
                AuthenticationProvider provider = (AuthenticationProvider)var8.next();
                if (provider.supports(toTest)) {
                    if (debug) {
                        logger.debug("Authentication attempt using " + provider.getClass().getName());
                    }
    
                    try {
                        result = provider.authenticate(authentication);
                        if (result != null) {
                            this.copyDetails(authentication, result);
                            break;
                        }
                    } catch (InternalAuthenticationServiceException | AccountStatusException var13) {
                        this.prepareException(var13, authentication);
                        throw var13;
                    } catch (AuthenticationException var14) {
                        lastException = var14;
                    }
                }
            }
            
            //...
            
            //...
            
            
            }
           
      }

     

     

     

     

    • PoviderManager  : AuthenticationManager구현체, 
      • authenticate() : 인증 처리, 인자로 받은 유저의 정보(Authentication) 값이 유효한지 판단.
        • 인증 방법 (Form / JWT / OAUTH) 에 따라 처리 할 수 있는 Provider를 통해 "Authentication" 값이 유효한지 판단
          • ex ) 사용자가 입력한 password가 UserDetails를 통해 읽어온 UserDetails 객체에 들어있는 password와 일치하는지 확인
          • 해당 사용자 계정이 잠겨 있진 않은지, 비활성 계정은 아닌지 등 확인

     

     

    최종적으로 AuthenticationManager가  인자로 받은 유저 인증 정보 "Authentication"을 Provider로 부터 인증을 마친 후 리턴 받은 Authentication(Principal, ...)은 SecurityContextHolder에 저장

    댓글

Designed by Tistory.