今天用Electron-Vite,后端用SpringBoot写一个简单的接口,测试一下是否联通,没想到一直报错,也没找到原因
Refused to connect to 'http://localhost:8989/test' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.
原来是Electron-Vite设置没有设置CSP连接来源,导致Electron用了默认的
默认renderer/index.html
设置
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: "
/>
修改为
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' http://localhost:8989"
/>
指定后端接口为http://localhost:8989