Spring boot 如何将Springbean自动关联到Lamba代码中?

Spring boot 如何将Springbean自动关联到Lamba代码中?,spring-boot,spring-security,autowired,Spring Boot,Spring Security,Autowired,我正在为我的公司建立一个SpringSecuritySAML2.0蓝图 我正在学习的课程是。到目前为止一切都很好-示例身份验证过程运行良好 为了遵循我们公司的安全指南,我必须使SAMLConfig.java文件可配置。我的第一个想法是自动连接一个从“某处”加载配置的组件;像下面这样 /** * @author Ulises Bocchio */ @AutoConfigureBefore(WebSecurityConfig.class) @Configuration public class SA

我正在为我的公司建立一个SpringSecuritySAML2.0蓝图

我正在学习的课程是。到目前为止一切都很好-示例身份验证过程运行良好

为了遵循我们公司的安全指南,我必须使SAMLConfig.java文件可配置。我的第一个想法是自动连接一个从“某处”加载配置的组件;像下面这样

/**
* @author Ulises Bocchio
*/
@AutoConfigureBefore(WebSecurityConfig.class)
@Configuration
public class SAMLConfig {
  protected Logger log = LoggerFactory.getLogger(this.getClass());

  @Autowired
  private SAMLUserDetailsServiceImpl samlUserDetailsServiceImpl;


  @Autowired
  private SAMLProperties samlProperties;

  @Bean
  public SAMLAuthenticationProvider samlAuthenticationProvider() {
    final SAMLAuthenticationProvider provider = new SAMLAuthenticationProvider();
    provider.setUserDetails(this.samlUserDetailsServiceImpl);
    provider.setForcePrincipalAsString(false);
    return provider;
  }
这将不起作用,因为SamlProperty对象为null。我不确定,但是否可能,spring安全性是在其他(应用程序)组件之前启动的


如何实现SAMLConfig.java文件的动态配置?

一位同事向我暗示了以下信息-日志行:

@Bean method SAMLConfig.idpMetadataLoader is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
Ulises Bocchio对idpMetadataLoader方法的修改版本:

@Bean
BeanFactoryPostProcessor idpMetadataLoader(StaticBasicParserPool parserPool) {
return beanFactory -> {
  try {
    final Resource idpResource = new UrlResource(this.samlProperties.getIdpMetadataUrl());
    final String alias = this.samlProperties.getKeystoreAlias();
    final Timer refreshTimer = new Timer(true);

    final ResourceBackedMetadataProvider delegate =
        new ResourceBackedMetadataProvider(refreshTimer, new SpringResourceWrapperOpenSAMLResource(idpResource));
    delegate.setParserPool(parserPool);
    final ExtendedMetadata extendedMetadata = this.extendedMetadata().clone();
    final ExtendedMetadataDelegate provider = new ExtendedMetadataDelegate(delegate, extendedMetadata);
    provider.setMetadataTrustCheck(true);
    provider.setMetadataRequireSignature(false);
    extendedMetadata.setAlias(alias);
    beanFactory.registerSingleton(alias, provider);
  } catch (final Exception e) {
    this.log.error("Error while confiure SAML", e);
    e.printStackTrace();
  }
 };
}

关键是,lambda代码是在静态代码中转换的,因此不能访问非静态类成员。非常简单的解决方案是将配置作为方法参数交付

  @Bean
  BeanFactoryPostProcessor idpMetadataLoader(SAMLConfigFile properties, StaticBasicParserPool parserPool) {
    return beanFactory -> {
  ...
  };
}

看起来您使用的是旧版本的SpringSecuritySAML,它依赖于OpenSAML2,不再维护。我要更新到最新的SAML支持,它使用OpenSAML 3,它仍然保持着,它还可以与引导一起工作,并且支持更容易地外部化配置。谢谢你提供的信息。这不是原因,但我会很快更新库。