Administrator
2026-08-11 1415f6273a7cb91838d002803d13a68f767cba15
src/main/java/cc/mrbird/febs/common/configure/WebMvcConfigure.java
@@ -1,7 +1,12 @@
package cc.mrbird.febs.common.configure;
import cc.mrbird.febs.common.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -25,10 +30,63 @@
        registration.excludePathPatterns("/api/news/**");
        registration.excludePathPatterns("/api/member/cashOutSetting");
        registration.excludePathPatterns("/api/member/agentDetail");
        registration.excludePathPatterns("/api/member/activityInfo");
        registration.excludePathPatterns("/api/leader/leaderList");
        registration.excludePathPatterns("/api/leader/leaderListInFence");
        registration.excludePathPatterns("/api/leader/noLoginLeaderTitle");
        registration.excludePathPatterns("/api/xcxPay/wxpayCallback");
        registration.excludePathPatterns("/api/xcxPay/rechargeCallBack");
        registration.excludePathPatterns("/api/xcxPay/fapiaoCallBack");
        registration.excludePathPatterns("/api/fuPayReturn/callback");
        registration.excludePathPatterns("/api/fuPayReturn/payment/callback");
        registration.excludePathPatterns("/api/fuPay/notify");
        registration.excludePathPatterns("/api/tokenview/**");
        registration.excludePathPatterns("/api/order/lwPayNotify");
        registration.excludePathPatterns("/api/order/lwPayReturn");
        registration.excludePathPatterns("/api/order/bsPayNotify");
        registration.excludePathPatterns("/api/track/visit");
        // 添加Swagger UI相关路径
        registration.excludePathPatterns("/api/swagger-ui.html");
        registration.excludePathPatterns("/v2/api-docs");
        registration.excludePathPatterns("/configuration/ui");
        registration.excludePathPatterns("/configuration/security");
        registration.excludePathPatterns("/swagger-resources");
    }
    /**
     * Servlet 级别的跨域过滤器,比 Shiro Filter 和 Spring MVC Interceptor 优先级更高,
     * 确保 OPTIONS 预检请求能正确返回 CORS 头。
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 允许的来源域名(根据实际情况添加)
        config.addAllowedOrigin("https://www.polypepprotein.com");
        config.addAllowedOrigin("https://polypepprotein.com");
        config.addAllowedOrigin("http://localhost:30002");
        // 允许所有方法
        config.addAllowedMethod("*");
        // 允许所有请求头
        config.addAllowedHeader("*");
        // 允许携带 Cookie / Authorization 等凭证信息
        config.setAllowCredentials(true);
        // 预检请求缓存时间
        config.setMaxAge(3600L);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 跨域请求配置(作为 CorsFilter 的补充,处理 Spring MVC 层面的 CORS)
        registry.addMapping("/**")
                .allowedOrigins("https://www.polypepprotein.com", "https://polypepprotein.com")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}