Safari http请求标头未反映在axios拦截器的更改上

时间:2020-04-24 15:09:34

标签: javascript ajax safari axios basic-authentication

在我的Vue + Laravel SPA应用程序中,当我将请求标头的授权从“基本”更改为“承载”时,Chrome可以发送带有“承载”标头的http请求,但是在Safari中,即使我将授权更改为axios拦截器。

为什么他们的行为有所不同?我该如何解决? 任何帮助坦克。 *详细信息

.htaccess 基本身份验证设置。所有请求都需要基本身份验证凭据,但需要Jwt。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Satisfy Any

AuthUserfile .htpasswd
AuthGroupfile /dev/null
AuthName "Enter your ID and PASSWORD"
AuthType Basic
require valid-user

SetEnvIf Authorization "(Bearer)" jwt_request
Order Deny,Allow
Deny from all
allow from env=jwt_request

<Files "service-worker.js">
   Require all granted
</Files>

jwt的axios设置

axios.interceptors.request.use(config => {
  config.headers['X-Requested-With'] = 'XMLHttpRequest'
  const token = store.getters['auth/token']
  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token
  }
  return config
}

当我执行console.log(config)时,它显示“ Bearer + JWTtoken”,但是当我检查Safari的网络控制台时,它使用“ Basic”,并且在jwt 401失败,并显示消息“未提供令牌”。

Safari(13.1)

1 个答案:

答案 0 :(得分:1)

基本身份验证和承载身份验证使用相同的Authorization标头。 Chrome,Firefox和Edge可以使用用户名/密码进行身份验证并传递JWT。但是,Safari 12+始终会使用基本身份验证凭据覆盖Authorization标头。

解决此问题的方法是使用自定义标头进行授权(例如x-access-token)。

相关问题