从 servlet 打开受密码保护的网站

发布于 2024-07-25 00:06:05 字数 856 浏览 7 评论 0原文

该场景的预期用户将访问 servlet(例如 http://someip/myservlet)进而验证受密码保护的网站(例如mysite.com - 托管在 IIS 服务器上,启用 Windows 身份验证隐式然后打开mysite.com – 这样:

  1. 目标用户将不会收到输入用户名和密码的提示。 密码,因为他将通过 servlet(托管在 Tomcat 等服务器上)
  2. 任何其他访问 链接文本< /a> 在不知道凭据的情况下将无法访问

要求是 (它必须打开该网站而不是获取内容,如 mysite.com< /a> 具有动态功能)

Java 中是否可能(HttpURLConnection)?

任何帮助表示赞赏。

谢谢。

The scenario is intended user will access the servlet (e.g http://someip/myservlet) which in turn authenticates a password protected website (e.g. mysite.com - which is hosted on IIS server with Use Windows authentication enabled) implicitly and then opens that mysite.com – so that:

  1. target users will not get prompt for username & password as he will go through the servlet(hosted on some server like tomcat)
  2. any other user accessing link text will not be able to access without knowing credentials

The requirement is
(it has to open that website not fetch the content as mysite.com has dynamic functionality in it)

Is it possible in Java (HttpURLConnection) ??

any help is appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑色毁心梦 2024-08-01 00:06:05

为了确保我理解您的需求,这里有一个摘要:
您希望给定用户 A 连接到您的第一台服务器 Server1.domain1.com,该服务器将(从 java 服务器内部)连接到第二台服务器 server2.domain2.com(当前位于 IIS 下)。 然后,服务器 1 会将用户转发到服务器 2 的网页,挑战是避免任何身份验证弹出窗口。

根本问题是将server1 从server2 获得的身份验证票证从server1 传输到客户端浏览器,然后从客户端浏览器传输到server2。

这不是一个具体的java问题,而是一个全球性的WEB问题。 事实上,server2 接收到的用于识别客户端用户的唯一信息是在 http 流中,简而言之就是 IP 地址、URL 和 cookie。

如果 server1 和 server2 不是同一个域,则 Cookie 是一个死胡同(请参阅 RFC 2109 : http:// /www.ietf.org/rfc/rfc2109.txt),因为仅当 cookie 从同一域的服务器(相同或另一个)返回时,浏览器才会将 cookie 发送到给定域的服务器。

所以答案是一个两步过程:

  • 首先,server1 应该从 server2 获取 sessionID,可能是通过使用适当的凭据提交 http 请求(基本身份验证?表单字段?或者最糟糕的是,“类似 Windows 的域身份验证”?)。 对于这一步,我建议使用 apache httpclient 库。
  • 其次,server1 应该将客户端用户转发到 server2 的适当 URL,其中包含 sessionID 作为参数。 它要求 server2 提供这样的可能性才能进入。

乍一看,我没有看到其他简单的解决方案。

To be sure I understood your need, here is a summary :
you want a given user A to connect on your first server Server1.domain1.com, that would connect (from inside the java server) on a second server server2.domain2.com (currently under IIS). Then server1 would forward user to server2 web page, the challenge being avoiding any authentication popup.

The root problem is to transmit, from the server1 to the client browser, then from client browser to server2, the authentication ticket that server1 got from server2.

It is not specifically a java problem but more a global WEB problem. Indeed the only information received by server2 to identify client user is in the http flow, in short words the IP adress, the URL, and cookies.

Cookies is a dead end if server1 and server2 are not the same domain (see RFC 2109 : http://www.ietf.org/rfc/rfc2109.txt), as browser would send cookies to a server of a given domain only if the cookie was returned from a server (the same or another) of the SAME domain.

So the answer is a two step process :

  • first, server1 should get a sessionID from server2, probably through submitting a http request with appropriate credentials (basic-auth ? form fields ? or worst, "Windows like domain auth" ?). For this step, I suggest using apache httpclient library.
  • Second, server1 should forward client user to an appropriate URL of server2, that would include the sessionID as an argument. It requires that server2 offers such a possibility to get in.

At first sight I see no other simple solution.

伪装你 2024-08-01 00:06:05

HTTP 反向代理 似乎涵盖了您想要实现的功能。
这种代理将在另一个位置(例如 site2.domain2.com)“镜像”某些站点 site1.domain1.com

最常见的用例是 site1 不是公共的,反向代理将充当网关、负载均衡器、SSL 网关或类似的。

对于 Java 中的实现:我还推荐 Apache HTTP 客户端 库。 并且可以通过此客户端库使用 NTLM 身份验证。

It seems the functionality you like to implement is covered by HTTP reverse proxies.
This kind of proxy will "mirror" some site site1.domain1.com at another location like site2.domain2.com

Most common use-cases are that site1 is not pubic and the reverse proxy will act as a gateway, load balancer, SSL gateway or similar.

For implementation in Java: I also recommend the Apache HTTP client library. And it's possible to use NTLM authentication with this client lib.

碍人泪离人颜 2024-08-01 00:06:05

如果您确实想要实现这种请求转发,那么内置的 HTTPURLConnection 可能还不够。 尝试一下 Apache HTTPClient 它有足够的自定义选项。 您还可以查找 TCP 转发解决方案或这篇文章

The built in HTTPURLConnection might be not enough if you really want to implement this kind of request forwarding. Try the Apache HTTPClient which has ample of customization options. You could also look for TCP forward solutions or this post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文