SpringBoot- Custom Auto-configuration: EnableAutoConfiguration combination of magical and spring.factories

On some expansion projects, we can not directly operate spring container

By adding in any src directory /META-INF/spring.factoriesfile,
the key is org.springframework.boot.autoconfigure.EnableAutoConfiguration
the value of our written 配置类
as:

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

Here Insert Picture Description

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());
    }
}

Published 501 original articles · won praise 112 · views 20000 +

Guess you like

Origin blog.csdn.net/LawssssCat/article/details/104958546