[스프링 시큐리티] WebSecurityConfigurerAdapter deprecated 대응

2023. 2. 6. 23:21·Spring
목차
  1. WebSecurityConfigurerAdapter is deprecated
  2. HttpSecurity 설정
  3. WebSecurity 설정
  4. JDBC 설정
  5. References
반응형

 

스프링 시큐리티 로고

WebSecurityConfigurerAdapter is deprecated

Spring Security 6.0 버전 기준으로 WebSecurityConfigurerAdapter를 완전 사용할 수 없게 됐다.

import를 하지 못한다.

기존 WebSecurityConfigurerAdapter를 상속 후에, configure 메소드를 오버라이딩 하는 방식은 deprecated 됐다.
대신 개발자가 직접 component-based security 설정을 할 수 있도록 변경되었다. 즉 커스텀 할 설정들을 @Bean으로 등록하여 사용한다.

HttpSecurity 설정


      
// 변경된 방식 (filterChain을 사용)
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
return http.build();
}
}
// 기존 방식 (deprecated)
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
}
}

 

WebSecurity 설정


      
// 변경된 방식(WebSecurityCustomizer 클래스 사용)
@Configuration
public class SecurityConfiguration {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
// antMatchers 부분도 deprecated 되어 requestMatchers로 대체
return (web) -> web.ignoring().requestMatchers("/ignore1", "/ignore2");
}
}
// 기존 방식
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/ignore1", "/ignore2");
}
}

JDBC 설정


      
// 변경된 방식
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
auth.jdbcAuthentication()
.withDefaultSchema()
.dataSource(dataSource())
.withUser(user);
}
}
// 기존 방식
@Configuration
public class SecurityConfiguration {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript(JdbcDaoImpl.DEFAULT_USER_SCHEMA_DDL_LOCATION)
.build();
}
@Bean
public UserDetailsManager users(DataSource dataSource) {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
JdbcUserDetailsManager users = new JdbcUserDetailsManager(dataSource);
users.createUser(user);
return users;
}
}

그 외에 공식문서에서 Ldap이나 In-memory 그리고  AuthenticationManager등의 변경된 설정법을 확인할 수 있다.


References

Spring Security without the WebSecurityConfigurerAdapter - https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter

Spring Security: Upgrading the Deprecated WebSecurityConfigurerAdapter - https://www.baeldung.com/spring-deprecated-websecurityconfigureradapter

반응형
저작자표시 동일조건 (새창열림)
  1. WebSecurityConfigurerAdapter is deprecated
  2. HttpSecurity 설정
  3. WebSecurity 설정
  4. JDBC 설정
  5. References
'Spring' 카테고리의 다른 글
  • [Spring AI] 챗봇 만들기 (Kotlin)
  • [Spring AI] 준비 (기본 개념, OpenAI API Key, 크레딧 충전)
  • [스프링 시큐리티] AuthenticationManager, AuthenticationProvider
  • [스프링 시큐리티] 필터(DelegatingFilterProxy, FilterChainProxy)
SooJae
SooJae
코드는 효율적으로, 공부는 비효율적으로
    반응형
  • SooJae
    이수재 블로그
    SooJae
  • 전체
    오늘
    어제
    • 분류 전체보기 (60)
      • Spring (8)
      • Next.JS (4)
      • React (3)
      • Angular (1)
      • Language (6)
        • Java (1)
        • Kotlin (1)
        • Javascript (4)
      • Keycloak (5)
      • Knowledge (16)
        • Test (4)
        • Web (9)
        • Security (2)
        • Data Structure (1)
      • Infra (9)
        • Proxmox (2)
        • AWS (0)
        • Kubernetes (3)
      • Tools (1)
        • IntelliJ (1)
      • Algorithm (2)
      • Tistory (4)
      • ETC (1)
  • 블로그 메뉴

    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    spring ai
    GPT
    deepl api
    오블완
    티스토리챌린지
    springboot
    스프링 ai
    javascript
    ChatGPT
    openAI
    스프링 번역
    Kotlin
    웹 마스터 도구
    keycloak
    Auth
    ai
    Next.js
    React
    test
    Functional Programming
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
SooJae
[스프링 시큐리티] WebSecurityConfigurerAdapter deprecated 대응
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.