개발/Spring

Spring Security 6 변경점

Ridiss 2023. 1. 25. 16:43

spring security 6 (spring boot 3) 사용시 이전버전과 달라진 점

 

1. mvcMatchers deprecated

 - 대신 requestMatchers를 사용

 

2. @Configuration 추가해야함

 - 기존에는 @EnableWebSecurity 내부에 @Configuration이 있었으나 6버전부터 제거되어서 sequrity config class에 아래와 같이 추가해야한다.

@Configuration
@EnableWebSecurity
class SecurityConfig {
}

 

3. .access() 사용방법 변경

기존

.antMatchers("/admin").access("hasRole('ADMIN') or hasRole('SYS')")

변경

.requestMatchers("/admin").access(new WebExpressionAuthorizationManager("hasRole('ADMIN') or hasRole('SYS')"))

 

4. inMemory유저 생성

기존

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("{noop}1111").roles("USER"); // 인메모리 유저 생성
        auth.inMemoryAuthentication().withUser("sys").password("{noop}1111").roles("SYS"); // 인메모리 유저 생성
        auth.inMemoryAuthentication().withUser("admin").password("{noop}1111").roles("ADMIN"); // 인메모리 유저 생성
    }

변경

    @Bean
    public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) {
        UserDetails user = User.withUsername("user")
                .password(passwordEncoder.encode("1111"))
                .roles("USER")
                .build();

        UserDetails sys = User.withUsername("sys")
                .password(passwordEncoder.encode("1111"))
                .roles("SYS")
                .build();

        UserDetails admin = User.withUsername("admin")
                .password(passwordEncoder.encode("1111"))
                .roles("ADMIN")
                .build();

        return new InMemoryUserDetailsManager(user, sys, admin);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        return encoder;
    }

'개발 > Spring' 카테고리의 다른 글

Webclient 한글깨짐 문제 해결  (0) 2023.03.10
SPRING-Security SessionManagement(세션 관리 기능)  (0) 2023.01.23
Spring-IOC  (0) 2022.03.01
@Component, @Configuration 차이  (0) 2021.08.04
[JPA] Page, nativeQuery사용  (0) 2021.07.13