Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use middleware to prevent illegal login and access control in laravel

Shulou Source: shulou.com Published: 2022-06-01 02:29:46 10月03日 Update

This article Xiaobian introduces in detail for you "laravel how to use middleware to prevent illegal login and access control", the content is detailed, the steps are clear, and the details are handled properly. I hope this "laravel how to use middleware to prevent illegal login and access control" article can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.

1. First, let's open artisan and enter the following command line to create a middleware file

Php artisan make:middleware AdminLogin

After executing the command, we will find that there is an extra AdminLogin file in the Middleware folder.

2. Then we open kernel.php and add this middleware to the routing middleware

Protected $routeMiddleware = ['auth' = >\ App\ Http\ Middleware\ Authenticate::class,' auth.basic' = >\ Illuminate\ Auth\ Middleware\ AuthenticateWithBasicAuth::class, 'can' = >\ Illuminate\ Foundation\ Http\ Middleware\ Authorize::class,' guest' = >\ App\ Http\ Middleware\ RedirectIfAuthenticated::class, 'throttle' = >\ Illuminate\ Routing\ Middleware\ ThrottleRequests::class,' admin.login' = >\ App\ Http\ Middleware\ AdminLogin::class,]

You can see that we point to the middle section class in this array with an index of admin.login.

3. Then add the middleware to the association on the route

Route::group (['middleware' = > [' web','admin.login.login']], function () {Route::get ('/ admin/login/login','Admin\ LoginController@login'); / / login page Route::get ('/ admin/index/index','Admin\ IndexController@index'); / / background homepage routing})

Here, the login route and background route must be put into an associated middleware. Only by putting it in the same middleware can session be shared.

4. Finally, we modify the original code in AdminLogin.php under Middleware.

Here, let's make a simple judgment to see if session exists. If so, just follow the route we are visiting now. If there is no session, redirect him to the login page.

A simple anti-illegal login can be used.

We can also use this method to do rbac, or to set up a middleware to judge by session and check whether the administrator in the library has the right to access the route.

You may need it at this point.

Route = $request- > path (); read here, this article "how to use middleware to prevent illegal login and access control" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more related articles, welcome to follow the industry information channel.

Tags: Middleware login routing permissions control file article content background command page association proper admiral subscript code original only idea point to Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple Redmi Xiaomi Linux