강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

qwerty jon님의 프로필 이미지
qwerty jon

작성한 질문수

스프링부트 시큐리티 & JWT 강의

스프링부트 시큐리티 2강 - 시큐리티 설정

코틀린 시큐리티 컨피그 설정방법 참고하세요

작성

·

675

1

코틀린에서 이번강의 설정할땐


import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer
import org.springframework.security.web.SecurityFilterChain

@Configuration
@EnableWebSecurity
class SecurityConfig {
    @Bean
    fun filterChain(http:HttpSecurity):SecurityFilterChain{
        http.csrf(CsrfConfigurer<HttpSecurity>::disable)
        http.authorizeHttpRequests{
            it.requestMatchers("/user/**").authenticated()
            it.requestMatchers("/manager/**").hasAnyRole("MANAGER","ADMIN")
            it.requestMatchers("/admin/**").hasRole("ADMIN")
            it.anyRequest().permitAll()
        }
        http.formLogin{
            it.loginPage("/login")
            it.permitAll()
        }
        return http.build()
    }
}

이런식으로 하시면됩니다

WebSecurityConfigurerAdapter는 사라졌고 대신 빈을 생성해서 등록하는식으로 바뀐거같고

기본적으로 직렬로 메서드체이닝하던게 다 람다식으로 빠졌고(자기자신을 설정하는건 다 내부람다식으로 동작하게 바뀐듯)

authorizeRequests->authorizeHttpRequests

antMatcher->requestMatchers

and().formLogin()->http.formLogin{...}

이렇게 이름이 변경된친구들도 있습니다

답변 1

0

안녕하세요, 인프런 AI 인턴입니다.
qwerty jon님, 코틀린을 사용하여 스프링 시큐리티 설정 방법에 대해 질문해주셨네요.

코틀린에서 스프링부트의 시큐리티 설정은 WebSecurityConfigurerAdapter 대신 SecurityFilterChain 빈을 정의하는 방식으로 대체되는 추세입니다. 여러분이 제공해주신 예시는 코틀린의 DSL(Domain-Specific Language)을 사용한 스프링 시큐리티 설정 방식으로, 기존의 자바 설정 방식과 비교하여 좀 더 간결하고 명확하게 표현될 수 있습니다.

스프링부트 레퍼런스 문서에 따르면, 이런 변화의 일환으로 몇 가지 메서드 이름 변경도 있었습니다. 예를 들어, authorizeRequestsauthorizeHttpRequests로, antMatcherrequestMatchers로 변경되었습니다. 또한 ‘and().formLogin’은 http.formLogin{...}로 바뀌었습니다.

스프링부트가 제공하는 보다 구체적인 예시를 참고하시려면 Spring Boot Reference Documentation에 있는 코드를 확인해보시면 도움이 됩니다. 해당 문서에서는 코틀린과 자바 양쪽 모두의 예시를 제공하고 있어, 코틀린으로 스프링 시큐리티 설정을 구현할 때 유용한 참고 자료가 될 것입니다.

모든 웹 애플리케이션의 보안 설정이 필요한 경우 위의 설정을 참고하여 사용자 정의 SecurityFilterChain을 정의하시면 될 것 같습니다. 추가로 구체적인 설정 방법이나 보안 관련 다른 질문이 있으시면 언제든 문의해주세요.

qwerty jon님의 프로필 이미지
qwerty jon

작성한 질문수

질문하기