Java HttpClient和设计身份验证问题
我在web服务器上运行Desive,并试图获得一个Java应用程序进行身份验证。应用程序通过身份验证后,Deave应授权应用程序在web服务器上创建和更新记录 我正在查看rails服务器日志,以比较web表单发布的内容和我的应用程序。以下是不同的输出: 网络表单(工作) 参数:{“utf8”=>“Γ”;“Authentity_token”=>“vYC9qd0dVIUH7B/WCHW59JWZQUX4YAOGXZ32PBN1SO=”,“user”=>{“username”=>“user”,“password”=>“[筛选]”,“membered(me”=>“0”},“commit”=>“Sign-in”} 应用程序(不工作) 参数:{“utf8”=>“?”,“用户名”=>“用户”,“密码”=>“[已筛选],“提交”=>“登录”} 这是我的应用程序代码Java HttpClient和设计身份验证问题,java,ruby-on-rails,devise,httpclient,Java,Ruby On Rails,Devise,Httpclient,我在web服务器上运行Desive,并试图获得一个Java应用程序进行身份验证。应用程序通过身份验证后,Deave应授权应用程序在web服务器上创建和更新记录 我正在查看rails服务器日志,以比较web表单发布的内容和我的应用程序。以下是不同的输出: 网络表单(工作) 参数:{“utf8”=>“Γ”;“Authentity_token”=>“vYC9qd0dVIUH7B/WCHW59JWZQUX4YAOGXZ32PBN1SO=”,“user”=>{“username”=>“user”,“pas
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
}
}
我认为在发出post请求时,您还需要发送您的
authenticity\u令牌
"authenticity_token"=>"vYC9qd0dVIUH7B/wCHW59JwZquX4yaiogXZ32pbn1So="
当用户查看表单以创建、更新或销毁资源时,
rails应用程序将创建一个随机令牌,并存储此令牌
标记,并将其放置在表单中的隐藏字段中。什么时候
用户提交表单后,rails将查找
将其与会话中存储的令牌进行比较,然后
如果它们匹配,则允许继续请求
最后一行显示
如果它们与请求匹配,则允许继续。
这在您从java代码发出post请求时不会发生。考虑到哪种请求有效,我会尝试以下方法:
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"));
来自web页面的请求是什么样子的?我不认为HTTP有办法发送这样的对象,我怀疑ror有办法将某些字符串识别为序列化对象。我是否发布了您要求的bdares?我知道这是一个问题,但我目前已禁用CSRF。在应用程序\u controller.rb中,“保护\u免受\u伪造”被注释掉。所以,我认为这不是问题所在。如果你知道我如何获得代币并将其添加进去,那就太好了!对于cookie处理,您需要使用java内置的
cookie类
,或者检查http客户端
文档以进行cookie/session处理和管理。我不是rails专家,但我是rails专家。提到了一种不同的方法来删除auth_令牌。我添加了config.action_controller.allow_forgery_protection=false,就像那篇文章建议的那样。这确实在我使用web表单时删除了Authentity令牌,但java应用程序仍然无法进行身份验证。哈,我发誓我已经尝试过了!它成功了,谢谢你让我在我的努力中勤奋。
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"));