自定义 AccessDecisionManager 不会在添加过滤器时或其他情况下被调用。理想情况下希望设置 filterBefore 和自定义 AccessDecisionManager(使用 SpringBoot 1.5.2-release 版本)。或者想在默认 RoleVoter 上调用 setRolePrefix。还添加了 DefaultRolesPrefixPostProcessor,如 Spring 3 至 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
以授予/拒绝对您的安全方法的访问权限。
尝试以下操作:
从
SecurityConfig
中删除@EnableGlobalMethodSecurity
注释。将注解移动到另一个扩展
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< 创建)/
)。
Alternately would like to call setRolePrefix on default RoleVoter
您可以在不触及默认的 AccessDecisionManager
s 的情况下执行此操作:
@Bean
public GrantedAuthorityDefaults grantedAuthorityDefaults() {
return new GrantedAuthorityDefaults("");
}
这将在两个 默认 AccessDecisionManagers
上将角色前缀设置为 ""
。
关于java - 将自定义 AccessDecisionManager 与 HttpSecurity addFilterBefore 一起使用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43684355/