Spring Security
1. Spring Security 로그인 진행과정
진행과정 :
1-1. /login 같은 URL로 POST 요청(아이디/비밀번호 데이터 포함)을 보냅
1-2. AuthenticationFilter 에서 요청을 가로챔
1-3. Filter는 요청에서 아이디와 비밀번호를 추출하여 아직 검증되지 않은 Authentication 객체(토큰)를 만듬
1-4. AuthenticationFilter는 방금 만든 '미인증 토큰'을 AuthenticationManager 에 전달
1-5. AuthenticationManager 는 AuthenticationProvider 에게 넘김
1-6. AuthenticationProvider는 UserDetailsService클래스에게 "DB에서 이 아이디를 가진 유저 정보 좀 다 가져와 줘"라고 요청(loadUserByUsername)
1-7. UserDetailsService는 조회후 실제 비밀번호, 권한 등의 정보를 담은 UserDetails라는 객체를 생성
1-8. UserDetailsService는 UserDetails를 AuthenticationProvider 에게 돌려줌
1-9. AuthenticationProvider 는 UserDetails의 비밀번호와 처음 전달받았었던 비밀번호를 암호화 로직을 통해 비교
1-10. 일치하는경우 유저의 권한 정보(Authorities)까지 넣은 '인증이 완료된 새로운 Authentication 객체'를 만들어서 AuthenticationManager에게 반환
1-11. AuthenticationManager 는 '인증 완료 토큰'을 다시 맨 처음의 AuthenticationFilter에게 돌려놈
1-12. AuthenticationFilter는 이 완성된 인증 토큰을 SecurityContextHolder라는 아주 특별한 전역 저장소에 집어넣음
1-12-1. 여기에 저장이 완료되면 스프링 시큐리티는 이 사용자를 최종적으로 "인증된(로그인된) 사용자"로 취급하고 세션을 유지하게 됨.
순서도 : /login(post)
-> AuthenticationFilter
-> Authentication 객체 생성
-> AuthenticationManager 전달
-> AuthenticationProvider 전달
-> UserDetailsService(loadUserByUsername) 호출
-> UserDetails라는 객체를 생성
-> AuthenticationProvider
-> Authentication 과 UserDetails을 비교
-> 일치시 새로운 Authentication 생성
-> AuthenticationManager에게 전달
-> AuthenticationFilter에게 전달
-> SecurityContextHolder에 Authentication저장
2. Spring Security 로그인 실패시 순서
인증실패시 순서도 :
위의 -> Authentication 과 UserDetails을 비교 까지 동일하게 진행
-> Authentication 과 UserDetails불일치
-> BadCredentialsException (잘못된 자격 증명 예외) 에러 발생
-> AuthenticationProvider에게 에러전달
-> AuthenticationFilter에게 에러전달
-> Filter가 AuthenticationFailureHandler 호출
-> 웹 페이지(Form 로그인)인 경우: 사용자를 다시 로그인 페이지(/login)로 리다이렉트.
이때 보통 URL 끝에 ?error를 붙여서 화면에 "아이디 또는 비밀번호가 맞지 않습니다."라는 경고 문구를 띄움
REST API(JSON 통신)인 경우: 상태 코드 401 Unauthorized (권한 없음)와 함께 로그인 실패 메시지를 JSON 형태로 클라이언트(앱 또는 프론트엔드)에게 응답.
3. csrf
CSRF 공격: 브라우저가 로그인 쿠키를 자동으로 첨부해 버리는 성질을 이용한 묻지마 공격.
CSRF 토큰: 자동으로 날아오는 쿠키만 믿지 않고, 우리 웹사이트 화면에서만 알 수 있는 일회용 비밀번호(토큰)를 수동으로 제출하게 만들어서 진짜 요청인지 검사하는 방어막.
댓글
댓글이 없습니다.
