[英]CORS issue on accessing API with JWT token
使用JWT令牌從angular訪問Symfony REST API時,我遇到了CORS問題。
IIS服務器已配置為使用域名和IP地址訪問應用程序。 前端代碼位於Symfony的同一目錄中。
前端使用域名調用API。 由於API指向域名,因此使用JWT令牌從IP地址訪問應用程序會出現CORS問題。
我在內核偵聽器中使用以下選項設置了響應頭。
我正在使用lexik / jwt-authentication-bundle生成令牌
$responseHeaders->set('Access-Control-Allow-Headers', 'origin, content-type, accept,authorization');
$responseHeaders->set('Access-Control-Allow-Origin', '*');
$responseHeaders->set('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, PATCH, OPTIONS');
$responseHeaders->set('Access-Control-Allow-Credentials', true);
我還使用以下選項設置了Nelmio CORS捆綁包
enter code hernelmio_cors:
defaults:
allow_credentials: false
allow_origin: []
allow_headers: []
allow_methods: []
expose_headers: []
max_age: 0
hosts: []
origin_regex: false
forced_allow_origin_value: ~
paths:
'^/api/':
allow_origin: ['*']
allow_headers: ['*']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
max_age: 3600
'^/':
allow_origin: ['*']
allow_headers: ['*']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
max_age: 3600e
響應標題:
允許
選項,TRACE,GET,HEAD,POST內容長度
0日期
2017年11月13日星期一16:03:22 GMT公眾
選項,TRACE,GET,HEAD,POST服務器
Microsoft-IIS / 10.0 X-Firefox-Spdy
H2
提前致謝!
我的web.config(Symfony 4):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="origin, content-type, accept,authorization" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST, GET, PUT, DELETE, PATCH, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.