Spring security xml到java配置,如何实现自定义securityMetataSource

Spring security xml到java配置,如何实现自定义securityMetataSource,spring-security,Spring Security,因此,在我向javaconfig风格的spring安全性迁移的过程中,我遇到了一个关于自定义securityMetadataSource的问题。它似乎不会自动获取,我也不确定如何将其包含在http安全向导中。因为我们有多个站点配置,可以有各种安全配置,所以我们将安全性保留在单独的特定于站点的配置类中 @Bean DefaultFilterInvocationSecurityMetadataSource securityMetadataSource(){ SecurityExpr

因此,在我向javaconfig风格的spring安全性迁移的过程中,我遇到了一个关于自定义securityMetadataSource的问题。它似乎不会自动获取,我也不确定如何将其包含在http安全向导中。因为我们有多个站点配置,可以有各种安全配置,所以我们将安全性保留在单独的特定于站点的配置类中

  @Bean
  DefaultFilterInvocationSecurityMetadataSource securityMetadataSource(){

    SecurityExpressionHandler<FilterInvocation> securityExpressionHandler = new DefaultWebSecurityExpressionHandler();

    LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> map = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>();
    processSecurityPath(map,"/signin/**","permitAll");
    processSecurityPath(map,"/redirect/**","permitAll");

    processSecurityPath(map,"/*/*/account/g_product/add*/**" ,"hasAnyRole('ROLE_ADMIN','ROLE_EDITOR')");

    processSecurityPath(map,"/**" ,"permitAll");    

    ExpressionBasedFilterInvocationSecurityMetadataSource ms = new ExpressionBasedFilterInvocationSecurityMetadataSource(map, securityExpressionHandler);

    return ms;
}

private void processSecurityPath(LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> map, String path, String securityRule){
    map.put(new AntPathRequestMatcher(path), Arrays.<ConfigAttribute>asList(new SecurityConfig(securityRule)));
}

您不能使用自定义SecurityMetadatSource,因为authorizeRequests提供特定于所用实现的自定义dsl。相反,您应该避免使用authorizeRequests,而是创建一个自定义FilterSecurityInterceptor,并使用http.addFilter添加它(正如您所做的那样)。您可能正在使用授权请求覆盖配置。我很好奇为什么你觉得你不能使用授权请求。嗯,我可能只是没有正确理解它,但我有一个中心的spring安全配置类。另一方面,在同一个代码库中,我有多个潜在的“特定于应用程序”的配置类,它们具有不同的安全需求。因此,我使用securityMetadataSource。我想我只是不知道如何使用授权请求如果路径规范是可变的,您可以将变量注入到安全配置中,然后使用这些变量创建映射。这有用吗?如果没有,请更新您的问题以包括您正在寻找的内容。您能给出一个应用这些变量的示例吗?路径的数量和路径本身都随配置的不同而不同。我的理解正确吗?如果我按访问角色对ant路径进行分组,我可以这样做吗。antMatchers(“路径1”、“路径2”)。hasRole(“hasRole('ROLE_ENDUSER'))这种方式有点不方便,但我想是可行的。我更愿意输入我的securityDataMetaSource并完成它。而且,我真的不知道如何才能使这个通用化,因为我必须循环我的securitymetadatasource的设置?
 @Override
 protected void configure(HttpSecurity http) throws Exception {
  http
    .addFilter(myFilterSecurityInterceptor)
    .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class)
    .exceptionHandling().accessDeniedHandler(new MyAccessDeniedHandler())
      .and()
    .authorizeRequests()
    //                  .anyRequest().authenticated() // this should be handled by the DefaultFilterInvocationSecurityMetadataSource that is part of the project specific config
    [...]