curl -k -u jsmith:passwd
https://myserver.example.com:8443/cli/application/info
?application=JPetStore
對於第一個 REST 指令,不論方法類型為何,都不需要階段作業金鑰,因為用戶端程式利用使用者名稱及密碼進行鑑別。對於同一階段作業中的其他 PUT、POST 及 DELETE 指令,用戶端必須提供階段作業金鑰。伺服器會將階段作業金鑰與使用者名稱及密碼相符,因此用戶端不需要再次指定使用者名稱及密碼。
UCD_SESSION_KEY=6286eb44-b867-5243-875b-e61c5d8b4301;
Expires=Wed, 09-Sep-2082 16:51:45 GMT; Path=/; Secure
用戶端程式必須從此標頭中擷取階段作業金鑰。在上述範例中,階段作業金鑰是 6286eb44-b867-5243-875b-e61c5d8b4301。
然後,對於其他 PUT、POST 及 DELETE 指令,請包括下列標頭:UCD_SESSION_KEY:sessionKey
將階段作業金鑰用於 sessionKey。UCR_SESSION_KEY:sessionKey
#!/usr/bin/env python
import urllib2
import json
import base64
import sys
if not len(sys.argv) == 3:
print 'usage: script <username> <password>'
exit(1)
username = sys.argv[1]
password = sys.argv[2]
epass = base64.b64encode(username + ':' + password)
print 'base64 encoded: ' + epass
baseUrl = 'ucdeploy.example.org:8443'
url = 'https://' + baseUrl + '/cli/application/info' + '?application=JPetStore'
opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(url)
req.add_header('Authorization', 'Basic '+epass)
req.get_method = lambda: 'GET'
resp = opener.open(req)
print resp.read()
如需在 Groovy Script 中進行鑑別的範例,請參閱下列頁面:http://devblog.laraziosi.org/extensibility/index.php/devops-articles/6-getting-started-with-the-ibm-urbancode-deploy-rest-api-and-groovy
下列 Java™ 程式碼是利用使用者名稱及密碼進行鑑別的簡式範例。該程式碼接受所有憑證,但您可以修改該程式碼,以控制接受哪些憑證。
此範例需要 HttpComponents-Util.jar 及 uDeployRestClient.jar JAR 檔。檔案 HttpComponents-Util.jar 在伺服器安裝目錄的 lib 資料夾中提供。預設伺服器安裝目錄是 /opt/ibm-ucd/server(在 Linux 上)和 C:\Program Files\ibm-ucd\server(在 Windows 上)。檔案 uDeployRestClient.jar 在許多核心外掛程式(例如,UrbanCode Deploy Applications 外掛程式)中提供。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import com.urbancode.commons.httpcomponentsutil.HttpClientBuilder;
public class RESTExample {
public static void main(String[] args) {
// suppress log4j messages from UCD library
Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF);
HttpClientBuilder clientBuilder = new HttpClientBuilder();
clientBuilder.setUsername("admin");
clientBuilder.setPassword("admin");
// for SSL enabled servers, accept all certificates
clientBuilder.setTrustAllCerts(true);
DefaultHttpClient client = clientBuilder.buildClient();
try {
HttpGet request = new HttpGet(new URI(
"https://ucdeploy.example.org:8443/cli/application/info?application=JPetStore"));
try {
HttpResponse resp = client.execute(request);
BufferedReader br = new BufferedReader (
new InputStreamReader(resp.getEntity().getContent()));
String currentLine = new String();
while ((currentLine = br.readLine()) != null){
System.out.print(currentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
sslProtocol="TLS"
keystoreFile="conf/tomcat.keystore"
keystorePass="changeit" />
keytool -v -list -keystore keyStoreFileName
keytool 應用程式包括在 Java 開發者套件中,不是 IBM UrbanCode Deploy 的一部分。
將 server.xml 檔中 keystoreFile 屬性的名稱用於 keyStoreFileName。
當指令提示您輸入密碼時,請指定 keystorePass 屬性的值。預設值為 changeit。Keystore type: JKS
Keystore provider: SUN
Your keystore contains 1 entry
Alias name: server
Creation date: Mar 19, 2014
Entry type: PrivateKeyEntry
在此程式碼中,別名是 server。keytool -exportcert
-alias serverAlias
-keystore keyStoreFileName
-storetype jks
-file server.cert
將伺服器的別名用於 serverAlias。jreLocation\jre\bin\keytool.exe -importcert
-alias serverAlias
-file tomcat.cert
-storetype jks
-keystore jreLocation\jre\lib\security\cacerts
將 JRE 或 JDK 的位置用於 jreLocation。