💐 Spring/Spring Security

(Deprecated) WebSecurityConfigurerAdapter에 대응하기

2022. 12. 9. 00:30
목차
  1. 변경된 시큐리티 설정
  2. HttpSecurity 설정
  3. WebSecurity 설정
  4. AuthenticationManager 빈 등록
반응형

 

Spring Boot 2.7 (Spring 5.7.0-M2) 부터 WebSecurityConfigurerAdapter는 Deprecated가 되었다.

 

Spring Security without the WebSecurityConfigurerAdapter

<p>In Spring Security 5.7.0-M2 we <a href="https://github.com/spring-projects/spring-security/issues/10822">deprecated</a> the <code>WebSecurityConfigurerAdapter</code>, as we encourage users to move towards a component-based security configuration.</p> <p

spring.io

스프링 공식 블로그에서 제공하는 방식을 빠르게 정리해보겠다.

변경된 시큐리티 설정

HttpSecurity 설정

기존 설정 방식

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
    }

}

스프링은 SecurityFilterChain 빈을 등록하는 방법을 소개한다.

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
        return http.build();
    }

}

WebSecurity 설정

기존 방식

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/*.html", "/*.css", "/*.js");
    }

}

WebSecurityCustomizer를 빈으로 등록하는 방식

@Configuration
public class SecurityConfiguration {

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web
                                                .ignoring()
                                                .antMatchers("/*.html", "/*.css", "/*.js");
    }

}
  • 스프링 공식문서에서는 해당 방법을 권장하지 않는다.

ignoring을 한다면 애초에 시큐리티를 타지 않기 때문에 csrf 공격이나 다른 공격에 취약할 수 밖에 없다.

그러므로 스프링 시큐리티는 해당 방법을 권장하지 않는다.

권장하는 방법은 아래와 같다.

(HttpSecurity http) {
    http
        .requestMatchers(...atCommonResource()).permitAll()
    ...
}

그냥 정적 리소스의 Path를 permitAll()을 해주면 되는 것이다. 정적 리소스에 대한 인증은 거치지 않으면서도 시큐리티의 기본적인 보안 기능을 적용시키는게 권장사항이다.

  • 디버그도 함께 찍고 싶다면 아래 코드를 추가한다.
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
    return (web) -> web.`debug`(securityDebug)
        .ignoring()
        .antMatchers("/css/**", "/js/**", "/img/**", "/lib/**", "/favicon.ico");
}

AuthenticationManager 빈 등록

기존 방식

@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
  return super.authenticationManagerBean();
}

변경된 방식

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
    return authenticationConfiguration.getAuthenticationManager();
}

REFERENCES

  • https://docs.spring.io/spring-security/reference/servlet/authorization/authorize-http-requests.html
  • https://docs.spring.io/spring-security/reference/servlet/authorization/authorize-requests.html
  • https://www.baeldung.com/spring-deprecated-websecurityconfigureradapter
  • https://www.appsdeveloperblog.com/migrating-from-deprecated-websecurityconfigureradapter/
  • https://honeywater97.tistory.com/264
반응형
저작자표시 (새창열림)
  1. 변경된 시큐리티 설정
  2. HttpSecurity 설정
  3. WebSecurity 설정
  4. AuthenticationManager 빈 등록
'💐 Spring/Spring Security' 카테고리의 다른 글
  • CORS이란 무엇이고, Spring-boot에서 해결하기 위한 방법
  • Spring Security Error Code 별 페이지 처리하기
  • 12. JWT 토큰 Authorization을 위한 커스텀 필터 생성
  • 11. JWT 로그인을 위한 UsernamePasswordAuthenticationFilter 상속
iseunghan
iseunghan
꾸준하게 열심히..
iseunghan꾸준하게 열심히..
iseunghan
iseunghan

공지사항

  • 어제보다 나은 오늘이 되기 위해 🔥
  • 분류 전체보기 (261)
    • 💐 Spring (14)
      • 개념 및 이해 (2)
      • Spring 핵심 기술 (24)
      • Spring REST API (8)
      • Spring MVC, DB 접근 기술 (7)
      • Spring Security (23)
      • Spring in Action (1)
    • 🌻 JAVA (84)
      • 자바 ORM 표준 JPA 프로그래밍 (20)
      • 알고리즘, 자료구조 (13)
      • 디자인 패턴 (7)
      • 정리정리정리 (43)
      • JUnit (1)
    • 🔖 Snippets (3)
      • Javascript (3)
    • ⚙️ Devops (22)
      • ⛏ Git (11)
      • 🐳 Docker (6)
      • 🐧 Linux (3)
      • 🌈 Jenkins (1)
      • 📬 Kafka (1)
    • 💬 ETC.. (4)
      • 💻 macOS (2)
    • 🌧️ ORM (2)
      • JPA (2)
    • 🐍 Python (2)
    • 📚 Databases (15)
      • 오라클로 배우는 데이터베이스 개론과 실습(2판) (3)
      • RealMySQL 8.0 (8)
    • 🔥 Computer Science (5)
      • 📡 네트워크 (5)
    • 🏷️ 협업 (1)
    • 📜 코딩테스트 (38)
      • BAEKJOON\수학 1, 수학 2 (8)
      • BAEKJOON\재귀 (5)
      • BAEKJOON\브루트 포스 (3)
      • BAEKJOON\정렬 (1)
      • BAEKJOON\백트래킹 (5)
      • BAEKJOON\BFS, DFS (6)
      • BAEKJOON\이분탐색 (1)
      • BAEKJOON\다이나믹 프로그래밍 (9)
      • BAEKJOON\그리디 알고리즘 (0)
    • ✨ ISEUNGHAN (1)

인기 글

최근 글

전체
오늘
어제
반응형
hELLO · Designed By 정상우.
iseunghan
(Deprecated) WebSecurityConfigurerAdapter에 대응하기
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

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

블로그 게시글

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

모든 영역

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

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