package com.ruoyi.web.modules.industryservice.research.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import com.ruoyi.web.modules.industryservice.research.filter.ResearchOpenApiAuthFilter;
import com.ruoyi.web.modules.industryservice.research.support.ResearchOpenApiConstants;
import com.ruoyi.web.modules.industryservice.research.support.ResearchOpenApiTokenService;

/**
 * 注册科研开放接口鉴权过滤器。
 */
@Configuration
@ConditionalOnProperty(prefix = "research-open-api", name = "enabled", havingValue = "true")
public class ResearchOpenApiFilterConfig
{
    @Bean
    public FilterRegistrationBean<ResearchOpenApiAuthFilter> researchOpenApiAuthFilter(
            ResearchOpenApiProperties properties,
            ResearchOpenApiTokenService tokenService)
    {
        FilterRegistrationBean<ResearchOpenApiAuthFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new ResearchOpenApiAuthFilter(properties, tokenService));
        bean.addUrlPatterns(ResearchOpenApiConstants.API_PREFIX + "/*");
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE + 10);
        bean.setName("researchOpenApiAuthFilter");
        return bean;
    }
}
