Laravel:问"难道我登录"从机器人(未浏览器) [英] Laravel: asking "Am I Logged in?" from android (not browser)

查看:140
本文介绍了Laravel:问"难道我登录"从机器人(未浏览器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设使用Laravel框架一个RESTful API和它的工作pretty以及在浏览器上。
关键是,我还打算用它来从Android设备访问数据库。

I'm building a RESTful API using the Laravel framework and it's working pretty well on the browser. The thing is, I also intend to use it to access the DB from an Android device.

从移动设备,我能够登录进入我的应用程序。也就是说,我能够把用户名和密码发送到API,它会用这样的JSON

From the mobile device, I'm able to "log in" into my application. That is to say, I'm able to send the username and password to the API and it will respond with a JSON like this

{'action'=>'login', 'status'=>'success'}

到目前为止好,现在,说用户离开我的应用程序,回来后,我怎么知道,如果他们的会话仍处于活动?

So far so good, now, say the user leaves my app and comes back later, how do I Know if their session is still active?.

有内Laravel一个方法调用验证::检查:

There's a method within Laravel called Auth::check:

if (Auth::check())
{
    // The user is logged in...
}

但是,这总是返回假的!它那种有道理,我不发送任何给服务器说嘿!这是我一次是吗?

But this always returns false! And it kind of makes sense, I'm not sending anything to the server to say "Hey! it's me again"... right?.

我想我的问题归结为,是什么浏览器发送到API,因此识别出它?(我不认为这是用户和密码,发送此在每次你想请求将是非常不安全的,对吗?)

I guess my question comes down to, what is it that the browser is sending to the API so it recognizes it? (I don't think it is the user and password, as sending this over every time you want to make a request would be extremely insecure, am I right?)

推荐答案

下面是我落得这样做,

原来,你并不需要担心饼干,Android将管理它们。所有你需要做的就是确保你使用相同的HTTP客户端的所有请求

Turns out, you don't need to worry about cookies, Android will manage them for you. All you have to do is make sure you use the same http client for all your requests

private static DefaultHttpClient httpClient = new DefaultHttpClient();

所以,我创建了一个类,并把我的HttpClient那里。

So, I created a singleton class and put my httpClient there.

创建新的HttpClient每次都像在电脑上再关闭浏览器打开它。

Creating a new httpClient every time is like closing your browser and opening it again on the computer.

这篇关于Laravel:问"难道我登录"从机器人(未浏览器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆