2010-02-04 4 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

+0

예 ... j_security_check을 사용하고 있으며 폼 기반 인증을 다시 사용하는 sites.google.com에서 문서를 다운로드하는 코드를 시도했지만 제대로 작동하지 않았습니다. 이 사이트에서만 작동하지 않습니다. 브라우저의 요청은 POST/Site/block/send-receive-updates가됩니다. – user266443

답변

0

양식 기반 인증은 다른 요청과 크게 다르지 않습니다. 사용자 및 암호와 같은 필수 매개 변수를 제공하는 인증 양식에 요청을 제출하고 원본 페이지에서 가져와야하는 경우가있는 추가 토큰을 제출하기 만하면됩니다. 그런 다음 auth response 또는 session id 매개 변수에서 쿠키를 가져 와서 데이터를 가져올 다음 요청으로 복사해야합니다.

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을 고려, 적은 비 대한 코드와 동일 달성하기 위해.

관련 문제