sggnology
하늘속에서IT
sggnology
전체 방문자
오늘
어제
  • 분류 전체보기 (83)
    • Algorithm (31)
      • Programmers (27)
      • Baekjoon (4)
    • WIKI (4)
      • VirtualBox (1)
      • Power Toys (1)
    • NodeJS (4)
      • nvm (1)
      • React (1)
      • Vue (1)
    • Dev Language (3)
      • Java (2)
      • Kotlin (1)
    • Spring Boot (17)
      • Gradle (1)
      • JPA (3)
    • DB (4)
      • MariaDB (3)
      • Redis (0)
    • Android (6)
      • Debug (3)
    • Nginx (3)
      • Debug (1)
    • Intellij (0)
    • Network (1)
    • Git (2)
      • GitHub (2)
    • Chrome Extension (0)
    • ETC (5)
      • Monitoring (2)
    • Linux (1)
      • WSL (1)
    • Visual Studio (1)
    • Side Project (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 백준
  • 티스토리챌린지
  • Android Studio
  • 고득점KIT
  • 고득점 Kit
  • 레벨3
  • mariadb
  • JPA
  • 프로그래머스
  • 레벨2
  • spring boot
  • 알고리즘
  • 안드로이드 스튜디오
  • java
  • 오블완
  • DB
  • nginx
  • docker
  • 연습문제
  • kotlin

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
sggnology

하늘속에서IT

Spring Boot

[Spring Boot] Spring Security 설정 과정에서 `please use permitAll via HttpSecurity#authorizeHttpRequests instead` 문구 발생(feat. ignoring)

2024. 5. 28. 15:07
728x90

발단

spring security 를 적용하여 web 의 static resource 에 대한 접근시  권한과 무관하게끔 처리 하기 위한 설정으로 ignoring 을 사용하였는데 제목과 같은 문구가 발생하여 이를 제거하며 최적화한 과정을 정리합니다.

 

환경

  • Spring Boot 3.1 <= .. 
  • Spring Security 6.1 <= ..

# Spring Boot 3.1 버전 이상의 환경에서의 적용을 설명합니다.

 


문제 내용

@Bean
fun webSecurityCustomizer(): WebSecurityCustomizer {
    return WebSecurityCustomizer { web: WebSecurity ->
        web.ignoring().requestMatchers("/api/v1/test1")
    }
}
  • 위 코드는 Web 과 관련된 처리를 Spring 측에서 인터페이스로 제공하고 있어 사용하였습니다.
  • Spring Boot 2 버전에서 사용하던 방식으로 Spring Boot 3 로 마이그레이션 하는 과정에서 그대로 사용하는 방식중 하나였습니다.

 

 

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

 

  • 링크에서 위 코드에 대한 설명이 작성되어있습니다.

 

문제 검색

 

WARN when ignoring antMatchers - please use permitAll · Issue #10938 · spring-projects/spring-security

When I use web.ignoring().antMatchers() I'd like to see a DEBUG message instead of a WARNING for each ignored pattern. I'm confused by the message saying it's not recommended and I should use permi...

github.com

  • 문구로 검색시 위와 같은 github 문서를 찾아볼 수 있었습니다.
  • 간단히 설명해서 아래 내용과 같습니다.
ignoring 사용시 spring security 가 지원하는 보안적 처리가 진행되지 않으니, permitAll 을 사용하는 것이 좋습니다. 

 

이전에는 ignoring 사용시 문구가 없었는데 왜 이제 발생하게 되는걸까?

  • 문제 검색 링크를 살펴보면 ignoring 을 성능 최적화의 방법중 하나로 소개하며 하나의 이슈를 언급하고 있습니다.
  • 해당 이슈에서 세션 사용시 모든 요청에 대해 spring security 를 참조하게 되고 이는 성능적 저하가 발생한다고 하고 있습니다.
  • 즉, 버전이 상승하며 이전에 사용했던 방식보다 나은 방식이 있다고 가르쳐주려 문구가 출력되고 있는 것 입니다.

 

문제 해결

 

시도1: `문제 검색` 링크의 제안으로 처리

  • `문제 검색`의 링크에서는 이런 문구와 함께 해결책을 제시하고 있습니다.
`#10913` 이슈가 해결되는 동안에 spring security 를 사용하면서 성능적 이슈를 해결하고 싶다면 제안해준 코드로 처리하는 게 좋다. 이 방식은 세션으로부터 security context 에 대한 접근 없이 static resource 에 대한 보안을 유지한다.
  • `#10913` 이슈는 spring security 5.8.1 버전에서 처리되었습니다. 그런데 글 초기에 언급하였던 제 환경에서는 `spring security 6.1 <= ..` 을 사용하고 있음으로 `문제 검색`에서 언급한 방식으로 해결할 수 없습니다.

 

시도2: Spring Security 공식 문서 (6.1.10 - SNAPSHOT, Favor of permitAll over ignoring)

 

Authorize HttpServletRequests :: Spring Security

While using a concrete AuthorizationManager is recommended, there are some cases where an expression is necessary, like with or with JSP Taglibs. For that reason, this section will focus on examples from those domains. Given that, let’s cover Spring Secu

docs.spring.io

  • ignoring 사용보보다 permitAll 을 선호하자 라는 소제목으로 문서에서 설명하고 있습니다. 이는 Spring Security 6.1 이상의 문서에서 다루고 있으며 제가 사용하고자 하는 버전과 호한됨으로 이 시도를 통해 해결할 수 있었습니다.

 

해결 코드

/**
 * 설명
 * - default security filter chain
 * - static resource 를 처리
 * */
@Bean
fun resources(http: HttpSecurity): SecurityFilterChain {

    return http
        .authorizeHttpRequests { httpReq ->
            httpReq
                .requestMatchers("/path/to/resource/**").permitAll()
                .requestMatchers(*arrayOf("/path/to/resource/1", "/path/to/resource/2")).permitAll()
        }
        .build()
}

/**
 * 설명
 * - second security filter chain
 * - user 경로에 대해 개별 권한 처리
 * */
@Bean
@Order(1)
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
    return http
        .securityMatcher (*arrayOf("/user/**"))
        .authorizeHttpRequests { httpReq ->
            httpReq
                .requestMatchers("/user/test1").permitAll()
                .requestMatchers("/user/test2").authenticated()
                .requestMatchers("/user/test3").hasRole("USER")
                .anyRequest().permitAll()
        }
        .build()
}
728x90

'Spring Boot' 카테고리의 다른 글

[Spring Boot] Assert 가 동작하지 않는다?(feat. Kotlin)  (0) 2024.11.22
[Spring] Spring Security + MockMvc 로 테스트시 주의할 점(feat. kotlin)  (0) 2024.06.28
[Spring] Custom Enum Value 를 Swagger 문서에 노출시키고 싶으면 어떻게 해야할까?  (0) 2024.05.21
[Spring Boot] Access denied for user 'root'@'localhost'(using password: yes)  (1) 2024.04.24
[Spring Boot] @ModelAttribute 로 맵핑하려는 대상의 depth 가 2 이상일 때 요청하였는데 오류가 난다면?(feat. kotlin)  (0) 2023.10.16
    'Spring Boot' 카테고리의 다른 글
    • [Spring Boot] Assert 가 동작하지 않는다?(feat. Kotlin)
    • [Spring] Spring Security + MockMvc 로 테스트시 주의할 점(feat. kotlin)
    • [Spring] Custom Enum Value 를 Swagger 문서에 노출시키고 싶으면 어떻게 해야할까?
    • [Spring Boot] Access denied for user 'root'@'localhost'(using password: yes)
    sggnology
    sggnology
    하늘은 파란색이니까 내 삶도 파란색이길 ㅎㅎ

    티스토리툴바