Hadoop Auth、Java HTTP SPNEGO - 範例

使用瀏覽器存取 Hadoop Auth 保護的 URL

重要:瀏覽器必須支援 HTTP Kerberos SPNEGO。例如,Firefox 或 Internet Explorer。

對於 Firefox,透過載入 about:config 頁面存取低階組態頁面。然後前往 network.negotiate-auth.trusted-uris 偏好設定,並新增受 HTTP Kerberos SPNEGO 保護的網路伺服器的網域名稱或網域(如果使用多個網域和網域名稱,請使用逗號分隔)。

使用 curl 存取 Hadoop Auth 保護的 URL

重要:curl 版本必須支援 GSS,執行 curl -V

$ curl -V
curl 7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
Protocols: tftp ftp telnet dict ldap http file https ftps
Features: GSS-Negotiate IPv6 Largefile NTLM SSL libz

使用 kinit 登入 KDC,然後使用 curl 擷取受保護的 URL

$ kinit
Please enter the password for tucu@LOCALHOST:
$ curl --negotiate -u : -b ~/cookiejar.txt -c ~/cookiejar.txt http://$(hostname -f):8080/hadoop-auth-examples/kerberos/who
Enter host password for user 'tucu':

Hello Hadoop Auth Examples!
  • --negotiate 選項會在 curl 中啟用 SPNEGO。

  • -u : 選項是必要的,但使用者會忽略(使用已 kinit 的主體)。

  • -b-c 用於儲存和傳送 HTTP Cookie。

使用 Java Client

使用 AuthenticatedURL 類別取得經過驗證的 HTTP 連線

...
URL url = new URL("https://127.0.0.1:8080/hadoop-auth/kerberos/who");
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
...
HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token);
...
conn = new AuthenticatedURL().openConnection(url, token);
...

建立並執行範例

下載 Hadoop-Auth 的原始程式碼,範例位於 src/main/examples 目錄中。

伺服器範例

編輯 hadoop-auth-examples/src/main/webapp/WEB-INF/web.xml 並設定 AuthenticationFilter 定義的正確組態初始化參數,該定義已針對 Kerberos 組態(必須指定正確的 Kerberos 主體和 keytab 檔案)。有關詳細資訊,請參閱 組態文件

執行 mvn package 指令建立 Web 應用程式 WAR 檔案。

在 servlet 容器中部署 WAR 檔案。例如,如果使用 Tomcat,請將 WAR 檔案複製到 Tomcat 的 webapps/ 目錄中。

啟動 servlet 容器。

使用 curl 存取伺服器

嘗試使用 curl 存取受保護的資源。受保護的資源為

$ kinit
Please enter the password for tucu@LOCALHOST:

$ curl https://127.0.0.1:8080/hadoop-auth-examples/anonymous/who

$ curl https://127.0.0.1:8080/hadoop-auth-examples/simple/who?user.name=foo

$ curl --negotiate -u : -b ~/cookiejar.txt -c ~/cookiejar.txt http://$(hostname -f):8080/hadoop-auth-examples/kerberos/who

使用 Java client 範例存取伺服器

$ kinit
Please enter the password for tucu@LOCALHOST:

$ cd examples

$ mvn exec:java -Durl=https://127.0.0.1:8080/hadoop-auth-examples/kerberos/who

....

Token value: "u=tucu,p=tucu@LOCALHOST,t=kerberos,e=1295305313146,s=sVZ1mpSnC5TKhZQE3QLN5p2DWBo="
Status code: 200 OK

You are: user[tucu] principal[tucu@LOCALHOST]

....