package com.xindazhou.business.framework.security.config;

import com.xindazhou.framework.security.config.AuthorizeRequestsCustomizer;
import com.xindazhou.framework.security.config.SecurityProperties;
import com.xindazhou.framework.web.core.handler.GlobalExceptionHandler;
import com.xindazhou.framework.common.biz.system.oauth2.OAuth2TokenCommonApi;
import com.xindazhou.business.enums.ApiConstants;
import com.xindazhou.business.framework.security.filter.OptimizedTokenAuthenticationFilter;
import com.xindazhou.business.dal.redis.oauth2.OAuth2AccessTokenRedisDAO;
import com.xindazhou.business.dal.redis.oauth2.TokenBlacklistRedisDAO;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer;

/**
 * System 模块的 Security 配置
 */
@Configuration(proxyBeanMethods = false, value = "systemSecurityConfiguration")
public class SecurityConfiguration {

    @Bean("systemAuthorizeRequestsCustomizer")
    public AuthorizeRequestsCustomizer authorizeRequestsCustomizer() {
        return new AuthorizeRequestsCustomizer() {

            @Override
            public void customize(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry registry) {
                // TODO 芋艿：这个每个项目都需要重复配置，得捉摸有没通用的方案
                // Swagger 接口文档
                registry.requestMatchers("/v3/api-docs/**").permitAll()
                        .requestMatchers("/webjars/**").permitAll()
                        .requestMatchers("/swagger-ui").permitAll()
                        .requestMatchers("/swagger-ui/**").permitAll();
                // Druid 监控
                registry.requestMatchers("/druid/**").permitAll();
                // Spring Boot Actuator 的安全配置
                registry.requestMatchers("/actuator").permitAll()
                        .requestMatchers("/actuator/**").permitAll();
                // RPC 服务的安全配置
                registry.requestMatchers(ApiConstants.PREFIX + "/**").permitAll();
            }

        };
    }

    /**
     * 优化版 Token 认证过滤器
     * 
     * 使用 @Primary 覆盖框架默认的 TokenAuthenticationFilter
     * 优化点：直接查 Redis 和黑名单，减少 Feign 调用
     * 
     * 注意：返回类型使用 OncePerRequestFilter，因为 Spring Security 只需要这个接口
     */
    @Bean
    @Primary
    @ConditionalOnBean({OAuth2AccessTokenRedisDAO.class, TokenBlacklistRedisDAO.class})
    public org.springframework.web.filter.OncePerRequestFilter optimizedTokenAuthenticationFilter(
            SecurityProperties securityProperties,
            GlobalExceptionHandler globalExceptionHandler,
            OAuth2TokenCommonApi oauth2TokenApi,
            OAuth2AccessTokenRedisDAO oauth2AccessTokenRedisDAO,
            TokenBlacklistRedisDAO tokenBlacklistRedisDAO) {
        return new OptimizedTokenAuthenticationFilter(
                securityProperties,
                globalExceptionHandler,
                oauth2TokenApi,
                oauth2AccessTokenRedisDAO,
                tokenBlacklistRedisDAO
        );
    }

}
