본문 바로가기

백엔드

[Spring Boot] Spring Security 환경에 h2 database 연결하기 (SecurityConfig 설정)

728x90

H2 database 연결하기

 

Security 환경에서 H2 console 사용 가능하도록 설정하기

 

Spring Security를 적용하면 웹에서 확인하는 h2 database가 차단되거나 잘 보이지 않는다. 이는 security에 적용된 보안 문제 때문이다. 아래 Security Config 파일 설정을 통해 이 문제를 해결할 수 있다.

 

SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

	// 1
        http
                .csrf((auth) -> auth.ignoringRequestMatchers(PathRequest.toH2Console()).disable());
		
        // 2
        http.
                headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin));

	//3 , 4
        http
                .authorizeHttpRequests((auth) -> auth
                        .requestMatchers(PathRequest.toH2Console()).permitAll()
                        .requestMatchers("/h2-console/**").permitAll()
                        .anyRequest().authenticated()
                );

        return http.build();
    }
}

 

4가지 설정만 하면 /h2-console 을 오류없이 실행할 수 있다.

 

반응형