package com.ytpm.lemonios.config;


import com.ytpm.encrypt.ApiDecryptProperties;
import com.ytpm.encrypt.CryptoFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;

/**
 * @author hidewnd
 * @date 2026/1/13 10:56
 */
@Configuration
@EnableConfigurationProperties(ApiDecryptProperties.class)
@ConditionalOnProperty(value = "api-decrypt.enabled", havingValue = "true")
public class ApiDecryptAutoConfiguration {

    @Bean
    public FilterRegistrationBean<CryptoFilter> cryptoFilterRegistration(ApiDecryptProperties properties) {
        FilterRegistrationBean<CryptoFilter> registration = new FilterRegistrationBean<>();

        // 实例化您的自定义过滤器
        CryptoFilter filter = new CryptoFilter(properties);

        registration.setFilter(filter);
        // 设置过滤器名称
        registration.setName("cryptoFilter");
        // 设置拦截路径
        registration.addUrlPatterns("/*");
        // 设置优先级：最高
        registration.setOrder(Integer.MIN_VALUE);
        // 设置 Dispatcher 类型
        registration.setDispatcherTypes(DispatcherType.REQUEST);

        return registration;
    }

}
