-
Spring Boot 3.0 + Spring Security 6.0.0 FilterChain 작성 방법프로그래밍/Spring Boot 2022. 12. 18. 23:27
IntelliJ에서 기본적으로 스프링부트 프로젝트를 만들어주는 Spring Initialize가 있어서 항상 편하게 사용하는 편이다.
이번에 Spring Boot 3.0.0을 사용하고,
Dependency Module을 항목에서 Spring Security를 사용 했더니,
Spring Security 6.0.0 버전으로 설치가 된 것이다...
뭔가 앞자리가 6이고 뒷자리가 0.0 이라서 불안하긴 하지만...
열심히 리서치를해서 그나마 설정은 끝낸 것 같다.
@ComponentScan @Configuration public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception { return httpSecurity .headers(header -> header.disable()) .csrf(csrf -> csrf.disable()) .authorizeHttpRequests(auth -> auth .requestMatchers("/**").permitAll() .anyRequest().authenticated() ) .httpBasic(Customizer.withDefaults()) .formLogin().disable() .build(); } }
내가 설정은 코드는 위에와 같은데 SecurityFilterChain 객체를 반환해주는 Bean을 등록하면, 내부적으로 Security가 적용 되는 것 같다.
뭐 아무튼...
조금더 내용을 추가하여 정리를 해보면, SpringFilterChain 객체를 반환해주는 Bean을 등록하는게 필수적인 것 같다.
해당 내용은 HttpSecurity 객체에 람다(Lambda) 방식을 통해 FilterChain 객체를 최종적으로 빌드하게 됩니다.
HttpSecurity 구성 설정을 통해 특정 http요청에 대해 웹보안을 구성하면됩니다.
물론 과거 Spring Security 버전과 마찬가지로, 특정 URL에 대한 설정을 해주셔야합니다.
저는 중간에 requestMatcher("/**").permitAll() 을 통해, 모든 요청에 대해 접근 권한 없이 접근할 수 있도록 지정하였습니다.
스프링 시큐리티 자체가 한번 대충 보고 쓸 수 있는 라이브러리 같은게 아니라서 전체적으로 말로 설명하기가 힘드네요.
이후에 더 공부해서 스터디 자료 만들면서 공부하고 더 좋은 내용을 작성하겠습니다.
Spring Security GitHub에 있던 샘플코드이다.
GitHub - spring-projects/spring-security-samples
Contribute to spring-projects/spring-security-samples development by creating an account on GitHub.
github.com
'프로그래밍 > Spring Boot' 카테고리의 다른 글
Spring Boot 테스트코드 동작시 CommandLineRunner 중복 문제 (0) 2023.01.09 RestAPI PathVariable 부분에 여러개의 파라미터를 매핑하기 (0) 2022.12.18