CSRF( Cross Site Request Forgery ) 란?
CSRF는 사용자와 서버의 요청과 응답 간의 위조이다.
로그인 기능의 보안을 위한 기능으로, 사용자를 속여 의도치 않은 요청을(PostRequest) 실행시키는 공격을 방지해주는 매커니즘이다!
예시 코드
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
// ...
.csrf((csrf) -> csrf
.csrfTokenRepository(new HttpSessionCsrfTokenRepository())
);
return http.build();
}
}
사용자인 클라이언트가 서버에 요청을 보낼 때, CSRF 토큰을 확인한다.
이에 대하여 토큰을 확인하고 토큰이 불일치하거나 누락, 혹은 요청이 변경되면 접근을 거부한다.

위의 과정과 같이 CSRF 토큰을 발급받아 로그인을 진행한다
생성한 토큰은 CsrfTokenRepository에 저장
각각의 상황에 대한 Handler들은 상황에 맞게 interface를 implement함으로써 조작이 가능
(SuccessHandler, DeniedHandler 등 Custom으로 가능)

위와 같이 security의 필터를 설정하면,
인증 및 인가 검증을 하면서 무조건적으로 해당 filter를 거치게 되는 것이다.
해당 검증에서 통과하지 못하면 인증(Authentication) 실패로 이어지게 된다.
이는 Security에 대한 Configuration 파일에 저장할 수 있으며,
만약 제외시키고 싶다면 AuthenticationFilter에 대하여 Shouldnotfilter를 상속 받아 설정해주면 된다.
REF
https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html#csrf-components
'백엔드' 카테고리의 다른 글
[Redis] Redis에서 삭제된 캐시 복구하는 방법 (0) | 2025.02.22 |
---|---|
[백엔드] Test의 종류 (0) | 2025.02.19 |
[GitHub] 깃허브 pull 에러 해결하기 : files would be overwritten by merge (0) | 2024.11.25 |
[AWS] AWS 배포 중 gradlew build Permission denied (0) | 2024.11.21 |
[백엔드] Configuration Bean 인식 충돌 오류 (0) | 2024.11.21 |