오류 상황
DB에 manager, admin 권한 설정을 저장한 후 해당 회원 계정으로 로그인을 했는데, 각각 manager, admin 전용 페이지에 연결되지 않고 403 오류가 발생하였다.
SecurityConfig 코드를 확인해 보았다.
//권한 부여
http.authorizeHttpRequests(authorize -> {
authorize
.requestMatchers("/user/**").authenticated()
.requestMatchers("/manager/**").hasAnyRole("hasRole('ROLE_MANAGER') or hasRole('ROLE_ADMIN')")
.requestMatchers("/admin/**").hasAnyRole("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll();
});
얼핏 보기엔 문제가 없어보였다..
그래서 먼저 든 생각은 스프링 시큐리티 설정에 대한 문법 같은 게 바뀌었나? 였다.
다음과 같이 수정해 보았다.
//권한 부여
http.authorizeHttpRequests(authorize -> {
authorize
.requestMatchers("/user/**").authenticated()
.requestMatchers("/manager/**").hasAnyRole("ROLE_MANAGER", "ROLE_ADMIN")
.requestMatchers("/admin/**").hasAnyRole("ROLE_ADMIN")
.anyRequest().permitAll();
});
그랬더니 반갑게도 컴파일 오류가 발생하였다.!
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration':
Unsatisfied dependency expressed through method 'setFilterChains' parameter 0: Error creating bean with name 'filterChain' defined in class path resource [spring/securitybasicv1/config/SecurityConfig.class]:
Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'filterChain' threw exception with message:
ROLE_MANAGER should not start with ROLE_ since ROLE_ is automatically prepended when using hasAnyRole. Consider using hasAnyAuthority instead.
맨 아랫줄이 핵심인데, 정확하진 않지만 대충 해석해 보니 다음과 같았다.
ROLE_MANAGER는 ROLE_로 시작해선 안 된다. ROLE_은 자동적으로 hasAnyRole을 사용할 때 추가된다.
대신에 hasAnyAutority를 사용하는 것을 고려하자.
이 오류 메시지를 보고 hasAnyRole(”ROLE_MANAGER”, …) 에서 ROLE_만 빼서 실행해 보았다.
정상적으로 동작하는 것을 볼 수 있었다. [해결 완료]
admin 계정으로 manager 페이지도 갈 수 있는 걸 보니 권한을 두 개 이상 설정하면 자동으로 or 조건 처리가 되는 것 같다.
'Trouble Shooting' 카테고리의 다른 글
[Spring Security] JWT 토큰이 계속 헤더에서 사라지는 오류 (임시(?) 해결) (0) | 2024.06.26 |
---|---|
[Spring Boot 프로젝트] 주문 중 재고 부족 오류 (0) | 2024.05.24 |
[EC2] SSH - Host key verification failed. (0) | 2024.05.20 |
[Java, Spring] 주문 로직 중 NullPointerException 해결 (0) | 2024.05.19 |