본문 바로가기

프로젝트

[SpringSecurity] h2 설정

SpringSecurity를 적용한 후 로컬 개발 단계에서 사용하는 h2를 위한 설정을 해야합니다.

이때, 개발할 서버는 API 서버 설정임을 유의해야합니다.

(기본적인 h2의존성과 springsecurity의존성 설정이 되어있는 상태입니다.)

 

@RequiredArgsConstructor
@EnableWebSecurity
@Configuration
public class SecurityConfig {
	//...

   @Bean
   public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
      http
         .authorizeRequests()
            .antMatchers("/auth/**","/h2-console/**").permitAll()
            .anyRequest().authenticated()
         .and()
            .formLogin().disable()
            .httpBasic().disable()
            .csrf().disable()
         .headers().frameOptions().disable();

      return http.build();
   }
}
  • antMatchers("/h2-consooe/**").permitAll() : URI를 통해 h2에 접근해야하는데 인증과정없이 접근해야합니다. 그렇기 때문에 인증없이 접근가능하도록 설정
  • formLogin().disable() : API 서버를 개발하기 때문에 폼 로그인이 별도로 필요하지 않기때문에 비활성화 합니다.
  • csrf.disable() : API 서버를 개발하기 때문에 web 요청시 별도의 설정이 없다면 CSRF로 인해 웹브라우저에서 요청이 블락됨으로 이를 비활성화합니다.
    • 이 방법은 다른 web 통신을 수행할때 보안적으로 좋지 못할수있습니다. 그렇기 때문에 .csrf().ignoringAntMatchers("h2-console/**")를 추천합니다.
  • httpBasic().disable() : 기본적으로 인증용 로그인 창이 요청되는데 API 서버이기 때문에 비활성화합니다.
  • headers().frameOptions().disable() : xframe 공격을 막기위한 설정입니다. API 서버이기 때문에 비활성화합니다.