개발자 키우기

SecurityFilterChain hasRole 설정 본문

Back-end/Security

SecurityFilterChain hasRole 설정

개발자 키우기 2023. 8. 9. 16:39
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
   http
      .authorizeHttpRequests((authorize) -> authorize
      .antMatchers("/admin").hasRole("ADMIN")
      .anyRequest().authenticated()
   )
   .formLogin();
return http.build();
}

 

hasRole 설정을 하면 스프링이 접두사로 ROLE_ 를 자동적으로 붙여준다

 

만약에 hasRole("ROLE_ADMIN") 을 지정하고 컴파일하면 컴파일 에러가 뜨면서 IllegalArgumentException 이 발생한다

 

오류 내용은 아래와 같다

 

ROLE_ADMIN should not start with ROLE_ since ROLE_ is automatically prepended when using hasRole. Consider using hasAuthority instead.

 

앞에 살펴본 것과 같이 알아서 붙여주니 ROLE_ 을 때라는 소리다

 

 

# 참고로 User를 DB에 저장할때는 ROLE_ 을 붙여줘야 한다

'Back-end > Security' 카테고리의 다른 글

resources 자원 필터 거치지 않게 하기  (0) 2023.08.09
대칭키와 비대칭키 메커니즘  (0) 2023.05.19