java使用POST请求模拟登录openstack horizon问题请教

sowill 2019-05-15 04:25:53
我目前写了一段HttpClient模仿浏览器发送POST请求来登录openstack horizon的代码,目的是自己写一个登录页面来替代掉horizon本身的登录页面来实现登录,POST请求/dashboard/auth/login/,参数和csrftoken均能取到,大概代码如下:

CloseableHttpClient client = HttpClientBuilder.create().build();
URI uri = null;

Kv kv = getCookiesStr();
System.out.println("kv is :"+kv.toString());

List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username","admin"));
params.add(new BasicNameValuePair("password","123456"));
params.add(new BasicNameValuePair("region","http://10.0.11.211:5000/v3"));
params.add(new BasicNameValuePair("csrfmiddlewaretoken",kv.getStr("csrfmiddlewaretoken")));
params.add(new BasicNameValuePair("fake_email",""));
params.add(new BasicNameValuePair("fake_password",""));
params.add(new BasicNameValuePair("next","/dashboard/"));

uri = new URIBuilder().setScheme("http").setHost("10.0.11.71").setPort(80)
.setPath("/dashboard/auth/login/").setParameters(params).build();
HttpPost post = new HttpPost(uri);


String csrftoken = kv.getStr("csrftoken").split("=")[1];
post.setHeader("X-CSRFToken",csrftoken);
post.setHeader("Cookie",kv.getStr("csrftoken"));
post.setHeader("User-Agent","Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0");
post.setHeader("Host","10.0.11.71");
post.setHeader("Origin","10.0.11.71");
post.setHeader("Pragma","10.0.11.71");
post.setHeader("Cache-Control","no-cache");
post.setHeader("Connection","keep-alive");
post.setHeader("Upgrade-Insecure-Requests","1");
post.setHeader("Content-Type","application/x-www-form-urlencoded");
post.setHeader("Referer","http://10.0.11.71/dashboard/auth/login/?next=/dashboard/");
post.setHeader("Accept-Encoding","gzip, deflate");
post.setHeader("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
post.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

CloseableHttpResponse response = client.execute(post);
System.out.println("status+++:"+response.getStatusLine());
System.out.println("respone body+++:"+EntityUtils.toString(response.getEntity()));
System.out.println("respone header++:"+response.getAllHeaders());
for(Header header:response.getAllHeaders()){
System.out.println("header==>:"+header.getName()+":"+header.getValue());
}
System.out.println("status:++++++"+response.getStatusLine());


返回的response信息如下:
header==>:Date:Wed, 15 May 2019 06:38:16 GMT
header==>:Server:Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5
header==>:Content-Language:zh-cn
header==>:Expires:Wed, 15 May 2019 06:38:16 GMT
header==>:Vary:Cookie,Accept-Language
header==>:Cache-Control:no-cache, no-store, must-revalidate, max-age=0
header==>:X-Frame-Options:SAMEORIGIN
header==>:Set-Cookie:csrftoken=XWAZdvRupuND5oemj1qNeB7HZoiKHUCgOjvvAJrDEvONMdA6yF9DbrxVbqIy6XLN; Path=/
header==>:Set-Cookie:login_region=; expires=Thu, 14-May-2020 06:38:16 GMT; Max-Age=31536000; Path=/
header==>:Set-Cookie:login_domain=; expires=Thu, 14-May-2020 06:38:16 GMT; Max-Age=31536000; Path=/
header==>:Content-Length:9736
header==>:Keep-Alive:timeout=5, max=100
header==>:Connection:Keep-Alive
header==>:Content-Type:text/html; charset=utf-8
status:++++++HTTP/1.1 200 OK

看response body是直接给我返回到horizon自己的登录界面了,而且返回的header里没有sessionid,login_region也是空的,和正常浏览器请求登录返回的状态码302也不同,而keystone和apache2的日志里也没有任何报错日志,不知道哪位大佬试过用post来模拟登录openstack的horizon,情不吝赐教帮助我一下!感谢!
...全文
102 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
k2468183 2019-05-24
  • 打赏
  • 举报
回复
有没有外网地址
sowill 2019-05-15
  • 打赏
  • 举报
回复
引用 1 楼 代码间的舞者 的回复:
这说明你没有登录成功吧?

对,但是明明是按照浏览器的参数发的POST请求,不知道哪里出的问题,就是登录不成功
代码间的舞者 2019-05-15
  • 打赏
  • 举报
回复
这说明你没有登录成功吧?

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧