2012-01-26 19 views
3

我正在web服務器上運行設計,我試圖讓一個Java應用程序進行身份驗證。應用程序通過驗證後,設計應授權應用程序在Web服務器上創建和更新記錄。Java HttpClient和設計身份驗證問題

我在看rails服務器日誌,比較一下web表單發佈與我的應用程序。下面是不同的輸出:

Web表單(工作)

參數:{ 「UTF8」=> 「Γ£O」, 「authenticity_token」=> 「vYC9qd0dVIUH7B/wCHW59JwZquX4yaiogXZ32pbn1So =」,「用戶「=> {」username「=>」user「,」password「=>」[FILTERED]「,」remember_me「=>」0「},」commit「=>」登錄「}

應用程序(不工作)

參數:{ 「?」 「UTF8」=> 「用戶名」=> 「用戶」, 「密碼」=> 「[FILTERED]」, 「提交」=> 「登錄」}

這是我的應用代碼

public void webLogin(String methodName, String username, String password) {   
     httpPost = new HttpPost(webServiceUrl+methodName); 

     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("utf8", Character.toString('\u2713'))); 
      nameValuePairs.add(new BasicNameValuePair("username", username)); 
      nameValuePairs.add(new BasicNameValuePair("password", password)); 
      nameValuePairs.add(new BasicNameValuePair("commit", "Sign in")); 
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpClient.execute(httpPost); 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 
    } 

明顯的區別在於用戶名和密碼嵌套在用戶結構中。所以我的問題是,我該如何創建該結構?

編輯HTTP頭信息

Request URL:http://localhost:3000/users/sign_in 
    Request Method:POST 
    Status Code:302 Found 
    Request Headersview source 
    Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
    Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
    Accept-Encoding:gzip,deflate,sdch 
    Accept-Language:en-US,en;q=0.8 
    Cache-Control:max-age=0 
    Connection:keep-alive 
    Content-Length:197 
    Content-Type:application/x-www-form-urlencoded 
    Cookie:_WebApp_session=BAh7CEkiCmZsYXNoBjoGRUZvOiVBY3Rpb25EaXNwYXRjaDo6Rmxhc2g6OkZsYXNoSGFzaAk6CkB1c2VkbzoIU2V0BjoKQGhhc2h7BjoKYWxlcnRUOgxAY2xvc2VkRjoNQGZsYXNoZXN7BjsKSSIfSW52YWxpZCBlbWFpbCBvciBwYXNzd29yZC4GOwBUOglAbm93bzokQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaE5vdwY6C0BmbGFzaEAHSSIPc2Vzc2lvbl9pZAY7AEZJIiU4ODVjM2QxNmNkMTI4YWYxN2E5YzEyNmRjYWI1OTI1YgY7AFRJIhBfY3NyZl90b2tlbgY7AEZJIjFNVkV4aFNoSnZsQmhxRUFVdjZtZ0ZBblNVQzJjbU1Kb0l3N1U1OHErNUlVPQY7AEY%3D--8b8a1fcee6c194b938781fa60ddb3a91b1ac8c7c 
    Host:localhost:3000 
    Origin:http://localhost:3000 
    Referer:http://localhost:3000/users/sign_in 
    User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7 
    Form Dataview URL encoded 
    utf8:✓ 
    authenticity_token:MVExhShJvlBhqEAUv6mgFAnSUC2cmMJoIw7U58q 5IU= 
    user[username]:user 
    user[password]:password 
    user[remember_me]:0 
    commit:Sign in 
    Response Headersview source 
    Cache-Control:no-cache 
    Connection:Keep-Alive 
    Content-Length:88 
    Content-Type:text/html; charset=utf-8 
    Date:Thu, 26 Jan 2012 03:57:57 GMT 
    Location:http://localhost:3000/ 
    Server:WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09) 
    Set-Cookie:_WebApp_session=BAh7CUkiCmZsYXNoBjoGRUZvOiVBY3Rpb25EaXNwYXRjaDo6Rmxhc2g6OkZsYXNoSGFzaAk6CkB1c2VkbzoIU2V0BjoKQGhhc2h7ADoMQGNsb3NlZEY6DUBmbGFzaGVzewY6C25vdGljZUkiHFNpZ25lZCBpbiBzdWNjZXNzZnVsbHkuBjsAVDoJQG5vd286JEFjdGlvbkRpc3BhdGNoOjpGbGFzaDo6Rmxhc2hOb3cGOgtAZmxhc2hAB0kiD3Nlc3Npb25faWQGOwBGSSIlYmZkMDkwNDA1ZGZkN2JmZTA1OWM0MDNmZDIxYzU1ODMGOwBUSSIQX2NzcmZfdG9rZW4GOwBGSSIxTVZFeGhTaEp2bEJocUVBVXY2bWdGQW5TVUMyY21NSm9JdzdVNThxKzVJVT0GOwBGSSIZd2FyZGVuLnVzZXIudXNlci5rZXkGOwBUWwhJIglVc2VyBjsARlsGaQciIiQyYSQxMCR0b0tHc0FJTWpHRXQxSXp1aWVCRlBl--9e1e39fb97a4452e3693c65cc5f1ac4ad9855c37; path=/; HttpOnly 
    X-Runtime:0.231013 
    X-Ua-Compatible:IE=Edge 
+0

什麼從網頁樣子的要求嗎?我不認爲HTTP有辦法發送這樣的對象,我懷疑ror有某種方式將某些字符串識別爲序列化對象。 – bdares

+0

我發佈了您要求提供的服務嗎? – david

回答

2

看到,以什麼樣的要求是工作,我想試試這個:

nameValuePairs.add(new BasicNameValuePair("authenticity_token", myToken)); 
nameValuePairs.add(new BasicNameValuePair("user[username]", username)); 
nameValuePairs.add(new BasicNameValuePair("user[password]", password)); 
nameValuePairs.add(new BasicNameValuePair("user[remember_me]", 0)); 
nameValuePairs.add(new BasicNameValuePair("commit", "Sign in")); 
+0

哈,我發誓我試過了!它的工作,感謝讓我努力在我想要的。 – david

0

我認爲,同時使POST請求,你還需要發送您的authenticty_token

"authenticity_token"=>"vYC9qd0dVIUH7B/wCHW59JwZquX4yaiogXZ32pbn1So=" 

What authenticity_token does ?

當用戶查看錶單創建,更新,或破壞資源, Rails應用程序將創建一個隨機authenticity_token,存儲在會話中 令牌和地點它在窗體的隱藏字段中。當用戶提交表單時,rails會查找 authenticity_token,將其與會話中存儲的值進行比較,如果匹配請求被允許繼續, 將與 進行比較。

在最後一行中,它表示if they match the request is allowed to continue.當您從Java代碼發出發佈請求時,不會發生這種情況。

+0

我知道這是一個問題,但我目前禁用了CSRF。在application_controller.rb中,「protect_from_forgery」被註釋掉。所以,我不認爲這是問題。 – david

+0

如果您知道如何獲得令牌並添加該令牌,那就太棒了! – david

+0

對於cookie處理,您需要使用java內置的'cookie class'或者查看'http-client'文檔來獲取cookie /會話處理和管理。 – RanRag