CSRF
🔥 CSRF (사이트 간 요청 위조, Cross-Site Ruquest Forgery)
공격자가 인증된 브라우저에 저장된 쿠키의 세션 정보를 활용하여 웹 서버에 사용자가 의도하지 않은 요청을 전달하는 것
CSRF 설정이 되어있는 경우 html에서 CSRF 토큰 값을 넘겨주어야 요청을 수신 가능
쿠키 기반의 취약점을 이용한 공격이기 때문에 REST 방식의 API에서는 disable 가능
POST 요청마다 처리해주는 대신 CSRF protection을 disable
Spring Security - Filter Chain
- Spring 에서 모든 호출은 DispatcherServlet을 통과하게 되고 이후에 각 요청을 담당하는 Controller로 분배
- 이 때, 각 요청에 대해서 공통적으로 처리해야 할 필요가 있을 때, DispatcherServlet 이전 단계가 필요하며 이것이
Filter
- Spring Security도 인증 및 인가를 처리하기 위해 필터를 사용하는데, Spring Security는 FilterChainProxy를 통해 상세 로직 구현
Form Login 기반은 인증
- Form Login 기반 인증은 인증이 필요한 URL 요청이 들어왔을 때 인증이 되지 않았다면 로그인 페이지를 반환하는 형태
UsernamePasswordAuthenticationFilter
- UsernamePasswordAuthenticationFilter는 Spring Security의 필터인 AbstractAuthenticationProcessingFilter를 상속한 필터
- 기본적으로 Form Login 기반을 사용할 때 username과 password 확인하여 인증
- 인증 과정
- 사용자가 username과 password를 제출하면 UsernamePasswordAuthenticationFilter는 인증된 사용자의 정보가 담기는 인증 객체인 Authentication의 종류 중 하나인 UsernamePasswordAuthenticationToken을 만들어 AuthenticationManager에게 넘겨 인증 시도
- 실패하면 SecurityContextHolder를 비움
- 성공하면 SecurityContextHolder에 Authentication 세팅
- SecurityCotext
는 인증이 완료된 사용자의 상세정보(Authentication) 저장
- Authentication
- 현재 인증된 사용자를 나타내며, SecurityContext
에서 가져올 수 있음
- principal : 사용자 식별
- Username/Password 방식으로 인증할 때 일반적으로 UserDetails 인스턴스
- credentials : 주로 비밀번호, 대부분 사용자 인증에 사용한 후 비움
- authorities : 사용자에 부여한 권한을 GrantedAuthority로 추상화하여 사용
- UserDetailsService
: username/password 인증 방식을 사용할 때 사용자를 조회하고 검증한 후 UserDetails를 반환, Custom하여 Bean으로 등록 후 사용 가능
- UserDetails
: 검증된 UserDetails는 UsernamePasswordAuthenticationToken 타입의 Authentication을 만들 때 사용되며 해당 인증 객체는 SecurityContextHolder에 세팅됨, Custom하여 사용 가능
Spring Security 사용하여 로그인 과정
Spring Security 역할
- 인증 / 인가
- 성공 시, 컨트롤러로 client 요청 전달 (client 요청 + 사용자 정보(UserDetails))
- 실패 시, 컨트롤러로 client 요청 전달하지 않음(client에게 error response 전달)
- 인증 / 인가
로그인 처리 과정
'내일배움캠프' 카테고리의 다른 글
스프링 숙련 4주차 : 지연 로딩/즉시 로딩 (0) | 2023.11.29 |
---|---|
[KPT] 뉴스피드 프로젝트 (0) | 2023.11.28 |
스프링 숙련 3주차 : 필터 (1) | 2023.11.17 |
스프링 숙련 3주차 : JWT (0) | 2023.11.17 |
스프링 숙련 3주차 : 쿠키와 세션 (1) | 2023.11.17 |
CSRF
🔥 CSRF (사이트 간 요청 위조, Cross-Site Ruquest Forgery)
공격자가 인증된 브라우저에 저장된 쿠키의 세션 정보를 활용하여 웹 서버에 사용자가 의도하지 않은 요청을 전달하는 것
CSRF 설정이 되어있는 경우 html에서 CSRF 토큰 값을 넘겨주어야 요청을 수신 가능
쿠키 기반의 취약점을 이용한 공격이기 때문에 REST 방식의 API에서는 disable 가능
POST 요청마다 처리해주는 대신 CSRF protection을 disable
Spring Security - Filter Chain
- Spring 에서 모든 호출은 DispatcherServlet을 통과하게 되고 이후에 각 요청을 담당하는 Controller로 분배
- 이 때, 각 요청에 대해서 공통적으로 처리해야 할 필요가 있을 때, DispatcherServlet 이전 단계가 필요하며 이것이
Filter
- Spring Security도 인증 및 인가를 처리하기 위해 필터를 사용하는데, Spring Security는 FilterChainProxy를 통해 상세 로직 구현
Form Login 기반은 인증
- Form Login 기반 인증은 인증이 필요한 URL 요청이 들어왔을 때 인증이 되지 않았다면 로그인 페이지를 반환하는 형태
UsernamePasswordAuthenticationFilter
- UsernamePasswordAuthenticationFilter는 Spring Security의 필터인 AbstractAuthenticationProcessingFilter를 상속한 필터
- 기본적으로 Form Login 기반을 사용할 때 username과 password 확인하여 인증
- 인증 과정
- 사용자가 username과 password를 제출하면 UsernamePasswordAuthenticationFilter는 인증된 사용자의 정보가 담기는 인증 객체인 Authentication의 종류 중 하나인 UsernamePasswordAuthenticationToken을 만들어 AuthenticationManager에게 넘겨 인증 시도
- 실패하면 SecurityContextHolder를 비움
- 성공하면 SecurityContextHolder에 Authentication 세팅
- SecurityCotext
는 인증이 완료된 사용자의 상세정보(Authentication) 저장
- Authentication
- 현재 인증된 사용자를 나타내며, SecurityContext
에서 가져올 수 있음
- principal : 사용자 식별
- Username/Password 방식으로 인증할 때 일반적으로 UserDetails 인스턴스
- credentials : 주로 비밀번호, 대부분 사용자 인증에 사용한 후 비움
- authorities : 사용자에 부여한 권한을 GrantedAuthority로 추상화하여 사용
- UserDetailsService
: username/password 인증 방식을 사용할 때 사용자를 조회하고 검증한 후 UserDetails를 반환, Custom하여 Bean으로 등록 후 사용 가능
- UserDetails
: 검증된 UserDetails는 UsernamePasswordAuthenticationToken 타입의 Authentication을 만들 때 사용되며 해당 인증 객체는 SecurityContextHolder에 세팅됨, Custom하여 사용 가능
Spring Security 사용하여 로그인 과정
Spring Security 역할
- 인증 / 인가
- 성공 시, 컨트롤러로 client 요청 전달 (client 요청 + 사용자 정보(UserDetails))
- 실패 시, 컨트롤러로 client 요청 전달하지 않음(client에게 error response 전달)
- 인증 / 인가
로그인 처리 과정
'내일배움캠프' 카테고리의 다른 글
스프링 숙련 4주차 : 지연 로딩/즉시 로딩 (0) | 2023.11.29 |
---|---|
[KPT] 뉴스피드 프로젝트 (0) | 2023.11.28 |
스프링 숙련 3주차 : 필터 (1) | 2023.11.17 |
스프링 숙련 3주차 : JWT (0) | 2023.11.17 |
스프링 숙련 3주차 : 쿠키와 세션 (1) | 2023.11.17 |