Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AccessDecisionManager,如何添加RoleVoter_Java_Spring_Spring Security_Spring Boot - Fatal编程技术网

Java AccessDecisionManager,如何添加RoleVoter

Java 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

首先,我想问一下,是否可以在Java配置中访问default
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;
        }
    });