Spring Security h2-console 허가 하기

김재익·2023년 7월 11일
0

SPRING FRAMEWORK

목록 보기
6/6
post-thumbnail

개요

스프링 부트 예제를 만들다가 나는 h2 인메모리 방식으로 db를 사용했는데 h2 console을 띄울려고 하니까 계속 formLogin()이 실행되어 로그인 화면이 나온다.
해결 과정은 짧게 쓰고 답변을 바로 작성하겠다.

과정

http.csrf(csrf -> csrf.ignoringRequestMatchers("/h2-console/**"));
http.headers(header -> header.frameOptions(options -> options.sameOrigin()));
http.authorizeHttpRequests(requests -> requests.requestMatchers("/h2-console/**").permitAll());

csrf disable 했다가 ignoring에 콘솔 주소 썼다가
frameOptions disalbe 했다가 sameOrigin 했다가
온갖 똥꼬쇼를 했지만 안됐다.

해결

결국 비장의 영어 구글링을 사용해서 어지러움을 참으며 찾아냈다.

https://stackoverflow.com/questions/74680244/h2-database-console-not-opening-with-spring-security

마지막에 있는 답변 toH2Console()추가

csrf는 그냥 disable 해둬도 된다.

http.headers(header -> header.frameOptions(options -> options.sameOrigin()));
http.authorizeHttpRequests(requests -> requests.requestMatchers(toH2console()).permitAll());
import static org.springframework.boot.autoconfigure.security.servlet.PathRequest.*;

security 에서 제공하는 PathRequest에 있는 toH2console()로 h2 console의 RequestsMatcher를 넣어줌으로서 해결이 됐다.

생각

toH2Console()의 정확한 내부 반환값을 알고 싶었지만 너무 구조가 복잡해서 이해하지 못했다.
디버깅으로 보다가 도저히 이해 못하겠어서 대충 훑어봤는데 "/h2-console/**" 이거랑 뭔 차인지 모르겠다.

String으로 넣으면 MvcRequestMatcher로 적용되고 toH2Console()을 넣은것은 PathRequest의 H2ConsoleRequestsMatcher로 적용되는데 둘의 차이가 뭔지 모르겠다.

profile
개발자호소인

0개의 댓글