로컬 파일 시스템(file:/// 프로토콜)에서 실행한 html(js) 파일의 Same Origin Policy(document.domain) 우회하는 방법

experience

  • 로컬 파일을 https 로 접근해서 test할일이 생김


* 실제로는 서버로 띄워서 http로 접근하면 되지만 file:/// 로 접근할 경우에는 Same Origin Policy때문에 쉽지 않음..
 
* 간단한 방법으로는 Safari를 이용해서 해당 파일(file:///)로 접근하면 되는 듯...
 (safari에서 Develop 메뉴 > Disable local file restrictions 과 disable cache 설정하면 됨)
 
 
from : https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally
 
 
 
Procedural content
If you use just procedural geometries and don't load any textures, webpages should work straight from the file system, just double-click on HTML file in a file manager and it should appear working in the browser (accessed as file:///example).
Content loaded from external files
If you load models or textures from external files, due to browsers' "same origin policy" security restrictions, loading from a file system will fail with a security exception.
There are two ways how to solve this:
Change security for local files in a browser (access page as file:///example)
Run files from a local server (access page as http://localhost/example)
If you use option 1, be aware that you may open yourself to some vulnerabilities if using the same browser for a regular web surfing. You may want to create a separate browser profile / shortcut used just for local development to be safe.
Change local files security policy
Safari
Enable the develop menu using the preferences panel, under Advanced -> "Show develop menu in menu bar"
Then from the safari "Develop" menu, select "Disable local file restrictions", it is also worth noting safari has some odd behaviour with caches, so it is advisable to use the "Disable caches" option in the same menu; if you are editing & debugging using safari.
Chrome
Close all running chrome instances first. Then start Chrome executable with a command line flag:
chrome --allow-file-access-from-files
On Windows, the easiest is probably to create a special shortcut which has added flag (right-click on shortcut -> properties -> target).
Firefox
Go to about:config
Find security.fileuri.strict_origin_policy parameter
Set it to false
Run local server
The simplest probably is to use Python's built-in http server.
If you have Python installed, it should be enough to run this from a command line:
# Python 2.x
python -m SimpleHTTPServer
# Python 3.x
python -m http.server
This will serve files from the current directory at localhost under port 8000:
http://localhost:8000/
If you have Ruby installed, you can get the same result running this instead:
ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"
PHP also has a built-in web server, starting with php 5.4.0:
php -S localhost:8000
Node.js has a simple HTTP server package. To install:
npm install http-server -g
To run:
http-server .
Other simple alternatives are discussed here on Stack Overflow.
Of course, you can use any other regular full-fledged web server like Apache or nginx.
Example with lighttpd, which is a very lightweight general purpose webserver (on MAC OSX):
Install it via homebrew brew install lighttpd
Create a configuration file called lighttpd.conf in the directory where you want to run your webserver. There is a sample in this page.
In the conf file, change the server.document-root with the directory you want to serve
Start it with lighttpd -f lighttpd.conf
Navigate to http://localhost:3000/ and it will serve static files from the directory you chose.
 

댓글

이 블로그의 인기 게시물

엑셀(excel) 비쥬얼 베이직(visual basic, vba) 심플 실비(실손보험, 실손의료비) 계산식

아트월에 날짜, 시간 나오는 LED 벽시계 설치시 케이블(전선)이 두꺼워서 틈새(홈)에 매립이 안되는 경우 랜선(lan cable, lan선) 이용시 주의사항

신한투자증권(주) 님이 세금계산서를 발행하였습니다. TaxBill365 관련 안전한 메일인지 여부?