Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the cross-domain problem of springcloudgateway request

Shulou Source: shulou.com Published: 2022-06-03 09:35:27 11月15日 Update

Editor to share with you how to solve the cross-domain problem of springcloudgateway requests, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

The code is as follows

@ Configurationpublic class CorsConfig implements GlobalFilter, Ordered {private static final String ALL = "*"; private static final String MAX_AGE = "18000L"; @ Bean public RouteDefinitionLocator discoveryClientRouteDefinitionLocator (DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {return new DiscoveryClientRouteDefinitionLocator (discoveryClient, properties);} @ Bean public ServerCodecConfigurer serverCodecConfigurer () {return new DefaultServerCodecConfigurer () } / / add request header @ Bean public WebFilter corsFilter () {return (ServerWebExchange ctx, WebFilterChain chain)-> {ServerHttpRequest request = ctx.getRequest (); if (! CorsUtils.isCorsRequest (request)) {return chain.filter (ctx);} HttpHeaders requestHeaders = request.getHeaders (); ServerHttpResponse response = ctx.getResponse (); HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod (); HttpHeaders headers = response.getHeaders () Headers.add (HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin ()); headers.addAll (HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders ()); if (requestMethod! = null) {headers.add (HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name ());} headers.add (HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); headers.add (HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL) Headers.add (HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE); if (request.getMethod () = = HttpMethod.OPTIONS) {response.setStatusCode (HttpStatus.OK); return Mono.empty ();} return chain.filter (ctx);} } / / remove multiple values from the request header. This is a bug @ Override public Mono filter (ServerWebExchange exchange) of this version. GatewayFilterChain chain) {return chain.filter (exchange) .then (Mono.defer (()-> {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 (new ArrayList ()) {{add (kv.getValue () .get (0) | });}); return chain.filter (exchange);});} @ Override public int getOrder () {/ / specify that the filter is located after the NettyWriteResponseFilter / / that is, the response header return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER + 1 is processed after the response body is processed;}}

The version of spring-cloud-gateway here is 2.1.3.RELEASE

These are all the contents of the article "how to solve the cross-domain problem of springcloudgateway requests". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Articles questions content versions processing not much code multiple most more knowledge industry information channels filters this is channels references learning help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MariaDB Xiaomi vpn MySQL