【问题标题】:Download File from server that uses Icefaces form based authentication从使用基于 Icefaces 表单的身份验证的服务器下载文件
【发布时间】:2010-02-04 18:11:08
【问题描述】:

我是 ICEfaces 的新手,我有一个要求,我需要从给定的 url (http://ipaddress/formexec?objectid=201) 下载文档。

此 URL 使用通过 ICEFaces 部署的基于表单的身份验证

我跟踪了这​​个 URL 的请求,我得到以下行:

&ice.submit.partial=false&ice.event.target=loginForm%3Aj_id33&ice.event.captured=loginForm%3Aj_id33

是否有任何库或代码可以通过成功传递用户名和密码来下载文档。

【问题讨论】:

  • 可以肯定的是:使用基于表单的身份验证您的意思是说您使用的是j_security_check 而不是本土的?
  • 是的...它正在使用 j_security_check,我尝试使用代码从 sites.google.com 下载文档,该文档再次使用基于表单的身份验证,它正在工作。仅针对此站点无法正常工作来自浏览器的请求将是 POST /Site/block/send-receive-updates

标签: java jsf icefaces


【解决方案1】:

您需要从Set-Cookie 响应标头中提取jsessionid,并将其作为URL 属性附加到后续请求中,即http://example.com/path/page.jsf;jsessionid=XXX

这是一个借助“plain vanilla”java.net.URLConnection 的启动示例:

// Prepare stuff.
String loginurl = "http://example.com/login";
String username = "itsme";
String password = "youneverguess";
URLConnection connection = null;
InputStream response = null;

// First get jsessionid (do as if you're just opening the login page).
connection = new URL(loginurl).openConnection();
response = connection.getInputStream(); // This will actually send the request.
String cookie = connection.getHeaderField("Set-Cookie");
String jsessionid = cookie.split(";")[0].split("=")[1]; // This assumes JSESSIONID is first field (normal case), you may need to change/finetune it.
String jsessionidurl = ";jsessionid=" + jsessionid;
response.close(); // We're only interested in response header. Ignore the response body.

// Now do login.
String authurl = loginurl + "/j_security_check" + jsessionidurl;
connection = new URL(authurl).openConnection();
connection.setDoOutput(true); // Triggers POST method.
PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.write("j_username=" + URLEncoder.encode(username, "UTF-8")
          + "&j_password=" + URLEncoder.encode(password, "UTF-8"));
writer.close();
response = connection.getInputStream(); // This will actually send the request.
response.close();

// Now you can do any requests in the restricted area using jsessionid. E.g.
String downloadurl = "http://example.com/download/file.ext" + jsessionidurl;
InputStream download = new URL(downloadurl).openStream();
// ...

要以更少的臃肿代码实现相同的目标,请考虑Apache Commons HttpComponents Client

【讨论】:

    【解决方案2】:

    基于表单的身份验证与其他请求没有太大区别。您所要做的就是向身份验证表单提交请求,提供所需的参数,例如用户和密码,在某些情况下,您必须从源页面获取额外的令牌。然后,您需要从 auth 响应或会话 id 参数中获取 cookie,并将它们复制到将获取数据的下一个请求中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-16
      • 2018-12-11
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      相关资源
      最近更新 更多