diff --git a/build.gradle b/build.gradle
index fe9f499..10121af 100644
--- a/build.gradle
+++ b/build.gradle
@@ -78,7 +78,7 @@ configure(subprojects) { subproject ->
dependencies {
testCompile "org.mockito:mockito-core:$mockitoVersion"
testCompile "junit:junit:$junitVersion"
-// testRuntime("log4j:log4j:$log4jVersion")
+ testRuntime("log4j:log4j:$log4jVersion")
}
}
diff --git a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/GlobalSunJaasKerberosConfig.java b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/GlobalSunJaasKerberosConfig.java
index 1612e2f..78ce52f 100644
--- a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/GlobalSunJaasKerberosConfig.java
+++ b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/GlobalSunJaasKerberosConfig.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2010 the original author or authors.
+ * Copyright 2010-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,13 +20,15 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
+ * Config for global jaas.
+ *
* @author Mike Wiesner
* @since 1.0
- * @version $Id:$
*/
public class GlobalSunJaasKerberosConfig implements BeanPostProcessor, InitializingBean {
private boolean debug = false;
+
private String krbConfLocation;
public void afterPropertiesSet() throws Exception {
@@ -38,32 +40,27 @@ public class GlobalSunJaasKerberosConfig implements BeanPostProcessor, Initializ
}
}
-
-
- /**
+
+ /**
* Enable debug logs from the Sun Kerberos Implementation. Default is false.
*/
public void setDebug(boolean debug) {
this.debug = debug;
}
-
- /**
+ /**
* Kerberos config file location can be specified here.
- *
+ *
* @param krbConfLocation
*/
public void setKrbConfLocation(String krbConfLocation) {
this.krbConfLocation = krbConfLocation;
}
-
- /*
- * The following methods are not used here. This Bean implements only BeanPostProcessor to ensure that it
- * is created before any other bean is created, because the system properties needed to be set very early
- * in the startup-phase, but after the BeanFactoryPostProcessing.
- */
-
+ // The following methods are not used here. This Bean implements only BeanPostProcessor to ensure that it
+ // is created before any other bean is created, because the system properties needed to be set very early
+ // in the startup-phase, but after the BeanFactoryPostProcessing.
+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
diff --git a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosAuthenticationProvider.java b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosAuthenticationProvider.java
index 0b553f7..d395ecf 100644
--- a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosAuthenticationProvider.java
+++ b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosAuthenticationProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2009 the original author or authors.
+ * Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,11 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.springframework.security.extensions.kerberos;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@@ -26,38 +23,48 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
/**
+ * {@link AuthenticationProvider} for kerberos.
+ *
* @author Mike Wiesner
* @since 1.0
- * @version $Id$
*/
public class KerberosAuthenticationProvider implements AuthenticationProvider {
-
- private static final Log LOG = LogFactory.getLog(KerberosAuthenticationProvider.class);
-
+
private KerberosClient kerberosClient;
+
private UserDetailsService userDetailsService;
-
-
+ @Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
- UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
- String validatedUsername = kerberosClient.login(auth.getName(), auth.getCredentials().toString());
- UserDetails userDetails = this.userDetailsService.loadUserByUsername(validatedUsername);
- UsernamePasswordAuthenticationToken output = new UsernamePasswordAuthenticationToken(userDetails, auth.getCredentials(), userDetails.getAuthorities());
- output.setDetails(authentication.getDetails());
+ UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
+ String validatedUsername = kerberosClient.login(auth.getName(), auth.getCredentials().toString());
+ UserDetails userDetails = this.userDetailsService.loadUserByUsername(validatedUsername);
+ UsernamePasswordAuthenticationToken output = new UsernamePasswordAuthenticationToken(userDetails,
+ auth.getCredentials(), userDetails.getAuthorities());
+ output.setDetails(authentication.getDetails());
return output;
-
+
}
+ @Override
public boolean supports(Class extends Object> authentication) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
-
+
+ /**
+ * Sets the kerberos client.
+ *
+ * @param kerberosClient the new kerberos client
+ */
public void setKerberosClient(KerberosClient kerberosClient) {
this.kerberosClient = kerberosClient;
}
-
-
+
+ /**
+ * Sets the user details service.
+ *
+ * @param detailsService the new user details service
+ */
public void setUserDetailsService(UserDetailsService detailsService) {
this.userDetailsService = detailsService;
}
diff --git a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosServiceAuthenticationProvider.java b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosServiceAuthenticationProvider.java
index 575919e..ada78e6 100644
--- a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosServiceAuthenticationProvider.java
+++ b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosServiceAuthenticationProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2009 the original author or authors.
+ * Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.springframework.security.extensions.kerberos;
import org.apache.commons.logging.Log;
@@ -29,7 +28,6 @@ import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.extensions.kerberos.web.SpnegoAuthenticationProcessingFilter;
import org.springframework.util.Assert;
-
/**
*
Authentication Provider which validates Kerberos Service Tickets
* or SPNEGO Tokens (which includes Kerberos Service Tickets).
@@ -45,7 +43,6 @@ import org.springframework.util.Assert;
*
* @author Mike Wiesner
* @since 1.0
- * @version $Id$
* @see KerberosTicketValidator
* @see UserDetailsService
* @see SpnegoAuthenticationProcessingFilter
@@ -74,9 +71,7 @@ public class KerberosServiceAuthenticationProvider implements
this.ticketValidator = ticketValidator;
}
- /* (non-Javadoc)
- * @see org.springframework.security.authentication.AuthenticationProvider#authenticate(org.springframework.security.core.Authentication)
- */
+ @Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
KerberosServiceRequestToken auth = (KerberosServiceRequestToken) authentication;
@@ -90,9 +85,19 @@ public class KerberosServiceAuthenticationProvider implements
KerberosServiceRequestToken responseAuth = new KerberosServiceRequestToken(userDetails, userDetails.getAuthorities(), token);
responseAuth.setDetails(authentication.getDetails());
return responseAuth;
-
+
}
+ @Override
+ public boolean supports(Class extends Object> auth) {
+ return KerberosServiceRequestToken.class.isAssignableFrom(auth);
+ }
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ Assert.notNull(this.ticketValidator, "ticketValidator must be specified");
+ Assert.notNull(this.userDetailsService, "userDetailsService must be specified");
+ }
/**
* Allows subclasses to perform any additional checks of a returned UserDetails
@@ -108,19 +113,4 @@ public class KerberosServiceAuthenticationProvider implements
}
- /* (non-Javadoc)
- * @see org.springframework.security.authentication.AuthenticationProvider#supports(java.lang.Class)
- */
- public boolean supports(Class extends Object> auth) {
- return KerberosServiceRequestToken.class.isAssignableFrom(auth);
- }
-
- /* (non-Javadoc)
- * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
- */
- public void afterPropertiesSet() throws Exception {
- Assert.notNull(this.ticketValidator, "ticketValidator must be specified");
- Assert.notNull(this.userDetailsService, "userDetailsService must be specified");
- }
-
}
diff --git a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosServiceRequestToken.java b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosServiceRequestToken.java
index 907ea0e..a5479d2 100644
--- a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosServiceRequestToken.java
+++ b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/KerberosServiceRequestToken.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2009 the original author or authors.
+ * Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.springframework.security.extensions.kerberos;
import java.util.Arrays;
@@ -34,7 +33,6 @@ import org.springframework.security.extensions.kerberos.web.SpnegoAuthentication
*
* @author Mike Wiesner
* @since 1.0
- * @version $Id$
* @see KerberosServiceAuthenticationProvider
* @see SpnegoAuthenticationProcessingFilter
*/
@@ -99,21 +97,18 @@ public class KerberosServiceRequestToken extends AbstractAuthenticationToken {
return true;
}
- /* (non-Javadoc)
- * @see org.springframework.security.core.Authentication#getCredentials()
- */
+ @Override
public Object getCredentials() {
return null;
}
- /* (non-Javadoc)
- * @see org.springframework.security.core.Authentication#getPrincipal()
- */
+ @Override
public Object getPrincipal() {
return this.principal;
}
- /** Returns the Kerberos token
+ /**
+ * Returns the Kerberos token
*/
public byte[] getToken() {
return this.token;
diff --git a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/SunJaasKerberosClient.java b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/SunJaasKerberosClient.java
index af99648..667caba 100644
--- a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/SunJaasKerberosClient.java
+++ b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/SunJaasKerberosClient.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2009 the original author or authors.
+ * Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.springframework.security.extensions.kerberos;
import java.io.IOException;
@@ -41,19 +40,18 @@ import org.springframework.security.authentication.BadCredentialsException;
*
* @author Mike Wiesner
* @since 1.0
- * @version $Id$
*/
public class SunJaasKerberosClient implements KerberosClient {
private boolean debug = false;
-
private static final Log LOG = LogFactory.getLog(SunJaasKerberosClient.class);
+ @Override
public String login(String username, String password) {
LOG.debug("Trying to authenticate " + username + " with Kerberos");
String validatedUsername;
-
+
try {
LoginContext loginContext = new LoginContext("", null, new KerberosClientCallbackHandler(username, password),
new LoginConfig(this.debug));
diff --git a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/SunJaasKerberosTicketValidator.java b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/SunJaasKerberosTicketValidator.java
index a955a8d..a1d9e45 100644
--- a/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/SunJaasKerberosTicketValidator.java
+++ b/spring-security-kerberos-core/src/main/java/org/springframework/security/extensions/kerberos/SunJaasKerberosTicketValidator.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2009 the original author or authors.
+ * Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.springframework.security.extensions.kerberos;
import java.security.Principal;
@@ -48,7 +47,6 @@ import org.springframework.util.Assert;
*
* @author Mike Wiesner
* @since 1.0
- * @version $Id$
*/
public class SunJaasKerberosTicketValidator implements KerberosTicketValidator, InitializingBean {
@@ -58,9 +56,7 @@ public class SunJaasKerberosTicketValidator implements KerberosTicketValidator,
private boolean debug = false;
private static final Log LOG = LogFactory.getLog(SunJaasKerberosTicketValidator.class);
- /* (non-Javadoc)
- * @see org.springframework.security.extensions.kerberos.KerberosTicketValidator#validateTicket(byte[])
- */
+ @Override
public String validateTicket(byte[] token) {
String username = null;
try {
@@ -71,7 +67,32 @@ public class SunJaasKerberosTicketValidator implements KerberosTicketValidator,
return username;
}
- /** The service principal of the application.
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ Assert.notNull(this.servicePrincipal, "servicePrincipal must be specified");
+ Assert.notNull(this.keyTabLocation, "keyTab must be specified");
+ if (keyTabLocation instanceof ClassPathResource) {
+ LOG.warn("Your keytab is in the classpath. This file needs special protection and shouldn't be in the classpath. JAAS may also not be able to load this file from classpath.");
+ }
+ String keyTabLocationAsString = this.keyTabLocation.getURL().toExternalForm();
+ // We need to remove the file prefix (if there is one), as it is not supported in Java 7 anymore.
+ // As Java 6 accepts it with and without the prefix, we don't need to check for Java 7
+ if (keyTabLocationAsString.startsWith("file:"))
+ {
+ keyTabLocationAsString = keyTabLocationAsString.substring(5);
+ }
+ LoginConfig loginConfig = new LoginConfig(keyTabLocationAsString, this.servicePrincipal,
+ this.debug);
+ Set princ = new HashSet(1);
+ princ.add(new KerberosPrincipal(this.servicePrincipal));
+ Subject sub = new Subject(false, princ, new HashSet