问题
I have developer server that are used for testing. They have SSL self-signed certificates, which allow us to test the web application over HTTPS, but with prominent warnings that the certificates are not verifiable.
That\'s fine, but I have a Service Worker that throws an error with the navigator.serviceWorker.register
SecurityError: Failed to register a ServiceWorker: An SSL certificate error occurred when fetching the script.
How do I use a Service Worker with an intranet testing server which has a self-signed certificate?
回答1:
As an alternative to using self-signed certificates, you can launch Chrome or Firefox such that it pretends certain domains are secure. For example, using Chrome on a Mac, you can launch it using:
/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ --user-data-dir=/tmp/foo --unsafely-treat-insecure-origin-as-secure=http://www.your.site
Service workers should then work from http://www.your.site.
More info can be found here: Options for testing service workers via HTTP
Edit: Changed --unsafety-... to --unsafely-...
回答2:
The accepted answer above didn't work for me. I added the --ignore-certificate-errors to that as suggested by @stef52 for this question Error with Service Worker registration and that worked
chrome.exe --user-data-dir=/tmp/foo --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://localhost/
OR for MAC users
./Google\ Chrome --user-data-dir=/tmp/foo --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://localhost
回答3:
For local development we use self signed certs. To overcome the problems relating to local dev on OSX. We did the following:
- Create your certificate and serve it up
- Navigate to the https url
- Open dev tools > security > view certificate
- drag the certificate icon to the desktop and doubleclick on it, this will open up keychain access.
- drag the certificate icon to login, open up log-in and double click on the certificate (it should be named with the dev domain or similar) open up the trust dropdown and select always trust. Go back to your app close the window and re-open with https, you should now have 'faux' https for your dev domain.
回答4:
This answer repeats some Chucks' points.
If that specific DomException was happened locally at some address port, when accessing web resource at local machine, one of these latest version of browser launches may had helped:
open -a Opera.app --args --user-data-dir=/tmp/foo --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://localhost:8111
open -a Brave\ Browser.app --args --user-data-dir=/tmp/foo --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://localhost:8111
open -a Google\ Chrome.app --args --user-data-dir=/tmp/foo --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://localhost:8111
Chromium browser did not start with these settings to allow to overcome this specific DomException for using SSL with service worker locally.
This person provided some insights as a story as well for this matter: https://deanhume.com/testing-service-workers-locally-with-self-signed-certificates/
回答5:
For me, ignoring certificates or setting the insecure origin flag for mobile device did not work.
However, portforwarding did the trick. Service workers are allowed to run on localhost no matter if its signed with certificate and communicating through SSL, so basically you let your mobile device think that it runs on localhost but actually you are forwarding it to the real localhost on server machine. This can be achieved by each of these 2 methods:
- Using native chrome dev tools forwarding: https://stackoverflow.com/a/43426714/5048121
- Using ADB: https://stackoverflow.com/a/46795769/5048121
回答6:
For those who are getting error No such file or directory after trying @Chucks accepted answer try below,
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=/tmp/foo --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://localhost:1123
More information can be found on this link
来源:https://stackoverflow.com/questions/38728176/can-you-use-a-service-worker-with-a-self-signed-certificate