Java AccessDecisionManager,如何添加RoleVoter
首先,我想问一下,是否可以在Java配置中访问defaultJava AccessDecisionManager,如何添加RoleVoter,java,spring,spring-security,spring-boot,Java,Spring,Spring Security,Spring Boot,首先,我想问一下,是否可以在Java配置中访问defaultAccessDecisionManager(不使用任何xml文件) 其次,我的问题是这样的。我想将RoleVoter添加到我的配置中,但我不知道如何操作 @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfiguration extends WebSecurityConf
AccessDecisionManager
(不使用任何xml文件)
其次,我的问题是这样的。我想将RoleVoter
添加到我的配置中,但我不知道如何操作
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
...
@Bean
public RoleHierarchy roleHierarchy() {
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy("ADMIN > USER");
return roleHierarchy;
}
@Bean
public RoleHierarchyVoter roleHierarchyVoter(RoleHierarchy roleHierarchy){
return new RoleHierarchyVoter(roleHierarchy);
}
我的尝试是通过authorizeRequests().accessDecisionManager(defaultAccessDecisionManager)
将我的AffirmativeBased
管理器bean添加到HttpSecurity
但它在提交投票时失败,因为WebExpressionConfigAttribute
类在getAttribute
方法上总是返回null
编辑:我想我找到了答案。我的尝试并没有错,这里是对defaultAccessDecisionManager
@Bean
public AffirmativeBased defaultAccessDecisionManager(RoleHierarchy roleHierarchy){
WebExpressionVoter webExpressionVoter = new WebExpressionVoter();
DefaultWebSecurityExpressionHandler expressionHandler = new DefaultWebSecurityExpressionHandler();
expressionHandler.setRoleHierarchy(roleHierarchy);
webExpressionVoter.setExpressionHandler(expressionHandler);
return new AffirmativeBased(Arrays.asList((AccessDecisionVoter) webExpressionVoter));
}
但是,我仍然必须将这个defaultAccessDecisionManager
添加到配置中的每个HttpSecurity
对象中。有人知道如何在全球范围内进行吗?http
http
.requestMatchers().antMatchers("/**")
.authorizeRequests()
.antMatchers("/auth/**").permitAll()
.antMatchers("/admin/only").hasRole("ADMIN")
.anyRequest().authenticated()
.withObjectPostProcessor(new ObjectPostProcessor<AffirmativeBased>() {
@Override
public AffirmativeBased postProcess(AffirmativeBased affirmativeBased) {
affirmativeBased.getDecisionVoters().add(0, myAccessDecisionVoter1()); // add before WebExpressionVoter
affirmativeBased.getDecisionVoters().add(myAccessDecisionVoter2()); // add after WebExpressionVoter
return affirmativeBased;
}
});
.requestMatchers().antMatchers(“/**”)
.授权请求()
.antMatchers(“/auth/**”).permitAll()
.antMatchers(“/admin/only”).hasRole(“admin”)
.anyRequest().authenticated()
.withObjectPostProcessor(新的ObjectPostProcessor(){
@凌驾
基于公共确认的后处理(基于确认的基于确认的){
affirmativeBased.getDecisionVoters().add(0,myAccessDecisionVoter1());//在WebExpressionVoters之前添加
affirmativeBased.GetDecisionVorters().add(myAccessDecisionVoter2());//在WebExpressionVorter之后添加
返回基于肯定的;
}
});
这一切看起来都会起作用。创建一个表达式处理程序,添加您的角色层次结构
,然后通过http.authorizeRequests().expressionHandler(…)
将其注入到链中,不是更简单吗?我不认为存在全球违约(可能是错误的)。
http
.requestMatchers().antMatchers("/**")
.authorizeRequests()
.antMatchers("/auth/**").permitAll()
.antMatchers("/admin/only").hasRole("ADMIN")
.anyRequest().authenticated()
.withObjectPostProcessor(new ObjectPostProcessor<AffirmativeBased>() {
@Override
public AffirmativeBased postProcess(AffirmativeBased affirmativeBased) {
affirmativeBased.getDecisionVoters().add(0, myAccessDecisionVoter1()); // add before WebExpressionVoter
affirmativeBased.getDecisionVoters().add(myAccessDecisionVoter2()); // add after WebExpressionVoter
return affirmativeBased;
}
});