[Spring Security] 3.0.0버전에서스프링 시큐리티 설정하기

PersesTitan·2022년 12월 7일
1

Spring

목록 보기
41/48

SpringBoot가 3.0.0 버전으로 만들면서 시큐리티에 형태가 많이 달라져서 열심히 spring 공식 사이트를 찾아가보면서 구현을 하였습니다.

이제 config는 Bean으로 등록하면 됩니다.

방법은 여러가지가 있으므로 원하는시는 방법으로 구현하시면됩니다.
우선 제가 구현한 방법입니다.
자꾸 /login페이지로 넘어가서 동작확인이 안돼니깐 사실상 시큐리티 설치하면 config세팅하는게 필수...

코드

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.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    private static final String[] AUTH_WHITELIST = {
            "/", "/user/**"
    };

    @Bean
    protected SecurityFilterChain config(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(authorize -> authorize
                        .shouldFilterAllDispatcherTypes(false)
                        .requestMatchers(AUTH_WHITELIST)
                        .permitAll()
                        .anyRequest()
                        .authenticated())
                .build();
    }
}

이렇게 설정해주면 '/'와 일치하거나 '/user/'로 시작하는 url은 모두 권한 없이 들어갈 수 있게 되었습니다.

테스트

허용된 url

허용된 url은 잘 들어가지는 모습을 볼 수 있습니다.

허용하지 않은 url

허용하지 않는 url에 접속하게 된다면 403에러가 발생합니다.
403: 서버가 클라이언트의 접근을 거부할때

참고한 사이트 링크

profile
안녕하세요 페르세스 티탄입니다! 부족한 부분이 많이 있겠지만 잘부탁드립니다.

0개의 댓글