And*_*ili 5 php laravel laravel-5 laravel-authorization
我是PHP和Laravel新手,遇到以下问题。
我知道 Laravel 提供了一个现成的登录系统,使用以下语句创建:
php artisan make:auth
Run Code Online (Sandbox Code Playgroud)
问题是这个系统直接与数据库交互。
我的情况有所不同,因为我的 Laravel 应用程序仅实现了前端。所有业务逻辑均由公开 REST Web 服务的 Java 后端应用程序处理。
基本上 Laravel 前端应用程序是这样的:
1)显示登录表单(用户名和密码)的视图。
2) 一个控制器类,其中包含一个方法,该方法接收先前表单的提交,然后调用后端应用程序的 REST Web 服务(发送一个具有包含插入的用户名和密码的授权标头的请求)。
后端应用程序将返回(到前面的 Laravel 控制器方法)一个包含用户信息作为响应的JSON对象,如下所示(如果用户被授权)...
{
"userName": "Painkiller",
"email": "painkiller@gmail.com",
"enabled": true
}
Run Code Online (Sandbox Code Playgroud)
...或者,在用户未经授权的情况下,类似这样的事情...
{
"timestamp": 1485183649134,
"status": 401,
"error": "Unauthorized",
"message": "Credenziali non valide",
"path": "/Extranet/login"
}
Run Code Online (Sandbox Code Playgroud)
我最初的想法是编写一个执行这些操作的自定义控制器:
我不太热衷于前端开发,但我认为它应该可以工作,但是......我正在远离 Laravel 架构和 Laravel 逻辑。
因此,我的 Laravel 应用程序无法直接与数据库通信,但我想也许我可以尝试采用 Laravel 架构。
所以我想在我的 Laravel 项目中默认有\app\Http\Controllers\Auth\LoginController类代表标准 Laravel 登录系统,这个:
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
Run Code Online (Sandbox Code Playgroud)
它包含这一行:
use AuthenticatesUsers;
Run Code Online (Sandbox Code Playgroud)
我不太喜欢 PHP,这行代码到底是做什么的?起初,我认为它向LoginController添加了AuthenticatesUsers功能,但它更多的是与扩展和继承概念相关的行为。
无论如何, AuthenticatesUsers类似乎包含处理登录的逻辑的实现,该方法:
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
Run Code Online (Sandbox Code Playgroud)
所以我在想:我可以重写这个方法来执行对我的Web服务的调用,获取与登录用户相关的JSON对象,将此信息放入会话中并重定向到用户页面吗?
这对于我的目的来说是一个明智的解决方案吗?
小智 3
您不必这样做,您所要做的只是创建您的路线并覆盖登录方法或创建您自己的路线。
路线:
// authentication routes
Route::get('/login', 'Auth\LoginController@showLoginForm')->name('adminGetLogin');
Route::post('/login', 'Auth\LoginController@login')->name('adminPostLogin');
Route::get('/logout', 'Auth\LoginController@logout')->name('adminLogout');
// reset password routes
Route::get('/password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('showResetForm');
Route::post('/password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('sendResetEmail');
Route::get('/password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('showResetForm');
Route::post('/password/reset', 'Auth\ResetPasswordController@reset')->name('resetPassword');
Run Code Online (Sandbox Code Playgroud)
网络
public function login(Request $request){
if (! Auth::attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1])) {
// do whatever yo desire
return redirect('/home');
}
Run Code Online (Sandbox Code Playgroud)
休息API
public function login(Request $request){
if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1])) {
// do whatever yo desire
$user = Auth::user();
return response()->json([
'Error' => ['type' => 'success', 'desc' => 'Login successful', 'code' => 0],
'Response' => $user
]);
}
Run Code Online (Sandbox Code Playgroud)