gpt4 book ai didi

java - 将本地 React 前端连接到本地 Spring Boot 中间件应用程序时出现 CORS 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:58 24 4
gpt4 key购买 nike

我正在使用 React 开发一个前端应用程序,它连接到一个用 Spring boot 编写的中间件服务。我正在尝试从前端调用端点,如下所示:

return axios.post('http://localhost:8085/workshop/client/createClient', {username})
.then((response) => {
console.log('Success')
})
.catch((error) => console.log(error));

每当我在浏览器中发出请求时,我都会收到以下错误:

OPTIONS http://localhost:8085/workshop/client/createClient 401 ()

Failed to load http://localhost:8085/workshop/client/createClient: Response for preflight has invalid HTTP status code 401.

据我了解,这是因为预检请求被我的中间件应用程序阻止了。在网上阅读了一些有关启用此功能的内容后,我在我的 spring boot 应用程序中添加了一个 CorsFilter:

@Slf4j
public class CORSFilter implements Filter {
private static final String ONE_HOUR = "3600";

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", ONE_HOUR);
response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type, Accept, x-device-user-agent, Content-Type");

if (req instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) req;
if (httpServletRequest.getHeader(HttpHeaders.ORIGIN) != null
&& httpServletRequest.getMethod().equals(HttpMethod.OPTIONS.name())
&& httpServletRequest.getHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD) != null) {
log.debug("Received an OPTIONS pre-flight request.");
return;
}
}
chain.doFilter(req, res);
}

@Override
public void destroy() {
}
}

@Configuration
public class MvcConfiguration {

@Bean
public FilterRegistrationBean corsFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new CORSFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.setOrder(0);
return filterRegistrationBean;
}
}

这是端点的示例:

 @PostMapping("/createClient")
public ResponseEntity<?> createClient(@RequestBody CreateClientDto clientDto) {
try {
...
return new ResponseEntity<>(responseBody, OK);
} catch (Exception e ...) {}
}

任何帮助将不胜感激:)

更新:我的请求 url 略有错误(因此是 404)。我已经更新了错误消息。我似乎仍有 CORS 问题。

这些是我可以在开发工具中看到的响应 header :

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:access-control-allow-credentials, access-control-allow-methods, access-control-allow-origin, allow, content-type
Access-Control-Allow-Methods:POST
Access-Control-Allow-Origin:http://localhost:3000
Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:0
Date:Thu, 01 Mar 2018 14:06:38 GMT
Expires:0
Pragma:no-cache
Strict-Transport-Security:max-age=31536000 ; includeSubDomains
Vary:Origin
WWW-Authenticate:Basic realm="Spring"
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block

以及请求 header :

OPTIONS /workshop/client/createClient HTTP/1.1
Host: localhost:8085
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
Access-Control-Request-Headers: access-control-allow-credentials,access-control-allow-methods,access-control-allow-origin,allow,content-type
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,da;q=0.7

最佳答案

我也有这个问题,但请允许我以更简单的交流方式给出一个直接的答案。

首先,您必须告诉您的服务器(Spring boot java)关于客户端(Reactjs)URL

例如,大多数时候 spring boot 使用 http://localhost:8080 而 Reactjs 使用 http://localhost:3000

因此,您必须在 Spring boot java 中导航到 Controller ,并允许 Reactjs 的 URL 获得服务器(Spring boot)中的可访问性权限。这将帮助您摆脱 CORS 问题。

我们如何在 Spring boot 中执行此操作?只需添加 @CrossOrigin 注释指定 Reactjs URL 链接,如下所示:

例如:

    @GetMapping("/orphans")
@CrossOrigin(origins = "http://localhost:3000")
Iterable<Student> read() {
return studentService.findAll();
}

上面的方法是列出所有孤儿,所以我给了 Reactjs URL 链接权限,然后我摆脱了 CORS 问题。

快乐编码。

关于java - 将本地 React 前端连接到本地 Spring Boot 中间件应用程序时出现 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49049312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com