我将我的 alfresco 配置为使用外部 SSO。现在尝试通过在jsp页面中编写一个简单的java代码来测试它。代码在这里发布;
URL url1 = new URL("http://localhost:8080/share/page");
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setRequestProperty("x-alfresco-remote-user", "admin");
System.out.println("Request URL ... " + url1);
for (int i = 0;; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
System.out.println(headerName + "===");
System.out.println(headerValue);
if (headerName == null && headerValue == null) { break; }
}
String sessionValue = conn.getHeaderField("Set-Cookie").substring(0,conn.getHeaderField("Set-Cookie").indexOf(";"));
sessionValue = sessionValue.substring(sessionValue.indexOf("=")+1);
conn.setInstanceFollowRedirects(false);
HttpURLConnection.setFollowRedirects(false);
String newUrl = conn.getHeaderField("Location");
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("JSESSIONID", sessionValue);
conn.addRequestProperty("_alfTest", "_alfTest");
conn.addRequestProperty("_alfTest", "_alfTest");
conn.addRequestProperty("alfUsername3", "admin");
System.out.println("SECOND");
for (int i = 0;; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
System.out.println(headerName + "===");
System.out.println(headerValue);
if (headerName == null && headerValue == null) { break; }
}
conn.connect();
dis = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
while ((myString = dis.readLine()) != null)
{
System.out.println(myString);
}
response.setHeader("JSESSIONID", sessionValue);
response.setHeader("_alfTest", "_alfTest");
response.setHeader("alfUsername3", "admin");
response.sendRedirect(newUrl);
%>
输出变得像:
Request URL ... http://localhost:8080/share/page
null===
HTTP/1.1 302 Found
Server===
Apache-Coyote/1.1
Set-Cookie===
JSESSIONID=7F9DCDE35B45B91D245E3B544B29F8D2; Path=/share/; HttpOnly
Cache-Control===
no-cache
Location===
http://localhost:8080/share/page/user/admin/dashboard
Content-Type===
text/html;charset=utf-8
Content-Language===
en-US
Content-Length===
0
Date===
Fri, 19 Apr 2013 07:03:04 GMT
null===
null
http://localhost:8080/share/page/user/admin/dashboard
SECOND
null===
HTTP/1.1 200 OK
Server===
Apache-Coyote/1.1
Set-Cookie===
JSESSIONID=334D167D21B0910981804ECD14619D66; Path=/share/; HttpOnly
Cache-Control===
no-cache
Content-Type===
text/html;charset=utf-8
Content-Language===
en-US
Transfer-Encoding===
chunked
Date===
Fri, 19 Apr 2013 07:03:04 GMT
null===
null
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ....... the full html content of logged in user home page goes here.
但是通过 response.sendRedirect(newUrl); 重定向时 ,它再次询问用户名和密码。我怎样才能避免这种情况?需要在这里获取登录用户的主页。