CSRF
요청 위조
Spring Security가 적용된 Application이 CSRF를 방어하도록 설정
CSRF Token : 내가 한 것을 입증 할 수 있다.
SecurityConfig.java
//.requestMatchers(AntPathRequestMatcher.antMatcher("/member/login")) 주석 처리
추가
.requestMatchers(AntPathRequestMatcher.antMatcher("/member/login"))
.permitAll()
// CSRF 방어를 적용한다
//http.csrf(csrf -> csrf.disable());
모든 HTTP POST, PUT, DELETE 요청에 CSRF Token 전달하기
- Form 태그를 직접 사용하지 않고 springForm을 사용할 경우, CSRF 태그는 자동 적용된다.
- 표준 Form 태그를 사용할 경우, <sec:csrfInput />을 사용해야 한다.
이렇게 form 안쪽에 작성.
Ajax 요청에 CSRF 적용
- Form을 사용하지 않는 Ajax 통신은 CSRF 토큰을 직접 전달해주어야 한다.
jsp -> <sec:csrfMetaTags /> 추가
js
CORS
Cross-Origin Resource Sharing - 교차 출처 리소스 공유
<방법>
두 개의 Application 준비한다.
hello-spring 프로젝트를 복사해 hello-spring-9090으로 붙여넣기
hello-spring-9090/application.yml 포트 변경한 후 두 Application 구동.
#포트 변경
server.port=9090
hello-spring에서 hello-spring-9090으로 댓글을 등록해보면,
변경 후 댓글을 등록하면, CORS 정책에 의해 요청이 Block되었음을 알 수 있다.