무지성 박치기로 로그인을 구현하려했다가 몇주를 헤맸던지... 만약 처음으로 로그인을 구현하려 한다면 documentation부터 차근차근 보는것을 추천합니다. 깃헙 뒤지고 아무리 구글링해도 내가 딱 원하는 나만의 로그인을 구현하다보면 생각외로 굉장히 많은 생소한 에러를 발견합니다.
1. SecurityContextHolder
SecurityContextHolder는 SecurityContext를 가지고 있고 이를 통해 인증을 증명할 수 있습니다. 일반적으로 thread-local을 사용해서 정보를 저장하기 때문에 동일한 스레드라면 항상 SecurityContext에 접근 가능합니다. 다른 방식을 원하면 따로 설정 가능합니다.
2. SecurityContext
Authentication 객체를 가지고 있습니다.
#SecurityContext로도 충분한것 같은데 SecurityContextHolder는 꼭 필요한가?
SecurityContextHolder는 애플리케이션의 여러 부분에서 SecurityContext에 접근할 수 있는 전역적인 접근점을 제공합니다.
3. Authentication
3.1 역할
1. 인증 관리자의 입력으로 사용되어 인증 대상의 credential을 제공합니다. 이 경우 isAuthenticated() 값은 false입니다.
2. 현재 인증된 사용자를 나타냅니다. true
3.2 구조
1. principal - 사용자 식별, id/pw로 인증할땐 UserDetails 인스턴스
2. credentials - 비밀번호 (비공개)
3. authorities - 부여하는 권한 (GrantedAuthority로 추상화)
4. GrantedAuthority
어플리케이션 전체에 걸친 권한
5. AuthenticationManager
AuthenticationManager는 Spring Security 필터에서 인증을 수행하는 방식을 정의하는 API입니다. Manager가 반환하는 Authentication을 SecurityContextHolder에 설정하는 것은 Manager를 호출한 필터가 담당합니다. ProviderManager가 일반적으로 구현체로 사용됩니다.
6. ProviderManager
ProviderManager는 AuthenticationManager의 구현체로, AuthenticationProvider 목록에 인증 처리를 위임합니다.
7. AuthenticationProvider
AuthenticationProvider는 ProviderManager에 주입되는 구성 요소로, 다양한 유형의 인증을 수행합니다. DaoAuthenticationProvider, JwtAuthenticationProvider 등이 예시입니다.
8. request credentials with AuthenticationEntryPoint
클라이언트가 접근 권한이 없는 리소스에 인증되지 않은 요청을 보낼 때 클라이언트에게 credential을 요청합니다.
9. AbstractAuthenticationProcessingFilter
credential을 인증하기 위한 기본 필터
9.1 필터 동작 순서
1. 사용자가 credential 제출
2. abstractAuthenticationProcessingFilter는 httpservletrequest로 부터 authentication 생성 ( authentication 타입은 하위클래스에 따라 다름 ex) usernamepasswordauthenticationfilter는 usernamepasswordauthenticaitontoken 생성)
3. authentication을 authenticationmanager로 넘겨서 인증
4. 실패시 authenticationfailuerhandler 실행
5. 성공시 securitycontextholder에 authentication 세팅후 세션관련 처리
10. Username/Password Authentication
이름과 비밀번호로 인증할수있는 방법을 종합적으로 지원합니다.
10.1 읽는 메커니즘
1. 폼 로그인
2. 기본인증
3. 다이제스트 인증
10.2 저장 메커니즘
1. 인메모리 인증, 심플 스토리지
2. jdbc 인증, rdb
3. userdetailservice, 커스텀 데이터 스토어
4. ldap인증과 ldap 스토리지
11. Session Management
HTTP 세션 관련 기능은 SessionManagementFilter와 SessionAuthenticationStrategy 인터페이스에서 처리됩니다. 세션 픽스에이션 공격 방어, 타임아웃 감지, 동시에 열 수 있는 세션 수 제한 등의 기능이 포함됩니다.
12. remember-me authentication
쿠키를 이후 세션에서 감지하고 자동으로 로그인하는 방식입니다.
UserDetailsService가 꼭 필요
# remember-me와 refresh token의 차이
remember-me : 사용자가 로그인 세션이 만료되더라도 자동으로 로그인 상태를 유지하기 위함입니다.
refresh token : JWT 등의 액세스 토큰이 만료되었을 때, 새로운 액세스 토큰을 발급하여 사용자의 로그인 상태를 유지하고 지속적인 인증을 보장하기 위함입니다.
이 외에도 Anonymous Authentication, CAS authentication, X.509 Authentication 여러 인증방식이 존재합니다. 자세한건 reference를 보시고 원하는 것을 채택하시면 됩니다.
# References
chatgpt
https://godekdls.github.io/Spring%20Security/authentication/
'Back-end > Spring-login project' 카테고리의 다른 글
OAuth 2.0의 원리와 동작 (0) | 2023.06.16 |
---|---|
Spring Security의 기초와 구조 (0) | 2023.06.12 |
고민의 흔적 (~ing) (0) | 2023.04.22 |
요구사항과 설계 (0) | 2023.03.21 |