| | |
| | | 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;
|
| | |
| | | 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) {
|
| | | // 跨域请求配置
|
| | | registry.addMapping("/**")//允许请求路径
|
| | | .allowedOrigins("*")//表示允许所有网址发起跨域请求
|
| | | .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")//表示允许跨域请求的方法
|
| | | .maxAge(3600);//表示在3600秒内不需要再发送预校验请求
|
| | | 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);
|
| | | }
|
| | | }
|