添加过滤器或以其他方式不会调用自定义AccessDecisionManager。理想情况下,希望设置filterBefore和自定义AccessDecisionManager(使用SpringBoot 1.5.2-release版本)。另外,想在默认RoleVoter上调用setRolePrefix。还添加了DefaultRolesPrefixPostProcessor,如Spring 3 to 4迁移指南的8.3节中所述,但RoleVoter仍在寻找“ROLE_”前缀

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    AuthenticationFilter authenticationFilter;

    @Bean
    public AccessDecisionManager accessDecisionManager() {
        List<AccessDecisionVoter<? extends Object>> decisionVoters
          = Arrays.asList(
            new WebExpressionVoter(),
            new RoleVoter(),
            new AuthenticatedVoter());
        return new AffirmativeBased(decisionVoters);
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests().accessDecisionManager(accessDecisionManager())
        .anyRequest().authenticated();

        http.addFilterBefore(authenticationFilter, BasicAuthenticationFilter.class);
    }
}

最佳答案

似乎您希望调用AccessDecisionManager来授予/拒绝对安全方法的访问。

请尝试以下操作:

  • @EnableGlobalMethodSecurity中删除SecurityConfig批注。
  • 将注释移至扩展了GlobalMethodSecurityConfiguration的另一个配置类。

  • 例如:
    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
    public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
        @Autowired
        AccessDecisionManager accessDecisionManager;
    
        @Override
        protected AccessDecisionManager accessDecisionManager() {
            return this.accessDecisionManager;
        }
    
    }
    

    说明:
    GlobalMethodSecurityConfiguration负责创建方法拦截器,并且它不查找要使用的AccessDecisionManager bean。必须通过重写方法提供它。

    注意:
    默认情况下,使用两种 AccessDecisionManagers:一种用于过滤器(由AbstractInterceptUrlConfigurer创建),另一种用于保护方法(由GlobalMethodSecurityConfiguration创建)。

    或者想在默认RoleVoter上调用setRolePrefix

    您无需触摸默认的AccessDecisionManager即可执行此操作:
    @Bean
    public GrantedAuthorityDefaults grantedAuthorityDefaults() {
        return new GrantedAuthorityDefaults("");
    }
    

    这会将角色前缀设置为默认""上的AccessDecisionManagers

    10-07 19:14