스프링 시큐리티 설정 후 h2 접속이 거부된다면, SpringBoot 3.x.x 버전과 2.x.x 버전이 해결 방법이 다르다.
우선 2버전 해결 방법부터 보자면 이렇다.
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests() // 권한요청 처리 설정 메서드
.antMatchers("/h2-console/**").permitAll() // 누구나 h2-console 접속 허용
.and()
// ...
.csrf()
.ignoringAntMatchers("/h2-console/**")
.disable(); // GET메소드는 문제가 없는데 POST메소드만 안되서 CSRF 비활성화 시킴
}
출처: https://moonsiri.tistory.com/33 [Just try it!:티스토리]
이렇게 h2 요청들만 추가로 처리해주면 접속이 가능해진다.
그런데 3버전부터는 시큐리티 설정 정보를 작성하는 코드 문법도 달라지기도 해서,
해결하는 방법도 달라졌다!
내가 해결한 방법은, SecurityConfig 클래스의 제일 하단에 아래의 코드를 넣어줬더니 접속이 가능해졌다.
@Bean
@ConditionalOnProperty(name = "spring.h2.console.enabled",havingValue = "true")
public WebSecurityCustomizer configureH2ConsoleEnable() {
return web -> web.ignoring()
.requestMatchers(PathRequest.toH2Console());
}
해결!
'에러 기록' 카테고리의 다른 글
스웨거 Failed to load remote configuration (0) | 2024.01.10 |
---|---|
Swagger 403 에러 (1) | 2024.01.03 |
AWS - cd 배포 과정 중 Environment still has health 오류 해결 (1) | 2023.12.27 |
MalformedJwtException - jwt토큰 손상 문제 (Swagger) (2) | 2023.11.16 |
(Spring) 경로 변수 설정 문제 - requestMappingHandlerMapping (0) | 2023.09.25 |