Spring Security Role Hierarchy not working using Java Config

前端 未结 6 2085
轻奢々
轻奢々 2021-01-04 21:48

First of all, I am new to Java Spring Framework. So forgive me if I did not provide enough info. I have tried to add RoleHierarchy into my app but it did not work. Below are

6条回答
  •  生来不讨喜
    2021-01-04 22:12

    Override the createExpressionHandler method so it returns a configured global expression handler

      @Configuration
      @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
      public class GlobalMethodSecurityConfig extends GlobalMethodSecurityConfiguration {
          @Autowired
          private RoleHierarchy roleHierarchy;
    
          @Override
          protected MethodSecurityExpressionHandler createExpressionHandler(){
              return methodSecurityExpressionHandler();
          }
    
          private DefaultMethodSecurityExpressionHandler methodSecurityExpressionHandler(){
              DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
              expressionHandler.setRoleHierarchy(roleHierarchy);
              return expressionHandler;
          }
    
          @Bean
          public RoleHierarchyImpl roleHierarchy() {
              RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
          roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_OWNER > ROLE_USER");
              return roleHierarchy;
          }
    
          @Bean
          public RoleHierarchyVoter roleVoter() {
              return new RoleHierarchyVoter(roleHierarchy);
          }
    
          @Configuration
          public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
              @Override
              protected void configure(HttpSecurity http) throws Exception {}
          }
      }
    

提交回复
热议问题