【问题标题】:Why my custom PermissionEvaluator isn't invoked?为什么我的自定义 PermissionEvaluator 没有被调用?
【发布时间】:2016-10-11 06:15:34
【问题描述】:

我正在为我的 Spring Security 配置而苦苦挣扎,到目前为止我无法使其正常工作。 我不知道为什么我的自定义 PermissionEvaluator 没有被调用,并且我使用 hasPermission 表达式的 @PreAuthorize 注释被忽略了。

我正在使用 Spring 4.2.4 和 Spring security 4.1.0

她是我的密码:

网络安全配置

@Configuration
@EnableWebSecurity
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http //
                .addFilterBefore(wafflePreAuthFilter(), AbstractPreAuthenticatedProcessingFilter.class) //
                .authenticationProvider(preauthAuthProvider()) //
                .csrf().disable() //
                .authorizeRequests() //
                .antMatchers("/ui/**").authenticated() //
                .anyRequest().permitAll();
    }

    @Bean
    public WafflePreAuthFilter wafflePreAuthFilter() throws Exception {
        WafflePreAuthFilter filter = new WafflePreAuthFilter();
        filter.setAuthenticationManager(authenticationManager());
        return filter;
    }

    @Bean
    public PreAuthenticatedAuthenticationProvider preauthAuthProvider() {
        PreAuthenticatedAuthenticationProvider preauthAuthProvider = new PreAuthenticatedAuthenticationProvider();
        preauthAuthProvider.setPreAuthenticatedUserDetailsService(userDetailsServiceWrapper());
        return preauthAuthProvider;
    }

    @Bean
    public UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> userDetailsServiceWrapper() {
        UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> wrapper = new UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken>();
        wrapper.setUserDetailsService(myUserDetailsService());
        return wrapper;
    }

    @Bean
    public UserDetailsService myUserDetailsService() {
        return new myUserDetailsService();
    }
}

方法安全配置

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = true)
public class MyServiceMethodSecurityConfig extends GlobalMethodSecurityConfiguration {
    @Bean
    public PermissionEvaluator myPermissionEvaluator() {
        return new DcePermissionEvaluator();
    }

    @Override
    public MethodSecurityExpressionHandler createExpressionHandler() {
        DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
        expressionHandler.setPermissionEvaluator(myPermissionEvaluator());
        return expressionHandler;
    }
}

权限评估器

public class MyPermissionEvaluator implements PermissionEvaluator {
    @Autowired
    private MyService myAutowiredService;

    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
        // checking permissions
        return true;
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
        // checking permissions
        return true;
    }
}

任何人都可以给我一个提示,告诉我该怎么做?

顺便说一句,如果我将 MyServiceMethodSecurityConfig 更改为此,则处理 myPermissionEvaluator 但依赖注入不起作用,因为它不是由 Spring 管理的:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = false)
public class MyServiceMethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    @Override
    public MethodSecurityExpressionHandler createExpressionHandler() {
        DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
        expressionHandler.setPermissionEvaluator(new DcePermissionEvaluator());
        return expressionHandler;
    }
}

【问题讨论】:

  • 这方面有什么进展吗?

标签: java spring spring-security


【解决方案1】:

我遇到了这个问题。这似乎是由于在多个地方指定了注释 @EnableGlobalMethodSecurity 引起的。

一旦我将它从我的 GlobalMethodSecurityConfiguration 实现之外的位置删除,一切就开始按预期工作了。

【讨论】:

  • 非常感谢,我遇到了同样的问题,我花了好几个小时才找到解决方案。删除除一个之外的所有 @EnableGlobalMethodSecurity 注释可以解决问题。有趣的是,它在 linux 下运行时可以使用多个注释,但在 windows 下则不行。
  • 谢谢,如果没有找到您的答案,我想我会花更多时间。 @Alan47 顺便说一句,我的情况是它在 Mac 中重现,但不是在 Windows 下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
相关资源
最近更新 更多