获取错误,指出在 SPring Boot 2.1.1 中扩展 GlobalMethodSecurityConfiguration 时已定义“methodSecurityInterceptor”

我正在重写类,但只有一种方法:.GlobalMethodSecurityConfigurationprotected MethodSecurityExpressionHandler createExpressionHandler()

当我尝试运行该应用程序时,我得到:

描述:

在类路径资源 [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class] 中定义的 bean 'methodSecurityInterceptor',无法注册。具有该名称的 Bean 已在类路径资源 [com/testing/config/MyMethodSecurityConfig.class] 中定义,并且已禁用重写。

行动:

考虑重命名其中一个 bean 或通过设置 spring.main.allow-bean-definition-overovering=true 来启用覆盖

配置类

为什么当我不覆盖该基本方法时,它会这样做?我如何覆盖而不得到这个错误?MethodSecurityExpressionHandler

import com.testing.AadMethodSecurityExpressionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class MyMethodSecurityConfig extends GlobalMethodSecurityConfiguration
{
    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler()
    {
        return new MyMethodSecurityExpressionHandler();
    }
}

表达式处理程序

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations;
import org.springframework.security.core.Authentication;

public class MyMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler
{
    @Override
    protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, MethodInvocation invocation)
    {
        MyMethodSecurityExpressionRoot root = new MyMethodSecurityExpressionRoot( authentication );
        root.setPermissionEvaluator( getPermissionEvaluator() );
        root.setTrustResolver( getTrustResolver() );
        root.setRoleHierarchy( getRoleHierarchy() );

        return root;
    }
}

表达式根

import org.springframework.security.access.expression.SecurityExpressionRoot;
import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations;
import org.springframework.security.core.Authentication;

public class MyMethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations
{
    private Object filterObject;
    private Object returnObject;
    private Object target;

    public MyMethodSecurityExpressionRoot(Authentication a)
    {
        super( a );
    }

    @Override
    public void setDefaultRolePrefix(String defaultRolePrefix)
    {
        //Simple test to see if this works
        super.setDefaultRolePrefix( "" );
    }

    public void setFilterObject(Object filterObject)
    {
        this.filterObject = filterObject;
    }

    public Object getFilterObject()
    {
        return filterObject;
    }

    public void setReturnObject(Object returnObject)
    {
        this.returnObject = returnObject;
    }

    public Object getReturnObject()
    {
        return returnObject;
    }

    void setThis(Object target)
    {
        this.target = target;
    }

    public Object getThis()
    {
        return target;
    }
}

答案 1

对于任何为我遇到此问题的人来说,解决方案是删除我在设置的WebSecurityConfigurer上配置的重复@EnableGlobalMethodSecurity注释。


答案 2

我通过合并两个配置类来解决

@EnableWebSecurity
public class SecurityConfig {

    @Configuration
    @RequiredArgsConstructor
    @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
    public static class GlobalMethodSecurityConfig extends GlobalMethodSecurityConfiguration {
        
        private final ApplicationContext applicationContext;

        @Override
        protected MethodSecurityExpressionHandler createExpressionHandler() {
            var expressionHandler = new CustomMethodSecurityExpressionHandler();
            expressionHandler.setApplicationContext(applicationContext);

            return expressionHandler;
        }
    }

    @Configuration
    public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //http config
        }

    }
}

正如春季文档中的建议