Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel网页中使用令牌身份验证_Php_Laravel_Authentication_Jwt - Fatal编程技术网

Php 如何在laravel网页中使用令牌身份验证

Php 如何在laravel网页中使用令牌身份验证,php,laravel,authentication,jwt,Php,Laravel,Authentication,Jwt,我正在尝试将JWT用于laravel网页,而不是会话。所以我做了一些改变 安装和 然后在config/auth.php中将默认保护更改为api 'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ], 'guards' => [ ... 'api' => [ 'driver' => 'token', 'provider' =>

我正在尝试将JWT用于laravel网页,而不是会话。所以我做了一些改变

  • 安装和

  • 然后在config/auth.php中将默认保护更改为
    api

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],
    
    'guards' => [
        ...
    
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],
    
  • 现在我犯了一个错误

    (1/1)对未定义方法的FatalErrorException调用 AuthenticatesUsers.php(第行)中的illumb\Auth\TokenGuard::trunt() 75)


    如何修复此问题并启动laravel网页的令牌身份验证(刀片不是API)。

    我认为您可以尝试以下方法:

    'defaults' => [
            'guard' => 'web',
            'passwords' => 'users',
        ],
    
    'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],
    
            'api' => [
                'driver' => 'token',
                'provider' => 'users',
            ],
    
    编辑

    你可以从图书馆找到一些帮助。在本例中,您需要关注如何配置和使用令牌基身份验证


    希望这对你有帮助

    请参考这个。如果您使用api作为默认值,那么laravel身份验证将抛出一个错误

    Laravel使用默认的基于会话的即时身份验证和您已有的默认脚手架用户视图控制器。你有额外的方法来添加你自己的,所以你可以根据需要使用防护

    因此,正如@KevinPatel所建议的,恢复到默认配置,然后在路由中:将要进行JWT身份验证的路由分组,添加JWT中间件,在这种情况下,您必须更新负责身份验证的控制器以使用JWTAuth而不是默认身份验证

    如果你需要更好地理解它,你应该检查这个答案


    推荐的一种方法是使用(当然,您不是在构建api,但是),因为Dingo已经为身份验证和其他路由管理添加了一些功能-因此使用和配置都非常简单

    我也在使用jwt保护我们的api。您应该更改配置,如下所示:

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],
    
    'guards' => [
        ...
    
        'api' => [
            'driver' => 'jwt', // KEY POINT!!!
            'provider' => 'users',
        ],
    ],
    
    确保jwt库安装正确:

  • Tymon\JWTAuth\Providers\LaravelServiceProvider::class已添加到config/app.php中

  • 如果在提供者中使用雄辩的模型,则用户模型实现JWTSubject接口


  • 我在这里找到了解决方案

    在/routes/api.php中-添加了一些基本的身份验证路由

    Route::post('login', 'Auth\LoginController@login');
    
    Route::get('/user', function (Request $request) {
        $user = $request->user();
        return dd($user);
    })->middleware('auth:api');
    
    在/app/http/Controller/auth/LoginController.php中

    然后重写登录控件中的方法

    public function login(Request $request)
    {
        $credentials = $request->only(["email","password"]);
        if ($token = $this->guard()->attempt($credentials)) {
            return $this->sendLoginResponse($request, $token);
        }
    
        $this->incrementLoginAttempts($request);
        return $this->sendFailedLoginResponse($request);
    }
    
    protected function sendLoginResponse(Request $request, $token)
    {
        $this->clearLoginAttempts($request);
    
        return $this->authenticated($request, $this->guard()->user(), $token);
    }
    
    protected function authenticated(Request $request, $user, $token)
    {
        setcookie("jwt_token", $token);
        return redirect('/');
        return response()->json([
            'token' => $token,
        ]);
    }
    
    protected function sendFailedLoginResponse(Request $request)
    {
        return response()->json([
            'message' => "not found",
        ], 401);
    }
    
    添加中间件
    AddToken

    public function handle($request, Closure $next)
    {
        $token = isset($_COOKIE["jwt_token"])?$_COOKIE["jwt_token"]:"";
        //$request['token'] = $token;//this is working
        $request->headers->set("Authorization", "Bearer $token");//this is working
        $response = $next($request);
        //$response->header('header name', 'header value');
        return $response;
    }
    
    在Kernel.php中注册中间件

     protected $middleware = [
        ....
        \App\Http\Middleware\AddToken::class,
    ];
    

    我想使用令牌身份验证而不是会话。您是否使用了
    $php artisan供应商:publish--provider=“Tymon\JWTAuth\Providers\JWTAuthServiceProvider”
    并在配置文件中设置了密钥?是的,我使用了:我想您需要在路由中使用
    ['middleware'=>'auth:api']
    。试试这个,让我知道结果!它对你有效吗?它将在身份验证后工作,但我在通过laravel默认脚手架登录页面提交身份验证时遇到此错误。是的,老板,我遇到此错误。那么你的建议是什么呢?回到
    'defaults'=>['guard'=>'web','passwords'=>'users',],
    美妙的建议@C2486很有魅力。在LoginController(web)中,我添加了` authenticified(…)'''$$token=auth()->尝试($credentials);和setcookie('jwt_token',$token);在LogoutController'getLogout()'return redirect()..->withCookie(\Cookie::forget('jwt_token'));´