csrf

서지우·2024년 3월 14일
0

Spring Boot

목록 보기
17/18


스프링 부트 프로젝트에 시큐리티 디펜던시를 추가하면

아무런 설정이 안되어 있어도 프로젝트가 시큐리티 적용이 된다.

프로젝트를 실행하고 접속해보면 로그인 창이 뜬다

기본 아이디 user
기본 비밀번호 터미널에서 찾을 수 있음

시큐리티에서 제공하는 로그인은 프로젝트의 로그인과 별도이다.

프로젝트의 세션과 시큐리티의 세션 저장위치가 다르다.

bean으로 시큐리티 필터체인을 만들어주면
기존에 자동 생성되었던 시큐리티필터체인이 작동되지 않고
개발자가 만든 시큐리티필터체인이 적용된다.

package com.example.my.config.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    
    @Bean
    // throw exception은 알아서 처리(함수 호출한 사람이), try catch는 안에서 처리
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception{

        // 이거랑 같은 느낌 httpSecurity.setName(); 
        //(매개변수 -> 내용)
        httpSecurity.csrf(config -> config.disable());

        return httpSecurity.getOrBuild();
    }

}


csrf를 안하면 요래

하면

서버 csrf토큰을 만들어서 페이지에 넣어줌

페이지에서 어떤 요청을 할때
해당 토근이 없으면 비정상 요청이라고 판단

csrf설정

    <!-- csrf token  -->
    <meta name="_csrf" th:content="${_csrf.token}" />
    <meta name="_csrf_header" th:content="${_csrf.headerName}" />

https://velog.io/@kimujin99/Side-project-Spring-Security-CSRF-Token-%ED%99%9C%EC%9A%A9%ED%95%98%EA%B8%B01

profile
미래가 기대되는 풀스택개발자 공부 이야기~~

0개의 댓글