簡體   English   中英

Spring Security 3.2-配置全局方法安全性以使用角色層次結構

[英]Spring Security 3.2 - configuring global method security to use role hierarchy

使用Spring Security 3.2.5和Spring 4.1.2,100%Java配置

我們的web應用啟用了全局方法安全性,並使用@PreAuthorize注釋服務方法-一切都按預期工作。 我正在嘗試添加角色層次結構,但完全沒有成功。 這是我要實現的層次結構:

  • ROLE_ADMIN可以訪問ROLE_USER可以訪問的所有方法。
  • ROLE_USER可以訪問ROLE_DEFAULT可以訪問的所有方法。

盡管我已盡力而為,但使用ROLE_ADMIN的用戶在執行某些操作時會收到403,該調用導致以@PreAuthorized("hasAuthority('ROLE_DEFAULT')")注釋的方法的調用

這是相關的配置代碼:

AppInitializer

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
  @Override
  protected Class<?>[] getRootConfigClasses()
  {
    return new Class[]
    {
      AppConfig.class, SecurityConfig.class
    };
  }

  @Override
  protected Class<?>[] getServletConfigClasses()
  {
    return new Class[]
    {
      MvcConfig.class
    };
  }
  // other methods not shown for brevity
}

AppConfig.java

@Configuration
@ComponentScan(basePackages={"myapp.config.profile", "myapp.dao", "myapp.service", "myapp.security"})
public class AppConfig
{
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth,
                              AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> detailSvc) throws Exception
  {
    PreAuthenticatedAuthenticationProvider authProvider = new PreAuthenticatedAuthenticationProvider();
    authProvider.setPreAuthenticatedUserDetailsService(detailSvc);
    auth.authenticationProvider(authProvider);
  }
  // other methods not shown for brevity
}

SecurityConfig.java

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
  @Override
  protected void configure(HttpSecurity http) throws Exception
  {
    PKIAuthenticationFilter pkiFilter = new PKIAuthenticationFilter();
    pkiFilter.setAuthenticationManager(authenticationManagerBean());

    http.authorizeRequests()
        .antMatchers("/app/**").fullyAuthenticated()
        .and()
        .anonymous().disable()
        .jee().disable()
        .formLogin().disable()
        .csrf().disable()
        .x509().disable()
        .addFilter(pkiFilter)
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  }

  @Override
  public void configure(WebSecurity web) throws Exception
  {
    // ignore everything but /app/*
    web.ignoring().regexMatchers("^(?!/app/).*");
  }
}

MvcConfig.java

@Configuration
@EnableWebMvc
@ComponentScan({"myapp.controller"})
public class MvcConfig extends WebMvcConfigurerAdapter
{
  // resource handlers, content negotiation, message converters configured here
}

在與SecurityConfig相同的程序包中(因此它是AppConfig組件掃描的一部分),我擁有此類:

GlobalMethodSecurityConfig.java

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class GlobalMethodSecurityConfig extends GlobalMethodSecurityConfiguration
{
  @Bean
  public RoleHierarchy roleHierarchy()
  {
    RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
    roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER > ROLE_DEFAULT");
    return roleHierarchy;
  }

  @Bean
  public RoleVoter roleVoter()
  {
    return new RoleHierarchyVoter(roleHierarchy);
  }

  @Bean
  @Override
  protected AccessDecisionManager accessDecisionManager()
  {
    return new AffirmativeBased(Arrays.asList(roleVoter()));
  }

  // The method below was added in an attempt to get things working but it is never called
  @Override
  protected MethodSecurityExpressionHandler createExpressionHandler()
  {
    DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler();
    handler.setRoleHierarchy(roleHierarchy());
    return handler;
  }
}

在另一個嘗試中,我使AppConfig擴展了GlobalMethodSecurityConfiguration但是具有ROLE_ADMIN的用戶無法調用需要ROLE_DEFAULT訪問的方法。

我確定我在某處配置了錯誤,但是盡管閱讀了有關使用角色層次結構配置全局方法安全性的所有信息,但我無法弄清楚哪里出了問題。 使用XML配置,這似乎是微不足道的,但是Java配置解決方案使我難以理解。

我剛剛經歷了所有這些設置,並在這里回答了相同的問題: Spring Boot + Spring Security + Hierarchical Roles

希望這可以幫助。

由於這個問題不斷引起人們的關注,我想我應該對此進行后續跟蹤。 問題似乎出在生產線上

roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER > ROLE_DEFAULT");

我不記得為什么要這樣寫層次結構,但這是不正確的。 方法API因此可以處理相同的情況:

角色層次結構: ROLE_A> ROLE_B和ROLE_B> ROLE_C。
直接分配的權限: ROLE_A。
可達權限: ROLE_A,ROLE_B,ROLE_C。

最終,很明顯,分層模型不適合我們的角色,因此我們實現了映射到角色的一組更細粒度的權限,如Spring Security Reference中所述

對於更復雜的要求,您可能希望在應用程序需要的特定訪問權限與分配給用戶的角色之間定義邏輯映射,並在加載用戶信息時在兩者之間進行轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM