我想在此类中使用该存储库,但是当我放置类似@Component的构造型时,会从IDE中收到错误消息:


  无法自动接线。找不到“身份验证”类型的bean。


public class CustomMethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {

    @Autowired
    private FlatRepository flatRepository;

    public CustomMethodSecurityExpressionRoot(Authentication authentication) {
        super(authentication);
     }
}

最佳答案

您不能在@AutowireSecurityExpressionRoot
但是,您可以手动提供该FlatRepository依赖项。

@Configuration类中配置Security对象时,可以在其中@Autowire所需的任何实例。

只需在CustomMethodSecurityExpressionRoot构造函数中为该新依赖项腾出空间

class CustomMethodSecurityExpressionRoot extends SecurityExpressionRoot
                                         implements MethodSecurityExpressionOperations {
    private final FlatRepository flatRepository;

    CustomMethodSecurityExpressionRoot(
            final Authentication authentication,
            final FlatRepository flatRepository) {
        super(authentication);
        this.flatRepository = flatRepository;
    }

    ...
}


并在实例化点手动注入

final SecurityExpressionRoot root = new CustomMethodSecurityExpressionRoot(authentication, flatRepository);

07-27 17:16