Laravel Echo + Laravel Passport + CORS

时间:2018-09-18 12:40:58

标签: php laravel laravel-passport laravel-echo

一天中的好时光,
最近,我在laravel-echo-server中遇到身份验证错误。

我设法使用Laravel Passport + Vue.js设置了一个应用程序,并且受auth:api 中间件保护的端点工作正常。

但是,当我尝试使用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 错误日志。

为澄清起见,我附加了日志本身:Pastebin

1 个答案:

答案 0 :(得分:0)

我猜测广播路由未链接。

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

您可以确认然后运行php artisan route:list并在其中显示api / broadcasting / auth。