Я переопределяю класс GlobalMethodSecurityConfiguration
, но только один метод: protected MethodSecurityExpressionHandler createExpressionHandler()
.
Когда я пытаюсь запустить приложение, я получаю:
Описание:
bean-компонент 'methodSecurityInterceptor', определенный в ресурсе пути к классу [org / springframework / security / config / annotation / method / configuration / GlobalMethodSecurityConfiguration.class], не может быть зарегистрирован.Компонент с таким именем уже определен в ресурсе пути к классу [com / testing / config / MyMethodSecurityConfig.class], и переопределение отключено.
Действие:
Рассмотрите возможность переименования одного из компонентов иливключить переопределение, установив spring.main.allow-bean-definition-overriding = true
CONFIG CLASS
Почему это происходит, когда я не переопределяюэтот базовый метод?Как я могу переопределить 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;
}
}