簡體   English   中英

Servlet請求到另一個具有授權的Servlet

[英]Servlet request to another servlet with authorization

我有一些應用程序的tomcat服務器。 該服務器使用標准的j_secure_check身份驗證。 在另一台tomcat服務器上,我需要部署必須是第一台服務器的代理的應用程序。 因此,我需要從另一個servlet調用servlet,但是首先需要使用j_secure_check進行身份驗證。 可以有計划地這樣做嗎?

您需要獲取會話cookie並將其傳遞給后續請求。

String url = "http://example.com/j_security_check?j_username=foo&j_password=bar";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

if (connection.getResponseCode() == 200) { // 200 = OK, 401 = unauthorized
    String cookie = connection.getHeaderField("Set-Cookie").split(";", 2)[0];

    url = "http://example.com/somepage.jsp";
    connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestProperty("Cookie", cookie);
    InputStream input = connection.getInputStream();
    // You can now write it to response.getOutputStream().
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM