Reactjs应用程序无法使用https请求到达我的Spring Boot后端

问题描述 投票:-1回答:1

[我有一个在https://localhost:8080上运行的ReactJS应用程序,我想将数据发布到我在https://localhost:8443上运行的Spring Boot后端应用程序中,但是当我尝试发布数据..我在我的浏览器控制台中收到此错误:] >

TypeError:“尝试获取资源时出现NetworkError。”跨域请求被阻止:同源策略禁止读取https://localhost:8443/api/处的远程资源(原因:CORS请求未成功)。

我的Spring Boot应用程序未使用Spring Security。我通过this教程启用了HTTPS。我使用HTTPS=true npm start运行我的ReactJS应用程序,并且更改了React应用程序端口,例如[package.json中的"start": "PORT=8080 react-scripts start"

这是我的application.properties:

server.ssl.key-store-password = password
server.ssl.key-store-type = PKCS12
server.ssl.key-alias = tomcat
server.ssl.enabled=true

这是尝试从React调用REST API的方法:

fetch('https://localhost:8443/api/..., {
            method: 'POST'
        }).then((data) => {
            if (data.ok) {
                this.setState({ 
                response: data })
            }
        })
        .catch(console.log);

这里是我的@RestController:

@RestController
@RequestMapping("/api")
public class MessageReceiverResource {

    @CrossOrigin(origins = "https://localhost:8080")
    @PostMapping
    public Response receiveMessages(@RequestBody Request request){
           ...
        return response;
    }
}

此解决方案在我启用HTTPS之前都可以正常工作。

我有一个在https:// localhost:8080上运行的ReactJS应用程序,我想将数据发布到在https:// localhost:8443上运行的Spring Boot后端应用程序中,但是当我尝试发布数据时..我得到了这个...

reactjs rest spring-boot post https
1个回答
0
投票

解决方案将此行添加到package.json:"proxy": "https://localhost:8443",

© www.soinside.com 2019 - 2024. All rights reserved.