栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Spring 5 Webflux中启用CORS?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Spring 5 Webflux中启用CORS?

我使用此自定义过滤器成功:

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpMethod;import org.springframework.http.HttpStatus;import org.springframework.http.server.reactive.ServerHttpRequest;import org.springframework.http.server.reactive.ServerHttpResponse;import org.springframework.web.cors.reactive.CorsUtils;import org.springframework.web.server.ServerWebExchange;import org.springframework.web.server.WebFilter;import org.springframework.web.server.WebFilterChain;import reactor.core.publisher.Mono;@Configurationpublic class CorsConfiguration {  private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN";  private static final String ALLOWED_METHODS = "GET, PUT, POST, DELETE, OPTIONS";  private static final String ALLOWED_ORIGIN = "*";  private static final String MAX_AGE = "3600";  @Bean  public WebFilter corsFilter() {    return (ServerWebExchange ctx, WebFilterChain chain) -> {      ServerHttpRequest request = ctx.getRequest();      if (CorsUtils.isCorsRequest(request)) {        ServerHttpResponse response = ctx.getResponse();        HttpHeaders headers = response.getHeaders();        headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);        headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);        headers.add("Access-Control-Max-Age", MAX_AGE);        headers.add("Access-Control-Allow-Headers",ALLOWED_HEADERS);        if (request.getMethod() == HttpMethod.OPTIONS) {          response.setStatusCode(HttpStatus.OK);          return Mono.empty();        }      }      return chain.filter(ctx);    };  }}

并且

org.springframework.boot:spring-boot-starter-web
不应作为依赖项包含在内-过滤器无法使用。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/415162.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号