将 RestTemplate 连接到 Atlassian 产品 (confluence)
connect with RestTemplate to Atlassian products (confluence)
我将使用 Spring RestTemplate 连接到 Atlassian application confluence,这有效:
RestTemplate template = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Cookie",
"_ga=GA1.2.62774864.1458801518; __utma=1.62774864.1458801518.1464093373.1464682866.13; __utmz=1.1458816981.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.3.62774864.1458801518; ki_t=1461064684094%3B1461064684094%3B1461069734488%3B1%3B2; ki_r=; seraph.confluence=5865502%3Adaa119bfd220fe01ddb5ebfb63fadf2c1f1e24ed; mywork.tab.tasks=false; JSESSIONID=AD61DF5C74564DBB35DB251EC3C8D6AD.it-confl02; crowd.token_key=0BJ0f0iIyp0TfFJnPo1PyA00");
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
HttpEntity<String> response = template.exchange("https://confluence.domain.net/rest/api/content/search?cql=space=KIIOCO",
HttpMethod.GET, requestEntity, String.class);
System.out.println(response.getBody());
使用 cookie,但我会避免使用这个长 cookie 字符串。
是否有可能替换此 cookie?
您不能将基本身份验证作为 header 而不是 cookie 发送吗?
我删除了您的 cookie 代码,而是每次都发送用户名和密码,并且有效:
package hello;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.Base64;
public class Application {
public static void main(String args[]) throws IOException {
RestTemplate template = new RestTemplate();
HttpHeaders httpHeaders = getHeaders();
HttpEntity<?> requestEntity = new HttpEntity(httpHeaders);
HttpEntity<String> response = template.exchange("https://confluence.domain.net/rest/api/content/search?cql=space=KIIOCO",
HttpMethod.GET, requestEntity, String.class);
System.out.println(response.getBody());
}
public static HttpHeaders getHeaders() {
String plainCreds = "my-username:my-password";
byte[] base64CredsBytes = Base64.getEncoder().encode(plainCreds.getBytes());
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
return headers;
}
}
我将使用 Spring RestTemplate 连接到 Atlassian application confluence,这有效:
RestTemplate template = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Cookie",
"_ga=GA1.2.62774864.1458801518; __utma=1.62774864.1458801518.1464093373.1464682866.13; __utmz=1.1458816981.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.3.62774864.1458801518; ki_t=1461064684094%3B1461064684094%3B1461069734488%3B1%3B2; ki_r=; seraph.confluence=5865502%3Adaa119bfd220fe01ddb5ebfb63fadf2c1f1e24ed; mywork.tab.tasks=false; JSESSIONID=AD61DF5C74564DBB35DB251EC3C8D6AD.it-confl02; crowd.token_key=0BJ0f0iIyp0TfFJnPo1PyA00");
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
HttpEntity<String> response = template.exchange("https://confluence.domain.net/rest/api/content/search?cql=space=KIIOCO",
HttpMethod.GET, requestEntity, String.class);
System.out.println(response.getBody());
使用 cookie,但我会避免使用这个长 cookie 字符串。 是否有可能替换此 cookie?
您不能将基本身份验证作为 header 而不是 cookie 发送吗?
我删除了您的 cookie 代码,而是每次都发送用户名和密码,并且有效:
package hello;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.Base64;
public class Application {
public static void main(String args[]) throws IOException {
RestTemplate template = new RestTemplate();
HttpHeaders httpHeaders = getHeaders();
HttpEntity<?> requestEntity = new HttpEntity(httpHeaders);
HttpEntity<String> response = template.exchange("https://confluence.domain.net/rest/api/content/search?cql=space=KIIOCO",
HttpMethod.GET, requestEntity, String.class);
System.out.println(response.getBody());
}
public static HttpHeaders getHeaders() {
String plainCreds = "my-username:my-password";
byte[] base64CredsBytes = Base64.getEncoder().encode(plainCreds.getBytes());
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
return headers;
}
}