【问题标题】:laravel-echo-server - Client can not be authenticated, got HTTP status 403 JWT authlaravel-echo-server - 客户端无法通过身份验证,获得 HTTP 状态 403 JWT 身份验证
【发布时间】:2019-01-25 04:25:32
【问题描述】:

我正在构建一个 SPA。我将 laravel 用于后端,将 vuejs 用于前端。它们在不同的端口上独立运行。 (使用 laravel api)

  • Laravel 在 8000 上运行
  • Vue 在 8082 上运行

我允许用户使用 JWT Auth 进行身份验证。

我正在尝试使用 laravel-echo-server 制作一个实时数据传输应用程序,并使用公共渠道传输数据,效果很好。

现在我需要使用私人频道向经过身份验证的用户广播数据,这就是我无法弄清楚如何使其工作的地方,并且出现了诸如

之类的错误
NuG9yhe46E3N9Q6zAAAA could not be authenticated to private-some-p
rivate-channel

Client can not be authenticated, got HTTP status 403

这里是 laravel-echo-server.json 文件

{
    "authHost": "http://localhost:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "mprimeapp",
            "key": "d8f6639653d0c4f31efa56d27f6ab502"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "127.0.0.1"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "localhost",
    "port": "8890",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": ["http://localhost:8000", "http://localhost:8082"],
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

我正在使用 redis 而不是正在运行的 sqlite 数据库。

我的 main.js 文件 vue 应用

const tokenData = JSON.parse(window.localStorage.getItem('authUser'))
if (tokenData !== null) {

  window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://localhost:8890',
    auth: {
      headers: {
        'Authorization': 'Bearer ' + tokenData.token
      }
    }
  })
}

window.Echo.private('some-private-channel')
  .listen('SomeEvent', (e) => {
    console.log(e)
  })

这里有几件事我想问。

  1. authEndpoint 在 laravel-echo-server.json 文件中,它是如何工作的?我是否必须在我的 api.php 文件中创建一个端点来验证用户身份。
  2. laravel-echo-server.json 文件中的clients数组,里面的数据有什么用?

在 config/app.php 内的 Laravel 应用中

我没有注释这行 App\Providers\BroadcastServiceProvider::class

在 BroadcastServiceProvider.php 中

 public function boot()
    {
        Broadcast::routes([ 'middleware' => [ 'auth:jwt.auth' ] ] ]);
        require base_path('routes/channels.php');
    }

更新

上面的函数好像有问题。更具体的事情[ 'middleware' => [ 'api', 'jwt.auth' ] ]

更改中间件数组后出现此错误

[15:58:13] - vAKK8BO7vs2TZy4SAAAA could not be authenticated to private-some-private-channel
{
    "message": "Auth guard [jwt.auth] is not defined.",
    "exception": "InvalidArgumentException",
    "file": "C:\\xampp\\htdocs\\mipm-l\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\AuthManag
er.php",
.....
.....
.....

【问题讨论】:

    标签: laravel socket.io laravel-echo


    【解决方案1】:

    您是否安装并配置了 JWT Auth 包 https://github.com/tymondesigns/jwt-auth ? (对不起,你没有提到) 还要确保你的 Kernel.php 中有以下几行

       protected $routeMiddleware = [
        ...
        'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
        'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
        ...
        ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-24
      • 2018-02-19
      • 2020-07-24
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2016-12-14
      相关资源
      最近更新 更多