久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Electron App 中定義 CSP HTTP Header

Define CSP HTTP Header in Electron App(在 Electron App 中定義 CSP HTTP Header)
本文介紹了在 Electron App 中定義 CSP HTTP Header的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

按照 API 文檔,我不明白如何為我的 Electron 應用程序的渲染器定義一個 Content-Security-Policy HTTP Header.我總是在 DevTools 中收到警告.

Following the API documentation, I don't understand how to define a Content-Security-Policy HTTP Header for the renderer of my Electron application. I always get a warning in the DevTools.

我試過了:

1) 盲目復制/粘貼API Doc中的代碼:

1) Copy/Paste the code in the API Doc, blindly:

app.on('ready', () => {
    const {session} = require('electron')
    session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
        callback({responseHeaders: `default-src 'self'`})
    })

    win = new BrowserWindow(...)
    win.loadUrl(...)
}

(順便說一句,我不明白為什么字符串中缺少Content-Security-Policy:".但是添加它不會改變任何東西)

(By the way, I don't get why "Content-Security-Policy:" is missing in the string. But adding it don't change anything)

2) 用同樣的代碼修改渲染器的會話:

2) Modifying the session of the renderer with the same code:

win = new BrowserWindow(...)
win.loadUrl(...)

const ses = win.webContents.session;
ses.webRequest.onHeadersReceived((details, callback) => {
  callback({responseHeaders: `default-src 'self'`})
})

3) 向渲染器添加額外的標頭:

3) Add an extra header to ther renderer:

win = new BrowserWindow(...)
win.loadURL(`file://${__dirname}/renderer.html`,{
    extraHeaders: `Content-Security-Policy: default-src 'self'`
});

...

唯一有效的是在渲染器 HTML 文件中使用元標記:

The only thing that works is using a meta tag in the renderer HTML file:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'>

推薦答案

不知道為什么文檔包含這個損壞的代碼.它把我弄糊涂了,但我通過反復試驗找到了一個可行的解決方案:

Not sure why the documentation contains this broken code. It confused the hell out of me but I found a working solution by trial and error:

session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
    callback({ responseHeaders: Object.assign({
        "Content-Security-Policy": [ "default-src 'self'" ]
    }, details.responseHeaders)});
});

所以 headers 參數必須是一個與 details.responseHeaders 中接收到的原始 headers 具有相同結構的對象.并且原始標頭也必須包含在傳遞的對象中,因為該對象似乎完全替換了原始響應標頭.

So the headers argument must be an object with the same structure as the original headers received in details.responseHeaders. And the original headers must be included in the passed object as well because this object seems to completely replace the original response headers.

extraHeaders 選項不適用于響應標頭.它用于發送到服務器的請求標頭.

The extraHeaders option isn't for response headers. It is for request headers sent to the server.

這篇關于在 Electron App 中定義 CSP HTTP Header的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 国产精品视频二区三区 | 欧美日韩国产中文字幕 | 嫩草网| 羞羞羞视频 | 福利片在线看 | 久久久精品国产 | 精品国产一级 | 国产在线中文字幕 | 国产成人jvid在线播放 | 久久久久久艹 | 国产精品久久久久久久免费观看 | 国产精品视频www | 国产精品久久久久久久久久了 | 亚洲免费网站 | 国产成人精品一区二区 | 亚洲免费网 | 亚洲日韩中文字幕一区 | 国产视频中文字幕 | 国外成人在线视频 | 高清视频一区二区三区 | 鲁一鲁资源影视 | 中文字幕精品一区二区三区精品 | 欧美精品1区2区3区 精品国产欧美一区二区 | 国产欧美一区二区精品久导航 | 色综合色综合 | 一级做受毛片免费大片 | 一区二区蜜桃 | 亚洲 中文 欧美 日韩 在线观看 | 二区国产 | 日韩不卡三区 | 午夜视频在线免费观看 | 国产精品视频999 | 亚洲成人在线免费 | 美女久久视频 | 九七午夜剧场福利写真 | 91精品中文字幕一区二区三区 | av中文字幕在线 | 91久久久久久久久久久 | 久久国产视频网站 | 中文字幕在线观 | 欧美激情第一区 |