스프링 시큐리티 인증 절차

2023. 9. 26. 12:59
728x90

<용어 정리>

1) Authentication (인증) : 'A'라고 주장하는 주체가 'A'가 맞는지 확인하는 것.

- 코드에서 Authentication : 인증 과정에 사용되는 핵심 객체.

2) Authorization (권한) : 특정 자원에 대한 권한이 있는지 확인하는 것. > 프로세스상 신분 '인증'을 거치고 신분인증이 되면 권한이 있는지 확인 후 서버 자원에 접근할 수 있도록 하는 순서.

3) Credential (증명서) : 인증 과정 중, 주체가 본인을 인증하기 위해 서버에 제공하는 것 (id, pwd 등) 

 

4) AuthenticationManager(interface) : Authentication 객체를 받아 인증하고 인증되면 인증된 Authentication 객체를 돌려주는 메서드를 구현하도록 하는 인터페이스 > isAuthenticated(boolean)값을 인증받으면 true로 바꿔줌

5) ProviderManager(class) : 스프링에서 인증을 담당하는 클래스. 직접 구현할 필요는 없지만, 멤버 변수로 가지고 있는 AuthenticationProvider들에게 인증을 위임처리하고 그 중에 하나의 AuthenticationProvider 객체가 인증 과정을 거쳐서 인증에 성공하면 요청에 대해 ProviderManager가 인증이 되었다고 알려주는 방식. > authenticate() 메서드의 리턴 값을 Authentication 객체 안에 인증 값을 넣어준다. 

 

6) AuthenticationProvider(interface) : 인증 가능한 클래스인지 확인하는 메서드. UsernamePasswordAuthenticationToken이 ProviderManager에 도착하면 > ProviderManager는 갖고 있는 AuthenticationProvider 목록을 순회하면서 해결 가능하다면 (supports()) TRUE를 리턴해주는 authentication() 메서드를 실행한다. 

1) 사용자가 로그인form을 통해 로그인 정보를 입력하고 인증 Request를 보낸다. 

 

2) 여러 필터 중 AuthenticationFilter(UsernamePasswordAuthenticationFilter)가 HttpServletRequest에서 사용자가 보낸 아이디와 패스워드를 인터셉트하여 유효성을 검사함.

- 유효성 검사 후 HttpServletRequest에서 꺼내온 사용자 아이디와 패스워드를 인증 처리부분 총괄하는 매니저 역할을 하는 AuthenticationManager 인터페이스(ProviderManager)에게 인증용 객체인 임시 Authentication 객체(UsernamePasswordAuthenticationToken)를 만들어 위임한다. 

 

3) AuthenticationManager임시 인증용 Authentication객체를 전달 받아 실질적인 인증부분을 담당하는 AuthenticationProvider에게 임시 인증용 객체인 Authentication 객체를 넘긴다. 

 

4) AuthenticationProvider에서 접속한 사용자와 DB의 사용자가 맞는지 아닌지 실질적인 인증 작업이 이루어진다.

- 먼저 UserDetailsService에서 DB에서 사용자가 로그인 요청한 정보가 있는지 가져오도록 지시함. 

 

5) 지시를 받은 UserDetailsService는 DB에서 사용자의 로그인 정보와 동일한 정보를 찾아 UserDetail객체에 담음.

 

6) DB에서 사용자의 로그인정보를 담아온 UserDetails 객체를 실질적인 인증부분을 담당하는 AuthenticationProvider에게 전달한다.

- 이곳에서 PasswordEncoder를 이용하여 UserDetails 객체에 담긴 암호화 된 Password와 인증을 위한 임시 Authentication 객체 안에 포함된 Password가 일치하는지 검증함. 

- 검증에 성공하면 UserDetails를 이용하여 인증된 Authentication 객체를 생성

- 검증에 실패하면 예외 발생, 인증처리 중단

 

7) 인증된 Authentication 객체AuthenticationManager에게 전달 

- 이 때 사용자의 정보 (Principal, Credential, GrantedAuthorities)를 가지고 있음.

 

8) 인증된 Authentication 객체AuthenticationFiler로 전달.

 

9) SecurityContextHolder를 이용해 SecurityContext에 인증된 인증된 Authentication 객체를 저장함. 

 

 

728x90

BELATED ARTICLES

more