前端请求到后端 提示:
DEBUG o.s.w.s.a.HttpWebHandlerAdapter Completed 403 FORBIDDEN
vue的请求可以到达服务,但是不响应
1. 前端直接调用服务的情况
在controller上添加注解@CrossOrigin
@CrossOrigin
@RestController
@RequestMapping("/api/user")
public class UserController {
}
2. 前端访问网关转发到服务
增加配置类
package com.onefamily.gateway.onefamilygateway.config;
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;
/**
* @author: xiongzx
* @Description: 解决跨域问题
* @date: 2022/07/06/ 16:21
*/
@Configuration
public class CorsConfig {
//这里为支持的请求头,如果有自定义的header字段请自己添加(不知道为什么不能使用*)
private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN,token,username,client,apiKey,dataRegion";
private static final String ALLOWED_METHODS = "GET, PUT, POST, DELETE, OPTIONS";
private static final String ALLOWED_ORIGIN = "*";
private static final String ALLOWED_EXPOSE = "*";
private static final String MAX_AGE = "18000L";//预检缓存时长
@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);//允许什么请求头 *表示所有
headers.add("Access-Control-Expose-Headers", ALLOWED_EXPOSE);//配置前端js允许访问的自定义响应头
headers.add("Access-Control-Allow-Credentials", "true");//允许后端服务跨域cookie传到前端
if (request.getMethod() == HttpMethod.OPTIONS) {
response.setStatusCode(HttpStatus.OK);
return Mono.empty();
}
}
return chain.filter(ctx);
};
}
}
6802

被折叠的 条评论
为什么被折叠?



