package com.example.webapi.config; import com.example.webapi.interceptor.SignatureInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.Arrays; /** * Web配置类 * * @author Your Name * @version 1.0.0 */ @Configuration public class WebConfig implements WebMvcConfigurer { private final SignatureInterceptor signatureInterceptor; public WebConfig(SignatureInterceptor signatureInterceptor) { this.signatureInterceptor = signatureInterceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(signatureInterceptor) .addPathPatterns("/**") .excludePathPatterns( "/fuquanapi/swagger-ui.html", "/fuquanapi/swagger-ui/**", "/fuquanapi/v3/api-docs", "/fuquanapi/v3/api-docs/**", "/fuquanapi/webjars/**", "/fuquanapi/favicon.ico", "/fuquanapi/swagger-resources/**" ); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOriginPatterns("*") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*") .allowCredentials(true) .maxAge(3600); } /** * CORS配置 * * @return CORS配置源 */ @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOriginPatterns(Arrays.asList("*")); configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); configuration.setAllowedHeaders(Arrays.asList("*")); configuration.setAllowCredentials(true); configuration.setMaxAge(3600L); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } }