diff --git a/spring-security-kerberos-client/src/main/java/org/springframework/security/kerberos/client/KerberosRestTemplate.java b/spring-security-kerberos-client/src/main/java/org/springframework/security/kerberos/client/KerberosRestTemplate.java
index c10e80b..0d332ac 100644
--- a/spring-security-kerberos-client/src/main/java/org/springframework/security/kerberos/client/KerberosRestTemplate.java
+++ b/spring-security-kerberos-client/src/main/java/org/springframework/security/kerberos/client/KerberosRestTemplate.java
@@ -15,6 +15,7 @@
*/
package org.springframework.security.kerberos.client;
+import java.io.IOException;
import java.net.URI;
import java.security.Principal;
import java.security.PrivilegedAction;
@@ -24,10 +25,16 @@ import java.util.Map;
import java.util.Set;
import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope;
@@ -57,6 +64,7 @@ import org.springframework.web.client.RestTemplate;
*
* - Leave keyTabLocation and userPrincipal empty if you want to use cached ticket
* - Use keyTabLocation and userPrincipal if you want to use keytab file
+ * - Use userPrincipal and password if you want to use user/password
* - Use loginOptions if you want to customise Krb5LoginModule options
* - Use a customised httpClient
*
@@ -70,13 +78,14 @@ public class KerberosRestTemplate extends RestTemplate {
private final String keyTabLocation;
private final String userPrincipal;
+ private final String password;
private final Map loginOptions;
/**
* Instantiates a new kerberos rest template.
*/
public KerberosRestTemplate() {
- this(null, null, null, buildHttpClient());
+ this(null, null, null, null, buildHttpClient());
}
/**
@@ -85,7 +94,7 @@ public class KerberosRestTemplate extends RestTemplate {
* @param httpClient the http client
*/
public KerberosRestTemplate(HttpClient httpClient) {
- this(null, null, null, httpClient);
+ this(null, null, null, null, httpClient);
}
/**
@@ -106,7 +115,7 @@ public class KerberosRestTemplate extends RestTemplate {
* @param httpClient the http client
*/
public KerberosRestTemplate(String keyTabLocation, String userPrincipal, HttpClient httpClient) {
- this(keyTabLocation, userPrincipal, null, httpClient);
+ this(keyTabLocation, userPrincipal, null, null, httpClient);
}
/**
@@ -115,7 +124,7 @@ public class KerberosRestTemplate extends RestTemplate {
* @param loginOptions the login options
*/
public KerberosRestTemplate(Map loginOptions) {
- this(null, null, loginOptions, buildHttpClient());
+ this(null, null, null, loginOptions, buildHttpClient());
}
/**
@@ -125,7 +134,7 @@ public class KerberosRestTemplate extends RestTemplate {
* @param httpClient the http client
*/
public KerberosRestTemplate(Map loginOptions, HttpClient httpClient) {
- this(null, null, loginOptions, httpClient);
+ this(null, null, null, loginOptions, httpClient);
}
/**
@@ -136,7 +145,19 @@ public class KerberosRestTemplate extends RestTemplate {
* @param loginOptions the login options
*/
public KerberosRestTemplate(String keyTabLocation, String userPrincipal, Map loginOptions) {
- this(keyTabLocation, userPrincipal, loginOptions, buildHttpClient());
+ this(keyTabLocation, userPrincipal, null, loginOptions, buildHttpClient());
+ }
+
+ /**
+ * Instantiates a new kerberos rest template.
+ *
+ * @param keyTabLocation the key tab location
+ * @param userPrincipal the user principal
+ * @param password the password
+ * @param loginOptions the login options
+ */
+ public KerberosRestTemplate(String keyTabLocation, String userPrincipal, String password, Map loginOptions) {
+ this(keyTabLocation, userPrincipal, password, loginOptions, buildHttpClient());
}
/**
@@ -148,9 +169,23 @@ public class KerberosRestTemplate extends RestTemplate {
* @param httpClient the http client
*/
private KerberosRestTemplate(String keyTabLocation, String userPrincipal, Map loginOptions, HttpClient httpClient) {
+ this(keyTabLocation, userPrincipal, null, loginOptions, httpClient);
+ }
+
+ /**
+ * Instantiates a new kerberos rest template.
+ *
+ * @param keyTabLocation the key tab location
+ * @param userPrincipal the user principal
+ * @param password the password
+ * @param loginOptions the login options
+ * @param httpClient the http client
+ */
+ private KerberosRestTemplate(String keyTabLocation, String userPrincipal, String password, Map loginOptions, HttpClient httpClient) {
super(new HttpComponentsClientHttpRequestFactory(httpClient));
this.keyTabLocation = keyTabLocation;
this.userPrincipal = userPrincipal;
+ this.password = password;
this.loginOptions = loginOptions;
}
@@ -172,16 +207,27 @@ public class KerberosRestTemplate extends RestTemplate {
return httpClient;
}
+ /**
+ * Setup the {@link LoginContext} with credentials and options for authentication against kerberos.
+ *
+ * @return the login context
+ */
+ private LoginContext buildLoginContext() throws LoginException {
+ ClientLoginConfig loginConfig = new ClientLoginConfig(keyTabLocation, userPrincipal, password, loginOptions);
+ Set princ = new HashSet(1);
+ princ.add(new KerberosPrincipal(userPrincipal));
+ Subject sub = new Subject(false, princ, new HashSet