簡體   English   中英

維護應用程序中兩個http不同請求之間的會話狀態

[英]Maintaining session state across two http different requests in an application

我有一個場景,我想在應用程序2中的多個會話之間存儲會話信息。我們在tomcat服務器上部署了兩個應用程序。 我們的用例如下:

A. Web應用程序1使用HTTP Rest客戶端向應用程序2發出HTTP發布請求。 POST請求包含一個JSON http請求正文,該正文封裝了要發送到Application#2的數據。代碼塊如下:

RestTemplate template = new RestTemplate();
final SearchCustomer customer = new SearchCustomer();
restTemplate.execute(
    SEND_CUSTOMER_PROFILE, HttpMethod.POST,
    new SearchRequestCallback(searchCustomer), null);

請求回調函數是

static class SearchRequestCallback implements RequestCallback {


  /**
   * Write a JSON response to the request body.
   */
    @Override
    public void doWithRequest(ClientHttpRequest request) throws IOException {
      HttpHeaders httpHeaders = request.getHeaders();
      List<MediaType> acceptableMediaTypes = new LinkedList<>();
      acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
      httpHeaders.setAccept(acceptableMediaTypes);
      httpHeaders.setContentType(MediaType.APPLICATION_JSON);
      request.getBody().write(
          new Gson().toJson(this.searchCustomer).getBytes(StandardCharsets.UTF_8.displayName()));
    }
  }

第二個應用程序具有一個Spring控制器,具有以下設置

@Controller
public class SearchCustomerController {

  /**
   * Builds customer profile knowledge graph.
   * 
   * <p>This is invoked as an synchronous request.
   */
  @RequestMapping(value="/searchProfilePayload.go", method=RequestMethod.POST)
  @ResponseStatus(HttpStatus.OK)
  public void constructSearchCustomerProfileKnowledgeGraph(
      @RequestBody final SearchCustomer customer, HttpServletRequest request) {
    UserContext userContext =
        (UserContext) request.getSession().getAttribute("userContext");
    if (userContext == null) {
      // Perform heavy operation to fetch user session.
      userContext = UserContextHelper.getUserContext(request);
      request.getSession("userContext", userContext)
    }
    userContext.setCustomerProfile(customer);
  }
}

當我通過瀏覽器對應用程序2中的另一個URI進行調用時,我希望這樣做的方式是在進行此調用時保留會話屬性。 有沒有辦法做到這一點?

我知道有關在cookie中存儲JSESSIONID URL重寫的信息,但是我不認為在進行rest調用並使用相同的JESSIONID維護會話屬性時如何設置值。

有一個更好的方法嗎? 這些沒有答案。 我看過這些鏈接,但似乎沒有一個回答我的問題。

HTTP和會話
維持狀態的方式比較

jraahhali上有現貨。

按照JSESSIONID = $ {sessionId}的值設置cookie標頭,或者根據URL重寫鏈接直接在url中使用它。

第一步是從初始響應中檢索JSESSIONID(這取決於您決定如何設置會話ID-URL或Cookies,現在讓我們假設是cookie)

@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
  HttpHeaders httpHeaders = request.getHeaders();
  List<MediaType> acceptableMediaTypes = new LinkedList<>();
  acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
  httpHeaders.setAccept(acceptableMediaTypes);
  httpHeaders.setContentType(MediaType.APPLICATION_JSON);
  request.getBody().write(
      new Gson().toJson(this.searchCustomer).getBytes(StandardCharsets.UTF_8.displayName()));

  ClientHttpResponse response = request.execute();
  String sessionId = response.getHeaders().get(HttpHeaders.SET_COOKIE).split(":")[1].trim(); // I didnt test this, will prolly get a NPE :P
  this.sessionId = sessionId;
}

然后在隨后的請求中(即來自應用程序1或瀏覽器或其他任何請求)

if (this.sessionId != null && !this.sessionId.equals(""))
  httpHeaders.set(HttpHeaders.COOKIE, "JSESSIONID=" + this.sessionId);

// ...
request.execute();

請注意,如果您真的想使用瀏覽器作為其他客戶端,那么我將使用URL重寫方法以簡化使用...

暫無
暫無

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

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