All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.lingkang.finalsecurity.solonplugin.FinalSecurityPlugin Maven / Gradle / Ivy

package top.lingkang.finalsecurity.solonplugin;

import org.noear.solon.core.AopContext;
import org.noear.solon.core.Plugin;
import org.noear.solon.core.handle.Filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLogger;
import top.lingkang.finalsecurity.common.annotation.FinalCheck;
import top.lingkang.finalsecurity.common.annotation.FinalCheckLogin;
import top.lingkang.finalsecurity.common.base.FinalHttpProperties;
import top.lingkang.finalsecurity.solonplugin.annotation.impl.FinalCheckAnnotation;
import top.lingkang.finalsecurity.solonplugin.annotation.impl.FinalCheckLoginAnnotation;
import top.lingkang.finalsecurity.solonplugin.config.FinalSecurityConfiguration;

import java.util.List;

/**
 * @author lingkang
 * Created by 2022/10/28
 * @since 3.0.0
 */
public class FinalSecurityPlugin implements Plugin {
    private static final Logger log = LoggerFactory.getLogger(FinalSecurityPlugin.class);

    @Override
    public void start(AopContext context) {
        log.info("final-security 开始配置");
        // 注册bean
        context.wrapAndPut(FinalSecurityHolder.class, new FinalSecurityHolder());

        //手动注册模式
        context.beanAroundAdd(FinalCheck.class, new FinalCheckAnnotation());
        context.beanAroundAdd(FinalCheckLogin.class, new FinalCheckLoginAnnotation());

        context.beanOnloaded(aopContext -> {
            FinalHttpProperties properties = null;
            List beansOfType = context.getBeansOfType(Filter.class);
            for (Filter filter : beansOfType) {
                if (FinalSecurityConfiguration.class.isAssignableFrom(filter.getClass())) {
                    FinalSecurityConfiguration configuration = (FinalSecurityConfiguration) filter;
                    configuration.init();
                    properties = configuration.getProperties();
                    // 将配置注册到上下文中
                    context.wrapAndPut(FinalHttpProperties.class, properties);
                    break;
                }
            }
            if (properties != null) {
                FinalCheckAnnotation finalCheckAnnotation = (FinalCheckAnnotation) context.beanAroundGet(FinalCheck.class).getReal();
                finalCheckAnnotation.properties = properties;
                FinalCheckLoginAnnotation loginAnnotation = (FinalCheckLoginAnnotation) context.beanAroundGet(FinalCheckLogin.class).getReal();
                loginAnnotation.properties = properties;
            } else {
                if (NOPLogger.class.isAssignableFrom(log.getClass())) {
                    System.err.println("警告,您还未配置 final-security ,安全认证未开启!");
                } else {
                    log.warn("警告,您还未配置 final-security ,安全认证未开启!");
                }
            }
            log.info("final-security 配置完成");
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy