【问题标题】:Better method to store user credentials存储用户凭据的更好方法
【发布时间】:2012-01-30 21:42:54
【问题描述】:

好的,所以我正在尝试为 iPhone 和 Android 开发一个移动网站应用程序。目前我的站点使用 cURL 将用户登录到另一个站点。我有一个 PHP 脚本,它根据用户的用户名创建一个 cookie。然后 cURL 将信息放入该 cookie 中。 cookie 存储在我网站的主机上。

基本上,我正在创建的这个移动网站假设允许用户登录我为其开发的论坛(网站所有者不允许我在他们的网站上创建移动版本,因此需要在我的网站上进行)。然后,一旦他们登录,他们就可以阅读帖子并回复他们。当它去读取一个线程需要加载 cookie 时,以及当他们尝试发帖时。

如何让 cookie 保存到用户的手机而不是我的服务器?我问的原因是,我希望这样我的主机不会被几十个带有用户凭据的文本文件填满(我不想看到,所以我不是网络钓鱼)。

我想要它,以便用户登录,cookie 被保存到手机中。他们想阅读电话拉起该cookie的帖子。他们想发帖,电话拉起饼干。

我查看了 PHP setcookie() 函数,不确定这是否是我需要的。

我们将不胜感激。

【问题讨论】:

    标签: android iphone web-applications curl session-cookies


    【解决方案1】:

    当您在服务器端设置 cookie 时,cookie 会通过称为 HTTP 标头的东西发送到客户端(在本例中为您的手机)。有一个名为“Set-Cookie”的 HTTP 标头和 cookie 的值。当浏览器将来向服务器发出请求时,它希望在名为“Cookie”的 HTTP Header 中返回该值

    因此,如果您想设置一个 cookie 并使用该 cookie,只需从您的请求中获取 cookie,将其存储在安全的地方,并在以后的请求中将其返回。

    http://en.wikipedia.org/wiki/HTTP_cookie

    这是一个简单的身份验证方法,它接受一个 url、一个用户名和一个密码并返回 cookie 值。

    static public String authenticate(String service_url, String username, String password) throws IOException
    {
        if (username == null || password == null)
            throw new IOException();
    
        String charset = "UTF-8";       
        URL url = new URL(service_url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="+charset);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setReadTimeout(5000); // 2 second timeout.
    
        String query = String.format("Email=%s&Password=%s",
                URLEncoder.encode(username, charset),
                URLEncoder.encode(password, charset));
    
        OutputStream output = null;
        try {
             output = connection.getOutputStream();
             output.write(query.getBytes(charset));
        } finally {
             if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
        }
    
        connection.getInputStream();
    
        List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
        if (cookies == null)
            throw new IOException();
    
        for (String cookie : cookies)
        {
            if (cookie.startsWith("authcookie"))
                return cookie; // this is the only correct path out.
        }
        throw new IOException();
    }
    

    HTTPGET 示例,注意 http 标头以将 cookie 值添加回请求。

    public static InputStream getDataFromHTTP(String url, String authenticationCookie, String     mimetype) throws ClientProtocolException, IOException
    {       
        DefaultHttpClient client = getHttpClient();     
    
        if (client == null)
            throw new IOException("Cant getHttpClient()");
    
        if (url == null)
            throw new IOException("URL is null");
    
        HttpGet httpget = new HttpGet(url);
        httpget.addHeader("Accept", mimetype);
        httpget.addHeader("Cookie", authenticationCookie);      
        httpget.addHeader("Accept-Encoding", "gzip");
        HttpResponse response = client.execute(httpget);
    
        InputStream instream = response.getEntity().getContent();
        Header contentEncoding = response.getFirstHeader("Content-Encoding");
    
        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
            instream = new GZIPInputStream(instream);
        }
    
        return instream;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      相关资源
      最近更新 更多