【问题标题】:Laravel Echo + Laravel Passport + CORSLaravel Echo + Laravel Passport + CORS
【发布时间】:2018-09-18 12:40:57
【问题描述】:

最近我在 laravel-echo-server 中遇到了身份验证错误。

我已经设法使用 Laravel Passport + Vue.js 设置了一个应用程序,并且受 auth:api middleware 保护的端点工作得很好。

但是,当我尝试使用 Laravel Echo 验证用户身份时,我在 laravel-echo-server 的日志中收到 405 异常。

为了向 laravel-echo-server 的 api 端点添加身份验证,我已将 BroadcastServiceProvider.php 的内容更改为
Broadcast::routes(['prefix' => 'api', 'middleware' => 'auth:api']);,这是我的laravel-echo-server.json 文件看起来像

{
  "authHost": "http://192.168.225.128",
  "authEndpoint": "/api/broadcasting/auth",
  "clients": [
    {
      "appId": "APP_ID",
      "key": "APP_KEY"
    }
  ],
  "database": "redis",
  "databaseConfig": {
    "redis": {},
    "sqlite": {
      "databasePath": "/database/laravel-echo-server.sqlite"
    }
  },
  "devMode": true,
  "host": null,
  "port": "3389",
  "protocol": "http",
  "sslCertPath": "",
  "sslKeyPath": "",
  "sslCertChainPath": "",
  "sslPassphrase": "",
  "apiOriginAllow": {
    "allowCors": true,
    "allowOrigin": "http://192.168.225.128",
    "allowMethods": "OPTIONS, GET, POST",
    "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
  }
}

这就是在 Vue.js 应用程序中配置回显服务器的方式:

import Vue from 'vue';
import Echo from 'laravel-echo';
const io = require('socket.io-client');
import store from '~/store';
window.io = io;

if (typeof io !== undefined) {
    let authenticationToken = store.getters['account/token'];
    let authenticated = store.getters['account/authenticated'];

    let echo = new Echo({
        broadcaster: 'socket.io',
        host: window.location.hostname + ':3389',
        auth: {
            headers: {
                Authorization: (authenticated) ? 'Bearer ' + authenticationToken : ''
            }
        }
    });
    window.Echo = echo;
    if (! Vue.hasOwnProperty('$echo')) {
        Object.defineProperty(Vue.prototype, '$echo', {
            get() {
                return echo;
        }
        });
    }
    if (! Vue.hasOwnProperty('$io')) {
        Object.defineProperty(Vue.prototype, '$io', {
            get() {
                return io;
            }
        });
    }
}

鉴于 laravel-echo-server 吐出 405 错误,这意味着传递给订阅服务的所有数据都是正确的,但是,由于某种原因,我得到的方法不是laravel-echo-server 错误日志中允许。

为了澄清,我附上the log itself

【问题讨论】:

    标签: php laravel laravel-passport laravel-echo


    【解决方案1】:

    我猜广播路由没有被链接。

    您需要在 config/app.php 中取消注释 App\Providers\BroadcastServiceProvider::class

    您可以在运行 php artisan route:list 时进行确认,然后 api/broadcasting/auth 应该会出现在那里。

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2020-04-26
      • 2020-07-23
      • 2017-07-29
      • 2018-05-07
      • 2019-09-15
      • 2015-09-30
      • 1970-01-01
      • 2017-01-10
      相关资源
      最近更新 更多