在一些扩展项目上,我们无法直接操作 spring 容器

可以通过 在任意src目录下添加 /META-INF/spring.factories 文件,
键为 org.springframework.boot.autoconfigure.EnableAutoConfiguration
值 写入我们的 配置类
如:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.cas.CustomAuthenticationConfiguration

package cn.cas;

import org.apereo.cas.authentication.AuthenticationEventExecutionPlan;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlanConfigurer;
import org.apereo.cas.authentication.AuthenticationHandler;
import org.apereo.cas.authentication.principal.DefaultPrincipalFactory;
import org.apereo.cas.authentication.principal.PrincipalFactory;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.services.ServicesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** * @author lawsssscat */
@Configuration
@EnableConfigurationProperties(CasConfigurationProperties.class) // 这里用不到
public class CustomAuthenticationConfiguration implements AuthenticationEventExecutionPlanConfigurer {

    @Autowired
    @Qualifier("servicesManager") // 必须指定,否则找不到
    private ServicesManager servicesManager;

    // 验证器交给Spring管理
    @Bean
    public AuthenticationHandler customerAuthenticationHandler() {
        String name = CustomerAuthenticationHandler.class.getName();
        ServicesManager servicesManager = this.servicesManager;
        PrincipalFactory principalFactory = new DefaultPrincipalFactory();
        // 定义为优先优先使用
        Integer order = 1;
        return new CustomerAuthenticationHandler(name,servicesManager, principalFactory, order) ;
    }

    // 注册自定义验证器
    @Override
    public void configureAuthenticationExecutionPlan(AuthenticationEventExecutionPlan plan) {
        plan.registerAuthenticationHandler(customerAuthenticationHandler());
    }
}