我目前正在研究Spring應用程序中的一段代碼,它需要確定登錄的用戶是應該重定向到新站點(在另一個服務器上)還是繼續舊的。在POST後將應用程序重定向到其他服務器
我一直在通過POST使用Apache HttpClient來做到這一點,我可以從舊登錄名登錄新網站。
我的問題是,我不能夠在登錄後瀏覽到新網站重定向和「處於登錄狀態」,而不是它重定向我到新網站的登錄頁面,因爲我沒有登錄in。
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。