簡體   English   中英

rails Devise http 認證手機

[英]rails Devise http authenticating mobile

我正在嘗試在使用 Devise gem 的 rails 應用程序上向我的服務器 ruby 驗證 android 客戶端應用程序。 但是我已經嘗試過 http 身份驗證,並發布身份驗證請求,服務器只對任何給定的用戶名/密碼響應 200。

我已經在用戶 model 處設置了 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;
}

如果服務器響應 200,這聽起來確實像服務器端配置,因此您應該使用桌面 web 瀏覽器和 Fiddler 之類的工具仔細檢查您的 URL 是否確實安全,這樣您就可以看到所有內容。 特別注意身份驗證標頭和狀態代碼; 至少你應該從服務器看到一個 401 來啟動事情。

您還可以在您的設備上打開 Apache HTTP 的診斷,它還會將標頭和內容轉儲到 LOGCAT,因此您可以確保一切正常。

檢查 WWW-Autnenticate 標頭的內容,它將指定接受哪些方案。 客戶端將重新請求 URL,但會將授權 header 放入其請求中。

簡而言之,確保你的服務器端在你的應用程序之外工作,在一個更容易排除故障的環境中。

客戶端,看起來您只是在激活 BASIC 身份驗證(每個人都停止使用它),並且您的端點可能只需要 DIGEST 或 NTLM 或 KERBEROS 或BASIC 以外的任何其他身份驗證方案 由於您似乎沒有為 SSL 設置,所以至少要使用 DIGEST,否則您有明文問題!

使用表單變量(用於身份驗證)僅適用於應用程序級別,而不是 HTTP 標頭(WWW-Autnenticate,授權)和狀態代碼(401、403)用於身份驗證過程的協議級別。 同樣,如果您沒有為僅 SSL 配置您的服務器(和客戶端),則會出現明文問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM