我尝试使用我的Laravel 5.4应用验证我的单页应用(用Marionette编写)。根据我之前的经验,SPA会:
发送用户名&密码到{
"access_token":"-wt7x8UPRH9GcbTeUQ3wIA7zuc02XeHil4qsoKJcOUU",
"type":"Bearer",
"expires_in":2570,
"refresh_token":"g9zmNkC1zpQ8fM0uSeJdy_VZe7TuBUG4MYGIBuAtXvg"
}
端点,返回的内容如下:
access_token
SPA会将access_token
存储在浏览器的本地存储中,并在access_token
标题中将其发送给后端的每个后续请求
/oauth/token
并对用户进行身份验证不幸的是,我很难理解如何在Laravel 5.4中实现这一目标。
我需要使用Passport吗?我真的不认为我需要OAuth2,但Passport是否也提供简单的基于令牌的身份验证?似乎,我从/api/user
端点获得了上述预期的令牌,但我不知道如何使用此令牌。我甚至不认为它是为了这个目的。
我尝试向标题中的此标记发送请求expires_in
,发送它,并作为查询字符串,没有运气。另外让我担心的是,Laravel的TokenGuard
是年(31536000秒= 365天),这似乎太长了。我担心这个Passport OAuth令牌实际上是用于OAuth2访问,而不是我过去使用的1天即将到期的访问令牌。
我还阅读了Laravel的api_token
,但这似乎是一些奇怪的令牌存储在user
表的UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
列中,我的心态完全错了。例如,它不会过期,并且它不是每用户会话,这意味着必须从多个设备使用相同的令牌。等
非常困惑......感激任何帮助!
答案 0 :(得分:1)
您可以尝试使用JWT,本教程可能会为您带来灵感:https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps
答案 1 :(得分:0)
我实际上最终使用了Passport,基于在https://laracasts.com/discuss/channels/code-review/api-authentication-with-passport/replies/282168
找到的一些非常有用的代码