abi*_*rto 1 php authentication basic-authentication laravel laravel-5.4
问题:我需要在使用 Laravel 5.4 创建的 API 上实现基本身份验证。由于我们需要在没有数据库的情况下实现它(只需从 获取凭据config()),我尝试创建一个注册中间件,如下所示:
<?php
namespace App\Http\Middleware;
class AuthenticateOnceWithBasicAuth
{
public function handle($request, $next)
{
if($request->getUser() != conf('auth.credentials.user') && $request->getPassword() != conf('auth.credentials.pass')) {
$headers = array('WWW-Authenticate' => 'Basic');
return response('Unauthorized', 401, $headers);
}
return $next($request);
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但这样我只能拥有整个 API 的一个凭据。我试图在配置中创建多个凭据,从请求中保存用户和密码,但是这样,它就像禁用了基本身份验证一样工作。
问题:有什么办法可以做到这一点吗?如何在不使用数据库的情况下在我的配置文件中拥有多个凭据?
您可以将您的授权用户名和密码作为集合保存在您的配置文件中。
config/myconfig.php
return [
'authorized_identities' => collect([
['user1','password1'],
['user2','password2'],
...
]),
];
Run Code Online (Sandbox Code Playgroud)
然后在你的中间件中
if(config('myconfig.authorized_identites')->contains([$request->getUser(),$request->getPassword()]))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4135 次 |
| 最近记录: |