1跨域问题解决
package com.win.credit.point.gateway.config;
import java.time.Duration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsWebFilter; import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; import org.springframework.web.util.pattern.PathPatternParser; /** ?*/ @Configuration public class CorsConfig { ? ? @Bean ? ? public CorsWebFilter corsFilter() { ? ? ? ? CorsConfiguration config = new CorsConfiguration(); ? ? ? ? config.addAllowedMethod("*");; ? ? ? ? config.addAllowedOrigin("*"); ? ? ? ? config.addAllowedHeader("*"); ? ? ? ? config.setMaxAge(Duration.ofHours(24));//options重复请求cache优化 ? ? ? ? UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); ? ? ? ? source.registerCorsConfiguration("/**", config);
? ? ? ? return new CorsWebFilter(source); ? ? } }
2有网关一层,网关会转发一次,重复跨域
package com.win.credit.point.gateway.filter; import java.util.Arrays;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; /** ?* 重复跨域,去重 ?*/ @Component("corsResponseHeaderFilter") public class ResponseCorsHeaderFilter implements GlobalFilter, Ordered {
?? ?private static final Logger logger = LoggerFactory.getLogger(ResponseCorsHeaderFilter.class); ?? ? ? ? @Override ? ? public int getOrder() { ? ? ? ? // 指定此过滤器位于NettyWriteResponseFilter之后 ? ? ? ? // 即待处理完响应体后接着处理响应头 ? ? ? ? return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER + 1000; ? ? }
? ? /** ? ? ?* Process the Web request and (optionally) delegate to the next {@code WebFilter} ? ? ?* through the given {@link GatewayFilterChain}. ? ? ?* ? ? ?* @param exchange the current server exchange ? ? ?* @param chain ? ?provides a way to delegate to the next filter ? ? ?* @return {@code Mono<Void>} to indicate when request processing is complete ? ? ?*/ ? ? @Override ? ? public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ? ? ? ? return chain.filter(exchange).then(Mono.fromRunnable(() -> { ? ? ? ? ?? ?exchange.getResponse().getHeaders().entrySet().stream() ? ? ? ? ? ? .filter(kv -> (kv.getValue() != null && kv.getValue().size() > 1)) ? ? ? ? ? ? .filter(kv -> (kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN) ? ? ? ? ? ? ? ? ? ? || kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS))) ? ? ? ? ? ? .forEach(kv -> { ? ? ? ? ? ? ? ? kv.setValue(Arrays.asList(kv.getValue().get(1))); ? ? ? ? ? ? }); ? ? ? ? })); ? ? } } ?
本文只给解决方式,仅供参考。
|