-
[Spring Security] 사용자 정의 보안 기능 구현Spring/Spring Secutiry 2024. 1. 9. 22:34

👋안녕하세요. 김예외입니다.
이번 글에선 스프링 시큐리티의 웹 보안 기능 초기화 및 설정하는 클래스 WebSecurityConfigurerAdapter 를 상속받아 커스텀 인증 기능을 구현해 보겠습니다.
WebSecurityConfigurerAdapter 상속
먼저 WebSecurityConfigurerAdapter를 사용하기 위해 상속받아서 SecurityConfig클래스를 만들어 보겠습니다.

같은 경로에 그냥 SecuityConfig로 만들었습니다. 
@Configuration 어노테이션과 @EnableWebSecurity 어노테이션을 추가해 줍니다.
그리고 WebSecurityConfigurerAdapter를 상속받으려 했는데 Deprecated 되어서 사용할 수 없다고 뜹니다.
공식문서 참조 : https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter/
Spring Security without the WebSecurityConfigurerAdapter
In Spring Security 5.7.0-M2 we deprecated the WebSecurityConfigurerAdapter, as we encourage users to move towards a component-based security configuration. To assist with the transition to this new style of configuration, we have compiled a list of common
spring.io
WebSecurityConfigurerAdapter가 사용중지 되었으니 SecurityFilterChain를 Bean으로 등록해서 사용해야 합니다.
SecurityConfig
@Configuration @EnableWebSecurity class SecurityConfig { @Bean fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { http //인가 .authorizeRequests() .anyRequest().authenticated() //모든요청에 인증 받음 http //인증 .formLogin() return http.build() } }모든 요청에 인증받도록 입력해 주었습니다.
application.properties
# 임시 계정 spring.security.user.name=user spring.security.user.password=1111임시로 사용할 계정을 등록해 보겠습니다. 이제는 user/1111로만 로그인이 가능합니다.
'Spring > Spring Secutiry' 카테고리의 다른 글
[Spring Security] Form Login 인증 Api 사용하기 (0) 2024.01.09 [Spring Security] 스프링 시큐리티 의존성 추가 (1) 2024.01.09