【问题标题】:rails Devise http authenticating mobilerails 设计 http 身份验证移动设备
【发布时间】:2011-10-27 05:55:03
【问题描述】:

我正在尝试向使用 Devise gem 的服务器 ruby​​ on rails 应用程序验证 android 客户端应用程序。但我已经尝试过 http 身份验证,并发布请求进行身份验证,服务器只对任何给定的用户名/密码响应 200。

我已经在用户模型中设置了 config.http_authenticable = true 和 :database_authenticable...

我会发布我的身份验证方法,所以你们可以看看它......

public static boolean authenticate(User user, String verb) throws IOException, JSONException
{

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(verb);

     CredentialsProvider credProvider = new BasicCredentialsProvider();
     credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(user.getMail(), user.getPassword()));

    httpClient.setCredentialsProvider(credProvider);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
    nameValuePairs.add(new BasicNameValuePair("email", user.getMail()));  
    nameValuePairs.add(new BasicNameValuePair("password", user.getPassword()));  
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse httpResponse = httpClient.execute(httpPost);


    int statusCode = httpResponse.getStatusLine().getStatusCode();
    //JSONObject resp = null;

     if (statusCode < 200 || statusCode >= 300){
        throw new IOException("Error");
     }


    return true;
}

【问题讨论】:

    标签: android ruby-on-rails devise http-post http-authentication


    【解决方案1】:

    如果服务器响应 200,这听起来确实像服务器端配置,因此您应该使用桌面 Web 浏览器和 Fiddler 之类的工具仔细检查您的 URL 是否确实受到保护,这样您就可以查看所有内容。特别注意身份验证标头和状态代码;至少您应该会看到来自服务器的 401 以开始操作。

    您还可以在您的设备上打开 Apache HTTP 诊断,它还会将标头和内容转储到 LOGCAT,因此您可以确保一切正常。

    检查 WWW-Autnenticate 标头的内容,它将指定接受哪些方案。客户端会重新请求该 URL,但它会将 Authorization 标头放入其请求中。

    简而言之,确保您的服务器端在您的应用程序之外工作,在一个更容易排除故障的环境中。

    客户端,您似乎只激活了 BASIC 身份验证(每个人都停止使用它!),并且您的端点可能只需要 DIGEST 或 NTLM 或 KERBEROS 或 除 BASIC 之外的任何其他身份验证方案。由于您似乎没有设置 SSL,请务必至少使用 DIGEST,否则您有明文问题!

    使用表单变量(用于身份验证)仅适用于应用程序级别,而不是 HTTP 协议级别,它使用 HTTP 标头(WWW-Autnenticate,授权)和状态代码(401、403)进行身份验证过程。同样,如果您没有将服务器(和客户端)配置为仅使用 SSL,则会出现明文问题。

    【讨论】:

      猜你喜欢
      • 2014-07-20
      • 2011-07-23
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多