【问题标题】:Redirecting application after POST to other serverPOST 后将应用程序重定向到其他服务器
【发布时间】:2017-02-10 17:28:50
【问题描述】:

我目前正在处理 spring 应用程序中的一些代码,它需要决定是应该将正在登录的用户重定向到新站点(在另一台服务器上)还是继续使用旧站点。

我一直在使用 Apache HttpClient 通过 POST 执行此操作,并且能够从旧登录名登录新站点。

我的问题是我无法在登录后将浏览器重定向到新站点并“保持登录状态”,而是将我重定向到新站点的登录页面,因为我没有登录.

private void redirect2NewSite(HttpServletResponse response, String docNum, String username, String passwd) {

    String url = "http://localhost:9080/website/doLogin";

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);

    List<NameValuePair> urlParameters = new ArrayList<>();
    urlParameters.add(new BasicNameValuePair("documentNumber", docNum));
    urlParameters.add(new BasicNameValuePair("username", username));
    urlParameters.add(new BasicNameValuePair("password", passwd));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));
    HttpResponse postResponse = client.execute(post);

    String responseUrl = postResponse.getFirstHeader("Location").getValue();

    response.setHeader("Location", responseUrl);
    response.sendRedirect(responseUrl);             // This sends me to the new page login
                                        // But should send me to the home page, already logged in
}

旧项目是使用struts重定向到控制器或jsp的。

【问题讨论】:

    标签: java spring post login httpclient


    【解决方案1】:

    登录会话通常基于某种 cookie。 cookie 附加到域。因此,如果您登录到第一个站点 (localhost:9080),您那里就有一个 cookie。如果您访问不同的站点(例如 google.com),您的 cookie 在那里无效,因此 HttpClient 不会发送 cookie。

    如果您需要,您可以手动操作/创建新的 cookie 以使它们对新站点有效。

    【讨论】:

      【解决方案2】:

      我终于让它工作了,这是一个非常古老的项目,它在它的 html 中使用了框架。所以我通过JSP + javascript发帖提交如下:

      注意target="_top" 属性,它允许在框架外加载其他网页。否则就不行了。

      <form action="<%=newLoginUrl%>" method="post" id="redirectForm" target="_top">
          <input type="hidden" name="documentNumber" value="<%=docNum%>" />
          <input type="hidden" name="username" value="<%=userName%>" />
          <input type="hidden" name="password" value="<%=userPassword%>" />
      </form>
      
      
      <script type="text/javascript">
          document.getElementById("redirectForm").submit();
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-22
        • 2018-01-27
        • 2018-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多