|
| 1 | +package com.arangodb;/* |
| 2 | + * DISCLAIMER |
| 3 | + * |
| 4 | + * Copyright 2016 ArangoDB GmbH, Cologne, Germany |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | + */ |
| 20 | + |
| 21 | + |
| 22 | +import org.apache.http.HttpEntity; |
| 23 | +import org.apache.http.HttpResponse; |
| 24 | +import org.apache.http.auth.AuthScope; |
| 25 | +import org.apache.http.auth.Credentials; |
| 26 | +import org.apache.http.client.methods.HttpGet; |
| 27 | +import org.apache.http.client.methods.HttpUriRequest; |
| 28 | +import org.apache.http.client.params.AuthPolicy; |
| 29 | +import org.apache.http.impl.auth.SPNegoSchemeFactory; |
| 30 | +import org.apache.http.impl.client.DefaultHttpClient; |
| 31 | +import org.apache.http.util.EntityUtils; |
| 32 | + |
| 33 | +import javax.security.auth.login.LoginException; |
| 34 | +import java.io.IOException; |
| 35 | +import java.security.Principal; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Michele Rastelli |
| 39 | + */ |
| 40 | +public class KerberosTest { |
| 41 | + static private String URL = "http://bruecklinux.arangodb.biz:8899/_db/_system/_api/database/"; |
| 42 | + |
| 43 | + public static void main(String[] args) throws IOException, LoginException { |
| 44 | + kerberos(); |
| 45 | + } |
| 46 | + |
| 47 | + private static void kerberos() throws IOException, LoginException { |
| 48 | + |
| 49 | + System.setProperty("java.security.auth.login.config", "/home/michele/arango/arangodb-java-driver/src/test/resources/login.conf"); |
| 50 | + System.setProperty("java.security.krb5.conf", "/etc/krb5.conf"); |
| 51 | + System.setProperty("sun.security.krb5.debug", "true"); |
| 52 | + System.setProperty("sun.security.jgss.debug", "true"); |
| 53 | + System.setProperty("javax.security.auth.useSubjectCredsOnly", "false"); |
| 54 | + System.setProperty("java.security.krb5.realm", "BRUECKLINUX.ARANGODB.BIZ"); |
| 55 | + System.setProperty("java.security.krb5.kdc", "kerberos.BRUECKLINUX.ARANGODB.BIZ"); |
| 56 | + |
| 57 | +// OK |
| 58 | +// LoginContext lc = new LoginContext("SampleClient", new TextCallbackHandler()); |
| 59 | +// lc.login(); |
| 60 | +// System.out.println("done"); |
| 61 | +// lc.getSubject(); |
| 62 | + |
| 63 | + final boolean stripPort = true; |
| 64 | + final boolean useCanonicalHostname = false; |
| 65 | + |
| 66 | + DefaultHttpClient httpclient = new DefaultHttpClient(); |
| 67 | + try { |
| 68 | + httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(stripPort, useCanonicalHostname)); |
| 69 | + Credentials use_jaas_creds = new Credentials() { |
| 70 | + public String getPassword() { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + public Principal getUserPrincipal() { |
| 75 | + return null; |
| 76 | + } |
| 77 | + }; |
| 78 | + httpclient.getCredentialsProvider().setCredentials( |
| 79 | + new AuthScope(null, -1, null), |
| 80 | + use_jaas_creds); |
| 81 | + |
| 82 | + HttpUriRequest request = new HttpGet("http://bruecklinux.arangodb.biz:8899/_db/_system/_api/database/"); |
| 83 | + HttpResponse response = httpclient.execute(request); |
| 84 | + HttpEntity entity = response.getEntity(); |
| 85 | + System.out.println("----------------------------------------"); |
| 86 | + System.out.println(response.getStatusLine()); |
| 87 | + System.out.println("----------------------------------------"); |
| 88 | + if (entity != null) { |
| 89 | + System.out.println(EntityUtils.toString(entity)); |
| 90 | + } |
| 91 | + System.out.println("----------------------------------------"); |
| 92 | +// This ensures the connection gets released back to the manager |
| 93 | + EntityUtils.consume(entity); |
| 94 | + |
| 95 | + } finally { |
| 96 | + // When HttpClient instance is no longer needed, |
| 97 | + // shut down the connection manager to ensure |
| 98 | + // immediate deallocation of all system resources |
| 99 | + httpclient.getConnectionManager().shutdown(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments