스프링/스프링 시큐리티

Role Hierarchy (계층 권한)

까마귀! 2024. 3. 23. 22:59

 

C>B>A 이 순으로 권한이 있었으면 좋겠다

 

 

SecurityConfig
// 계층 권한 등록
@Bean
public RoleHierarchy roleHierarchy() {

    RoleHierarchyImpl hierarchy = new RoleHierarchyImpl();

    hierarchy.setHierarchy("ROLE_C > ROLE_B\n" +
            "ROLE_B > ROLE_A" );

    return hierarchy;
}

 

 

 

 

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{

    //Role Hierarchy 예시 c>b>a
    http
            .authorizeHttpRequests((auth) -> auth
                    .requestMatchers("/login").permitAll()
                    .requestMatchers("/").hasAnyRole("A") // A,B,C, 모두 접근 가능
                    .requestMatchers("/manager").hasAnyRole("B") // B,C 접근 가능
                    .requestMatchers("/admin").hasAnyRole("C") C만 접근 가능
                    .anyRequest().authenticated()
            );

 

'스프링 > 스프링 시큐리티' 카테고리의 다른 글

Http Basic  (0) 2024.03.23
InMemory  (0) 2024.03.23
csrf 적용  (0) 2024.03.23
세션 설정  (0) 2024.03.23
로그인 검증 로직  (0) 2024.03.23