使用Java客户端在laravel中认证私有通道的问题

时间:2019-05-29 15:16:03

标签: java php laravel pusher laravel-passport

我想从服务器(使用laravel)向客户端(使用java)发送广播消息。

我正在使用什么

  1. Pusher作为广播广播驱动程序。
  2. 用于api身份验证的Laravel护照。

我在服务器端完成的操作

  1. 我已在.env文件中配置了Pusher凭据。
  2. App\Providers\BroadcastServiceProvider::class文件中未注释的config/app.php行。
  3. config/auth.php文件中,我添加了以下内容:
'guards' => [
     'web' => [
         'driver' => 'session',
         'provider' => 'users',
     ],

     'devices' => [
         'driver' => 'session',
         'provider' => 'devices',
     ],

     'api' => [
         'driver' => 'passport',
         'provider' => 'devices',
     ],
 ],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

     // using devices table to authenticate over api guard
    'devices' => [
        'driver' => 'eloquent',
        'model' => App\Device::class,
    ],
],
  1. App\Providers\BroadcastServiceProvider类中,我向boot()函数添加了以下内容:
Broadcast::routes(['prefix' => 'api', 'middleware' => 'auth:api']);
  1. routes/channels.php中,我添加了以下内容:
Broadcast::channel('device.{device_id}', function ($device, $device_id) {
    return $device->id === $device_id;
});
  1. 通过运行AdvertisementAdded创建事件php artisan make:event AdvertisementAdded,添加implements ShouldBroadcast,然后将以下内容添加到其broadcastOn()方法中:
return new PrivateChannel('device.'.$this->device_id);

我在客户端所做的事情

因为我现在正在测试,所以我通过发送邮递员的登录请求获得了access_tokendevice_id

Getting access_token from postman

我将该accessToken复制到我的Java客户端,并将其存储为accessTokenString变量中,这是代码:

String accessToken = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImY3ZTVlMTAzZWE3MzJjMTI5NzY1YTliMmMzOTM0N2ZhOGE4OTU5MjRjNDA5ZjgyOTA4ZDg5NTFjZTBkOGZlNTA2M2M1YTI1MDBlOTdhZDdiIn0.eyJhdWQiOiIxIiwianRpIjoiZjdlNWUxMDNlYTczMmMxMjk3NjVhOWIyYzM5MzQ3ZmE4YTg5NTkyNGM0MDlmODI5MDhkODk1MWNlMGQ4ZmU1MDYzYzVhMjUwMGU5N2FkN2IiLCJpYXQiOjE1NTkwOTYyNDgsIm5iZiI6MTU1OTA5NjI0OCwiZXhwIjoxNTkwNzE4NjQ3LCJzdWIiOiI3Iiwic2NvcGVzIjpbXX0.FKeE9Z-wv2yUNQPl-qsbu9baYGTdbQ6DuzaI1R8azR6l1CIP9uRI4hCaoWvgx0GXWWLPRNhfQl-YD3KP2YOraW16-h4ie_95B9VQrpFxXnlqKojsfh1xSrSNSl5HncslMWQPVjoesBpM5y_cpG19PGgu-SWo0W6s9Fiz_Nm70oyyZB9mSqU8PVQvAOSNr6TMR0aC3iMLFfkyZkTSwj8EoRyD2LGW6v4PFriqx8JLbZASCOiUYBlYnunWrTFDOAenZcoa5Sw7u7kbSvYehjDKRwKjQM6zmPfi0A3Mp0CHjHE599OXb-NG2IMH-wmlT0vEZjP2U97hxmsNW1RtHNXWaRKFL9T-WVmZbJf3fH5hXqTv495L3MQfq_m5YFHyc5NuIqK4K4xMJB956a33ICnH8DmvPmJgderNAhqEX1JHUAsR63K7xbZxRBDS8OlQYcEf-_v75X0kT1s067enSvI8Vs212AVnI6k0FmgQNM8DfJUq6YduD0m2F2ZWpKPrwdd6PdW5ZlZTEv-D8dYIEQ_CwOWohNoENATmTqxDpPBxK5c723MEt8S7Sa9MEGAo56HW3-9pbazbEdY1GqPWKVkov7K_6eBFcWsV67AgJpoKFt6RiBfRvokgiH96WG89qBB_Ucpm8uBahX93FaOXhVLW0VjJH2LQKrGw0bb5LS8Ql5o";
String deviceId = "7";

Map<String, String> authHeaders = new HashMap();

authHeaders.put("Authorization", accessToken);

HttpAuthorizer authorizer = new HttpAuthorizer("http://localhost:8000/api/broadcasting/auth");
authorizer.setHeaders(authHeaders);
PusherOptions options = new PusherOptions();
options.setAuthorizer(authorizer).setCluster(PUSHER_CLUSTER);
Pusher pusher = new Pusher(PUSHER_APP_KEY, options);

pusher.subscribePrivate("private-device." + deviceId, new PrivateChannelEventListener() {
    @Override
    public void onEvent(String channelName, String eventName, final String data) {
        System.out.println(String.format("Received event on channel [%s]", channelName));
    }

    @Override
    public void onSubscriptionSucceeded(String string) {
        System.out.println(String.format("Subscribed to channel [%s]", string));
    }

    @Override
    public void onAuthenticationFailure(String string, Exception excptn) {
        System.out.println(string);
    }
});

pusher.connect(new ConnectionEventListener() {
    @Override
    public void onConnectionStateChange(ConnectionStateChange change) {
        System.out.println("State changed to " + change.getCurrentState() +
                           " from " + change.getPreviousState());
    }

    @Override
    public void onError(String message, String code, Exception e) {
        System.out.println("There was a problem connecting!");
    }
});

// Keeping main thread alive
while (true) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

运行上面的代码时,它将在控制台上输出以下内容:

State changed to CONNECTING from DISCONNECTED
State changed to CONNECTED from CONNECTING
java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8000/api/broadcasting/auth

我确定auth:api中间件正在按我的期望在其他请求上正常工作。

这是我的routes/api.php的摘录:

Route::middleware('auth:api')->group(function () {
    Route::prefix('advertisements')->group(function () {
        Route::get('/request', 'AdvertisementsController@getDeviceAdvertisements')
            ->name('advertisements.getDeviceAdvertisements');
    });
});

这是从邮递员到该路由的测试(具有与上述相同的访问令牌):

Successful api authentication test on postman

这是从邮递员到api/broadcasting/auth路由的测试(具有与上述相同的访问令牌):

Failed api authentication on api/broadcasting/auth route

出什么问题了?为什么auth:api中间件下的所有api路由都能正常工作,但api/broadcasting/auth路由却不能正常工作?

注意

我尝试与公共渠道合作,没有问题。

1 个答案:

答案 0 :(得分:0)

经过一整天的搜索,终于解决了。

错误发生在授权频道时,而不是在使用auth:api中间件对请求进行身份验证时。

我在routes/channels.php中的私人频道授权功能始终返回false,这意味着它将拒绝对private-device.{device_id}频道的所有订阅请求:

Broadcast::channel('device.{device_id}', function ($device, $device_id) {
    // this always return false, because of inequality of types
    return $device->id === $device_id;
});

上面的授权函数总是返回false,因为$device->id(属于int类型)和$device_id(属于{{1类型)之间的类型不相等}}。

因此,为了解决该问题,我将它们都强制转换为string,然后检查是否相等。

这是我用来解决问题的代码:

int