And*_*ili 9 php session laravel laravel-5 laravel-5.3
我不是很喜欢PHP和Laravel而且我有以下问题,我来自Java.
我正在按照本教程实现自定义用户提供程序:
https://blog.georgebuckingham.com/laravel-52-auth-custom-user-providers-drivers/
我正在使用Larave 5.3版本.
我简要地说明了我的需求:我的Laravel应用程序只是一个前端应用程序,所有业务逻辑(包括用户身份验证)都由暴露REST Web服务的Java后端应用程序执行.
拨打电话:
http://localhost:8080/Extranet/login
Run Code Online (Sandbox Code Playgroud)
并通过用户名和密码作为基本身份验证我获得了一个代表已记录用户的JSON响应:
{
"userName": "Painkiller",
"email": "painkiller@gmail.com",
"enabled": true
}
Run Code Online (Sandbox Code Playgroud)
因此,在我的Laravel应用程序中,我必须执行此调用,然后解析先前返回的JSON对象,以将经过身份验证的对象生成到前端应用程序会话中.
为此,我实现了之前的教程(似乎可行),实现了名为UserProvider的自定义用户提供程序类,该类实现了Laravel IlluminateUserProvider接口:
<?php
namespace App\Authentication;
use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;
use GuzzleHttp\Client;
use function GuzzleHttp\json_encode;
use function GuzzleHttp\json_decode;
use Illuminate\Support\Facades\Log;
class UserProvider implements IlluminateUserProvider
{
public function retrieveById($identifier)
{
// TODO: Implement retrieveById() method.
\Log::info('retrieveById START');
// PERFORM THE CALL TO MY BACK END WB SERVICE AND CREATE A NEW GenericUser USING THESE INFORMATION:
$attributes = array(
'id' => 123,
'username' => 'nobili.andrea@gmail.com',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
);
$user = new GenericUser($attributes);
return $user;
}
public function retrieveByToken($identifier, $token)
{
// TODO: Implement retrieveByToken() method.
\Log::info('retrieveByToken START');
}
public function updateRememberToken(Authenticatable $user, $token)
{
// TODO: Implement updateRememberToken() method.
\Log::info('updateRememberToken START');
}
public function retrieveByCredentials(array $credentials) {
// TODO: Implement retrieveByCredentials() method.
\Log::info('retrieveByCredentials START');
\Log::info('INSERTED USER CREDENTIAL: '.$credentials['email'] . ' ' .$credentials['password']);
$client = new Client(); //GuzzleHttp\Client
$response = $client->get('http://localhost:8080/Extranet/login',
[
'auth' => [
'nobili.andrea@gmail.com',
'pswd'
]
]);
$dettagliLogin = json_decode($response->getBody());
\Log::info('response: '.(json_encode($dettagliLogin)));
//$user = new User('Pippo', 'pippo@google.com', true);
$attributes = array(
'id' => 123,
'username' => 'nobili.andrea@gmail.com',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
);
$user = new GenericUser($attributes);
\Log::info('USER: '.(json_encode($user)));
return $user;
}
public function validateCredentials(Authenticatable $user, array $credentials)
{
// TODO: Implement validateCredentials() method.
\Log::info('validateCredentials START');
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这只是一个初步测试,因此返回的数据被模拟.
它以这种方式工作:
1)当用户在登录页面(http:// localhost:8000/login)中插入他的凭证时,它被称为retrieveByCredentials()方法:
public function retrieveByCredentials(array $credentials) {
// TODO: Implement retrieveByCredentials() method.
\Log::info('retrieveByCredentials START');
\Log::info('INSERTED USER CREDENTIAL: '.$credentials['email'] . ' ' .$credentials['password']);
$client = new Client(); //GuzzleHttp\Client
$response = $client->get('http://localhost:8080/Extranet/login',
[
'auth' => [
'nobili.andrea@gmail.com',
'pswd'
]
]);
$dettagliLogin = json_decode($response->getBody());
\Log::info('response: '.(json_encode($dettagliLogin)));
//$user = new User('Pippo', 'pippo@google.com', true);
$attributes = array(
'id' => 123,
'username' => 'nobili.andrea@gmail.com',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
);
$user = new GenericUser($attributes);
\Log::info('USER: '.(json_encode($user)));
return $user;
}
Run Code Online (Sandbox Code Playgroud)
执行Web服务调用以获取与此用户相关的用户信息.然后这些凭证由validateCredentials()方法验证(此时它每次都返回true).Finnaly它返回一个GenericUser对象,其中包含已登录用户的信息(此时被模拟,因为这是一个测试,我还没有考虑我的Web服务重新设置的JSON.
然后,当用户访问下一页(成功登录后)在我看来称为retrieveById($ identifier)方法,这个:
public function retrieveById($identifier)
{
// TODO: Implement retrieveById() method.
\Log::info('retrieveById START');
// PERFORM THE CALL TO MY BACK END WB SERVICE AND CREATE A NEW GenericUser USING THESE INFORMATION:
$attributes = array(
'id' => 123,
'username' => 'nobili.andrea@gmail.com',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
);
$user = new GenericUser($attributes);
return $user;
}
Run Code Online (Sandbox Code Playgroud)
此时逻辑是它使用先前记录的用户的id来执行对后端Web服务的调用,再次获取这些信息并创建将返回到使用它的下一页的相同GenericUser对象.现在我嘲笑这些用户信息.
好的,这有效,但出于安全原因我不能这样做.
所以我的想法是:当我在retrieveByCredentials(数组$凭证)中检索用户信息时,在检查了正确之后,我将把这个GenericUser对象放入会话中.
然后在retrieveById()方法中,我将从会话中检索这些信息.
我可以这样做吗?可能是一个聪明的方式?如何在会话中放入和检索对象\数据(我不是PHP和前端)
TNX
是的你可以。
当您使用 Laravel 的身份验证 API 登录用户时,它会自动存储在应用程序的会话中。所以你所做的完全没问题。
retrieveByCredentials当您通过 获取用户实例时,您无需担心诸如触发对 REST API 的调用之类的方法Auth::user()。每次调用此方法时,它都会检查是否有用户已经登录。如果有,它只返回用户,否则,它会进行必要的调用来查找该用户。
但我建议将对 REST API 的这些调用放在另一个类中。它UserProvider仅responsibility向 Laravel 展示如何检索用户,因此它应该利用delegation调用其他对象来完成在某处查找某些内容的肮脏工作。
session Auth您可以在 laravel 源代码中查看有关该类如何工作的更多信息:
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Auth/SessionGuard.php