久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 国产一区二区久久 | 免费看91 | 51ⅴ精品国产91久久久久久 | 久久久精品影院 | 99热国产免费 | 国产精品免费小视频 | 亚洲精品自拍视频 | 91久久久久久久久 | 夜夜摸天天操 | 亚洲精品一区二区三区在线 | 欧美精品中文字幕久久二区 | 老司机午夜性大片 | 黄色成人在线 | 日本三级全黄三级三级三级口周 | 国产精品一区二区三区四区 | 午夜精品 | 超碰免费在| 一区二区三区四区免费在线观看 | 中文字幕精品一区二区三区精品 | 精品国产欧美 | 免费一区 | 国产精品久久久久一区二区三区 | 91亚洲国产成人精品一区二三 | 亚洲精品高清视频在线观看 | 国产美女永久免费无遮挡 | 亚洲一区二区在线 | 日韩黄| 91av视频在线播放 | 视频在线一区二区 | 青青草免费在线视频 | 四虎永久在线精品免费一区二 | 一级网站 | 一区二区视频在线 | 91免费观看| 久久久国产精品 | 国产精品久久久久久久免费大片 | 精品无码久久久久久久动漫 | 中文字幕在线一区二区三区 | 欧美视频网 | 亚洲国产成人精品一区二区 | 亚洲欧美综合精品久久成人 |