2010-02-04 39 views
0

我是ICEfaces的新手,我有一个要求,我需要从给定的URL下载文档(http://ipaddress/formexec?objectid=201)。从使用基于Icefaces表单认证的服务器下载文件

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

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

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

是否有任何库或代码成功通过了用户名和密码下载文档。

+0

可以肯定:使用基于表单的身份验证,你的意思是说你正在使用'j_security_check'而不是一个本地的身份验证? – BalusC 2010-02-04 18:20:25

+0

是的...它使用j_security_check,我试着从sites.google.com下载一个文档,再次使用基于表单的身份验证,它正在工作。仅适用于本网站,它不起作用来自浏览器的请求将为POST/Site/block/send-receive-updates – user266443 2010-02-05 07:23:07

回答

0

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

1

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

这里有一个开球例如与“普通的香草” 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