Spring Security Authentication using RestTemplate

后端 未结 7 1253
你的背包
你的背包 2020-12-02 17:58

I have 2 spring web apps that provide 2 separate set of services. Web App 1 has Spring Security implemented using a user-based authentication.

Now, Web App 2 needs

7条回答
  •  孤街浪徒
    2020-12-02 18:23

    The following will authenticate and return the session cookie:

    String sessionCookie= restTemplate.execute(uri, HttpMethod.POST, request -> {
            request.getBody().write(("j_username=USER_NAME&j_password=PASSWORD").getBytes());
        }, response -> {
            AbstractClientHttpResponse r = (AbstractClientHttpResponse) response;
            HttpHeaders headers = r.getHeaders();
            return headers.get("Set-Cookie").get(0);
        });
    

提交回复
热议问题