SEC-1459: Generifying AuthenticationUserDetailsService. Now parameterized with <? extends Authentication>.

This commit is contained in:
Luke Taylor
2010-04-15 01:47:29 +01:00
parent a45d2a4fb2
commit 74896f217b
10 changed files with 38 additions and 35 deletions

View File

@@ -51,7 +51,7 @@ public class CasAuthenticationProvider implements AuthenticationProvider, Initia
//~ Instance fields ================================================================================================ //~ Instance fields ================================================================================================
private AuthenticationUserDetailsService authenticationUserDetailsService; private AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService;
private UserDetailsChecker userDetailsChecker = new AccountStatusUserDetailsChecker(); private UserDetailsChecker userDetailsChecker = new AccountStatusUserDetailsChecker();
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
@@ -150,6 +150,7 @@ public class CasAuthenticationProvider implements AuthenticationProvider, Initia
} }
@Deprecated @Deprecated
@SuppressWarnings("unchecked")
/** /**
* @deprecated as of 3.0. Use the {@link org.springframework.security.cas.authentication.CasAuthenticationProvider#setAuthenticationUserDetailsService(org.springframework.security.core.userdetails.AuthenticationUserDetailsService)} instead. * @deprecated as of 3.0. Use the {@link org.springframework.security.cas.authentication.CasAuthenticationProvider#setAuthenticationUserDetailsService(org.springframework.security.core.userdetails.AuthenticationUserDetailsService)} instead.
*/ */
@@ -157,7 +158,7 @@ public class CasAuthenticationProvider implements AuthenticationProvider, Initia
this.authenticationUserDetailsService = new UserDetailsByNameServiceWrapper(userDetailsService); this.authenticationUserDetailsService = new UserDetailsByNameServiceWrapper(userDetailsService);
} }
public void setAuthenticationUserDetailsService(final AuthenticationUserDetailsService authenticationUserDetailsService) { public void setAuthenticationUserDetailsService(final AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService) {
this.authenticationUserDetailsService = authenticationUserDetailsService; this.authenticationUserDetailsService = authenticationUserDetailsService;
} }

View File

@@ -14,13 +14,10 @@
*/ */
package org.springframework.security.cas.userdetails; package org.springframework.security.cas.userdetails;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService; import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.Authentication;
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
import org.springframework.util.Assert;
import org.jasig.cas.client.validation.Assertion;
/** /**
* Abstract class for using the provided CAS assertion to construct a new User object. This generally is most * Abstract class for using the provided CAS assertion to construct a new User object. This generally is most
@@ -29,11 +26,11 @@ import org.jasig.cas.client.validation.Assertion;
* @author Scott Battaglia * @author Scott Battaglia
* @since 3.0 * @since 3.0
*/ */
public abstract class AbstractCasAssertionUserDetailsService implements AuthenticationUserDetailsService { public abstract class AbstractCasAssertionUserDetailsService
implements AuthenticationUserDetailsService<CasAssertionAuthenticationToken> {
public final UserDetails loadUserDetails(final Authentication token) throws UsernameNotFoundException { public final UserDetails loadUserDetails(final CasAssertionAuthenticationToken token) {
Assert.isInstanceOf(CasAssertionAuthenticationToken.class, token, "The provided token MUST be an instance of CasAssertionAuthenticationToken.class"); return loadUserDetails(token.getAssertion());
return loadUserDetails(((CasAssertionAuthenticationToken) token).getAssertion());
} }
/** /**

View File

@@ -47,6 +47,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
* @author Ben Alex * @author Ben Alex
* @author Scott Battaglia * @author Scott Battaglia
*/ */
@SuppressWarnings("unchecked")
public class CasAuthenticationProviderTests { public class CasAuthenticationProviderTests {
//~ Methods ======================================================================================================== //~ Methods ========================================================================================================

View File

@@ -9,7 +9,7 @@ import org.springframework.security.core.Authentication;
* @author Ruud Senden * @author Ruud Senden
* @since 2.0 * @since 2.0
*/ */
public interface AuthenticationUserDetailsService { public interface AuthenticationUserDetailsService<T extends Authentication> {
/** /**
* *
@@ -19,5 +19,5 @@ public interface AuthenticationUserDetailsService {
* if no user details can be found for the given authentication * if no user details can be found for the given authentication
* token * token
*/ */
UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException; UserDetails loadUserDetails(T token) throws UsernameNotFoundException;
} }

View File

@@ -13,7 +13,7 @@ import org.springframework.util.Assert;
* @author Scott Battaglia * @author Scott Battaglia
* @since 2.0 * @since 2.0
*/ */
public class UserDetailsByNameServiceWrapper implements AuthenticationUserDetailsService, InitializingBean { public class UserDetailsByNameServiceWrapper<T extends Authentication> implements AuthenticationUserDetailsService<T>, InitializingBean {
private UserDetailsService userDetailsService = null; private UserDetailsService userDetailsService = null;
/** /**
@@ -47,7 +47,7 @@ public class UserDetailsByNameServiceWrapper implements AuthenticationUserDetail
* Get the UserDetails object from the wrapped UserDetailsService * Get the UserDetails object from the wrapped UserDetailsService
* implementation * implementation
*/ */
public UserDetails loadUserDetails(Authentication authentication) throws UsernameNotFoundException { public UserDetails loadUserDetails(T authentication) throws UsernameNotFoundException {
return this.userDetailsService.loadUserByUsername(authentication.getName()); return this.userDetailsService.loadUserByUsername(authentication.getName());
} }

View File

@@ -10,6 +10,7 @@ import org.springframework.security.core.authority.AuthorityUtils;
* @author TSARDD * @author TSARDD
* @since 18-okt-2007 * @since 18-okt-2007
*/ */
@SuppressWarnings("unchecked")
public class UserDetailsByNameServiceWrapperTests extends TestCase { public class UserDetailsByNameServiceWrapperTests extends TestCase {
public final void testAfterPropertiesSet() { public final void testAfterPropertiesSet() {

View File

@@ -31,7 +31,7 @@ import org.springframework.util.Assert;
public class PreAuthenticatedAuthenticationProvider implements AuthenticationProvider, InitializingBean, Ordered { public class PreAuthenticatedAuthenticationProvider implements AuthenticationProvider, InitializingBean, Ordered {
private static final Log logger = LogFactory.getLog(PreAuthenticatedAuthenticationProvider.class); private static final Log logger = LogFactory.getLog(PreAuthenticatedAuthenticationProvider.class);
private AuthenticationUserDetailsService preAuthenticatedUserDetailsService = null; private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> preAuthenticatedUserDetailsService = null;
private UserDetailsChecker userDetailsChecker = new AccountStatusUserDetailsChecker(); private UserDetailsChecker userDetailsChecker = new AccountStatusUserDetailsChecker();
private boolean throwExceptionWhenTokenRejected = false; private boolean throwExceptionWhenTokenRejected = false;
@@ -77,7 +77,7 @@ public class PreAuthenticatedAuthenticationProvider implements AuthenticationPro
return null; return null;
} }
UserDetails ud = preAuthenticatedUserDetailsService.loadUserDetails(authentication); UserDetails ud = preAuthenticatedUserDetailsService.loadUserDetails((PreAuthenticatedAuthenticationToken)authentication);
userDetailsChecker.check(ud); userDetailsChecker.check(ud);
@@ -91,25 +91,17 @@ public class PreAuthenticatedAuthenticationProvider implements AuthenticationPro
/** /**
* Indicate that this provider only supports PreAuthenticatedAuthenticationToken (sub)classes. * Indicate that this provider only supports PreAuthenticatedAuthenticationToken (sub)classes.
*/ */
public boolean supports(Class<? extends Object> authentication) { public final boolean supports(Class<? extends Object> authentication) {
return PreAuthenticatedAuthenticationToken.class.isAssignableFrom(authentication); return PreAuthenticatedAuthenticationToken.class.isAssignableFrom(authentication);
} }
/** /**
* Set the AuthenticatedUserDetailsServices to be used. * Set the AuthenticatedUserDetailsService to be used to load the {@code UserDetails} for the authenticated user.
* *
* @param aPreAuthenticatedUserDetailsService * @param uds
*/ */
public void setPreAuthenticatedUserDetailsService(AuthenticationUserDetailsService aPreAuthenticatedUserDetailsService) { public void setPreAuthenticatedUserDetailsService(AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> uds) {
this.preAuthenticatedUserDetailsService = aPreAuthenticatedUserDetailsService; this.preAuthenticatedUserDetailsService = uds;
}
public int getOrder() {
return order;
}
public void setOrder(int i) {
order = i;
} }
/** /**
@@ -130,4 +122,12 @@ public class PreAuthenticatedAuthenticationProvider implements AuthenticationPro
Assert.notNull(userDetailsChecker, "userDetailsChacker cannot be null"); Assert.notNull(userDetailsChecker, "userDetailsChacker cannot be null");
this.userDetailsChecker = userDetailsChecker; this.userDetailsChecker = userDetailsChecker;
} }
public int getOrder() {
return order;
}
public void setOrder(int i) {
order = i;
}
} }

View File

@@ -30,14 +30,15 @@ import org.springframework.util.Assert;
* @author Ruud Senden * @author Ruud Senden
* @since 2.0 * @since 2.0
*/ */
public class PreAuthenticatedGrantedAuthoritiesUserDetailsService implements AuthenticationUserDetailsService { public class PreAuthenticatedGrantedAuthoritiesUserDetailsService
implements AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> {
/** /**
* Get a UserDetails object based on the user name contained in the given * Get a UserDetails object based on the user name contained in the given
* token, and the GrantedAuthorities as returned by the * token, and the GrantedAuthorities as returned by the
* GrantedAuthoritiesContainer implementation as returned by * GrantedAuthoritiesContainer implementation as returned by
* the token.getDetails() method. * the token.getDetails() method.
*/ */
public final UserDetails loadUserDetails(Authentication token) throws AuthenticationException { public final UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws AuthenticationException {
Assert.notNull(token.getDetails()); Assert.notNull(token.getDetails());
Assert.isInstanceOf(GrantedAuthoritiesContainer.class, token.getDetails()); Assert.isInstanceOf(GrantedAuthoritiesContainer.class, token.getDetails());
List<GrantedAuthority> authorities = ((GrantedAuthoritiesContainer) token.getDetails()).getGrantedAuthorities(); List<GrantedAuthority> authorities = ((GrantedAuthoritiesContainer) token.getDetails()).getGrantedAuthorities();

View File

@@ -104,9 +104,10 @@ public class PreAuthenticatedAuthenticationProviderTests {
return result; return result;
} }
private AuthenticationUserDetailsService getPreAuthenticatedUserDetailsService(final UserDetails aUserDetails) { private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken>
return new AuthenticationUserDetailsService() { getPreAuthenticatedUserDetailsService(final UserDetails aUserDetails) {
public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException { return new AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken>() {
public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException {
if (aUserDetails != null && aUserDetails.getUsername().equals(token.getName())) { if (aUserDetails != null && aUserDetails.getUsername().equals(token.getName())) {
return aUserDetails; return aUserDetails;
} }

View File

@@ -31,6 +31,7 @@ public class WebSphere2SpringSecurityPropagationInterceptorTests {
} }
/** SEC-1078 */ /** SEC-1078 */
@SuppressWarnings("unchecked")
@Test @Test
public void createdAuthenticationTokenIsAcceptableToPreauthProvider () throws Throwable { public void createdAuthenticationTokenIsAcceptableToPreauthProvider () throws Throwable {
WASUsernameAndGroupsExtractor helper = mock(WASUsernameAndGroupsExtractor.class); WASUsernameAndGroupsExtractor helper = mock(WASUsernameAndGroupsExtractor.class);