我有一个后端C ++应用程序,它使用标准TCP连接接收JSON请求。此应用程序管理所有业务逻辑(用户身份验证,事务处理,数据请求和验证)。
如何设置Laravel 5.2连接到此服务器以进行用户身份验证和事务处理?我不需要Laravel端的任何数据库,因为所有数据都将通过C ++应用程序访问。
作为奖励,我还想将JWT纳入用户身份验证部分,如果可能的话。
以下代码是我目前使用标准PHP连接到应用程序服务器的方式。我想要相同的功能,但需要更多的Laravel方式。
class tcp_client
{
private $sock;
function __construct()
{
// create the socket
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!is_resource($this->sock))
{
// throw exception
}
// set socket options
$this->set_options();
}
function connect($host, $port)
{
$timeout = 3;
$startTime = time();
while (!socket_connect($this->sock, $host, $port))
{
if ((time() - $startTime ) >= $timeout)
{
// throw exception
}
sleep(1);
}
}
private function set_options()
{
if (!socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 5,
'usec' => 0)))
{
// throw exception
}
if (!socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 5,
'usec' => 0)))
{
// throw exception
}
}
public function request($request)
{
// the first 6 characters will indicate the length of the JSON string
$request = str_pad(strlen($request), 6, '0', STR_PAD_LEFT) . $request;
//Send the message to the server
if (!socket_send($this->sock, $request, strlen($request), 0))
{
// throw exception
}
//Now receive header from server
$header = 0;
if (socket_recv($this->sock, $header, 6, MSG_WAITALL) === FALSE)
{
// throw exception
}
//Now receive body from server
$body = "";
if (socket_recv($this->sock, $body, $header, MSG_WAITALL) === FALSE)
{
// throw exception
}
return $body;
}
}
答案 0 :(得分:1)
我已经设法通过模仿DatabaseUserProvider来自行解决这个问题。
使用子文件夹App\Blah
和App\Blah\Auth
App\Blah\TCP
创建新的用户提供商
> php artisan make:provider App\Blah\Auth\BlahUserProvider
将\vendor\laravel\framework\src\Illuminate\Auth\DatabaseUserProvider.php
的内容复制到新的提供商(BlahUserProvider.php
),并将类名更改回BlahUserProvider
。
创建了App\Blah\TCP\TCPClient.php
并将我问题中的课程内容复制到此文件中。
更改名称空间并使用TCPClient
中的stdClass
和BlahUserProvider.php
namespace App\Blah\Auth;
use App\Blah\TCP\TCPClient;
use stdClass;
用{/ 1>替换retrieveByCredentials
中的函数BlahUserProvider
的内容
public function retrieveByCredentials(array $credentials)
{
$tcp_request = "{\"request\":\"login\","
. "\"email\":\"" . $credentials['email'] . "\","
. "\"password\":\"" . $credentials['password'] . "\"}";
$tcp_result = json_decode(str_replace("\n","\\n",$this->conn->request($tcp_request)), true);
$user = new stdClass();
$user->id = $tcp_result['user']['id'];
$user->name = $tcp_result['user']['name'];
return $this->getGenericUser($user);
}
我还将函数retrieveById
替换为与函数retrieveByCredentials
相同的内容,这样用户就可以登录,因为我仍然需要在C ++应用程序中创建请求。
扩展createUserProvider
中的\vendor\laravel\framework\src\Illuminate\Auth\CreatesUserProviders.php
功能,以包含我的新驱动程序,并添加了createBlahProvider
public function createUserProvider($provider)
{
$config = $this->app['config']['auth.providers.' . $provider];
if (isset($this->customProviderCreators[$config['driver']]))
{
return call_user_func(
$this->customProviderCreators[$config['driver']], $this->app, $config
);
}
switch ($config['driver'])
{
case 'database':
return $this->createDatabaseProvider($config);
case 'eloquent':
return $this->createEloquentProvider($config);
case 'blah':
return $this->createBlahProvider($config);
default:
throw new InvalidArgumentException("Authentication user provider [{$config['driver']}] is not defined.");
}
}
protected function createBlahProvider($config)
{
$connection = new \App\Blah\TCP\TCPClient();
return new \App\Blah\Auth\BlahUserProvider($connection, $this->app['hash'], $config['model']);
}
将config\auth.php
中的提供程序更改为blah用户提供程序
'providers' => [
'users' => [
'driver' => 'blah',
'model' => App\User::class,
],