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 을 오류없이 실행할 수 있다.
반응형
'백엔드' 카테고리의 다른 글
[Spring Boot] entity 생성 시 @Column 어노테이션을 생략한다면? (1) | 2024.10.13 |
---|---|
[Spring Boot] LocalDate를 LocalDateTime으로 변환 (0) | 2024.10.12 |
[Spring Boot] Client가 String으로 전달한 날짜 LocalDateTime으로 저장하기 (0) | 2024.10.12 |
[SpringBoot / IntelliJ] 인텔리제이 스프링부트 실행 시 sql문 실행 (1) | 2024.09.18 |
[IntelliJ/GitHub] 인텔리제이 깃허브 연동하는 방법, 인텔리제이 코드 깃허브에 올리기 (0) | 2024.08.28 |