-
security버전스프링 2023. 1. 15. 15:45
WebSecurityConfigurerAdapter가 사용할 수 없게 되어 다음과 같은 방식을 사용해야 한다.
@Configuration @EnableWebSecurity public class SecurityConfig{ @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.csrf().disable(); http.authorizeRequests() .antMatchers("/user/**").authenticated() .antMatchers("/manager/**").access("hasAnyRole('ROLE_MANAGER','ROLE_ADMIN')") .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .anyRequest().permitAll(); return http.build(); } }
버전 업데이트에 따라 Bean으로 등록하고 사용해야 한다,.
이때 HttpSecurity가 authorizeRequests()를 deprecate 해서 이 또한 변경해야 한다.
@Configuration @EnableWebSecurity // 스프링 시큐리티 필터가 스프링 필터체인에 등록이 됨 public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.csrf().disable(); http.authorizeHttpRequests() .requestMatchers("/user/**").authenticated() .requestMatchers("/manager/**").hasAnyRole("MANAGER", "ADMIN") .requestMatchers("/admin/**").hasRole("ADMIN") .anyRequest().permitAll() .and().formLogin().loginPage("/login"); return http.build(); } }
변경사항
.authorizeRequests() → .authorizeHttpRequests()
.antMatchers() → .requestMatchers()
.access("hasAnyRole('ROLE_A','ROLE_B')") → .hasAnyRole("A", "B")728x90'스프링' 카테고리의 다른 글
OAuth2.0과 JWT적용 - 2 (0) 2023.01.17 OAuth2.0과 JWT적용 - 1 (0) 2023.01.17 [Test] Mockito 기반의 Spring 단위 테스트 (0) 2023.01.12 [Test] 단위테스트 (0) 2023.01.12 Swagger이용하기2 (0) 2023.01.09