简体   繁体   English

Servlet请求到另一个具有授权的Servlet

[英]Servlet request to another servlet with authorization

I have tomcat server with some applications. 我有一些应用程序的tomcat服务器。 This server uses standart j_secure_check auth. 该服务器使用标准的j_secure_check身份验证。 On another tomcat server I need to deploy application that must be some proxy to first server. 在另一台tomcat服务器上,我需要部署必须是第一台服务器的代理的应用程序。 So I need to invoke servlet from another servlet but first I need to do authentication with j_secure_check. 因此,我需要从另一个servlet调用servlet,但是首先需要使用j_secure_check进行身份验证。 Is it possible to do it programmely? 可以有计划地这样做吗?

You need to grab the session cookie and pass it through on subsequent requests. 您需要获取会话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