1
0
mirror of synced 2026-08-05 17:57:15 +00:00

Refactored up an AuthoritiesPopulator and DaoAuthoritiesPopulator from functionality in the cas provider. This interface and impl are well suited for use in the openid provider, and possibly in the sitemesh provider.

This commit is contained in:
Ray Krueger
2008-01-16 03:01:51 +00:00
parent e90498c4f7
commit 66f73897e6
10 changed files with 137 additions and 426 deletions
@@ -0,0 +1,52 @@
package org.springframework.security.providers;
import org.springframework.security.AuthenticationException;
import org.springframework.security.userdetails.UserDetails;
/**
* Populates the <code>UserDetails</code> associated with a CAS authenticated
* user.
*
* <p>
* Intended to grant authorities (roles) for providers that do not support
* authorities/roles directly. It merely authenticates their identity.
* As Spring Security needs to know the authorities granted to a user in
* order to construct a valid <code>Authentication</code> object, implementations
* of this interface will provide this information.
* </p>
*
* <p>
* A {@link UserDetails} is returned by implementations. The
* <code>UserDetails</code> must, at minimum, contain the username and
* <code>GrantedAuthority[]</code> objects applicable to the authenticated
* user. Note that Spring Security ignores the password and enabled/disabled
* status of the <code>UserDetails</code> because this is
* authentication-related and should have been enforced by another provider server. The
* <code>UserDetails</code> returned by implementations is stored in the
* generated <code>AuthenticationToken</code>, so additional properties
* such as email addresses, telephone numbers etc can easily be stored.
* </p>
*
* <p>
* Implementations should not perform any caching. They will only be called
* when a refresh is required.
* </p>
*
* @author Ben Alex
* @author Ray Krueger
* @version $Id$
*/
public interface AuthoritiesPopulator {
/**
* Obtains the granted authorities for the specified user.<P>May throw any
* <code>AuthenticationException</code> or return <code>null</code> if the authorities are unavailable.</p>
*
* @param casUserId as obtained from the CAS validation service
*
* @return the details of the indicated user (at minimum the granted authorities and the username)
*
* @throws org.springframework.security.AuthenticationException DOCUMENT ME!
*/
UserDetails getUserDetails(String casUserId)
throws AuthenticationException;
}
@@ -0,0 +1,40 @@
package org.springframework.security.providers;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.AuthenticationException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.util.Assert;
/**
* Populates the CAS authorities via an {@link org.springframework.security.userdetails.UserDetailsService}.<P>The additional information (username,
* password, enabled status etc) an <code>AuthenticationDao</code> implementation provides about a <code>User</code>
* is ignored. Only the <code>GrantedAuthority</code>s are relevant to this class.</p>
*
* @author Ben Alex
* @version $Id$
*/
public class DaoAuthoritiesPopulator implements AuthoritiesPopulator, InitializingBean {
//~ Instance fields ================================================================================================
private UserDetailsService userDetailsService;
//~ Methods ========================================================================================================
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.userDetailsService, "A UserDetailsService must be set");
}
public UserDetails getUserDetails(String casUserId)
throws AuthenticationException {
return this.userDetailsService.loadUserByUsername(casUserId);
}
public UserDetailsService getUserDetailsService() {
return userDetailsService;
}
public void setUserDetailsService(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
}
@@ -15,14 +15,19 @@
package org.springframework.security.providers.cas;
import org.springframework.security.AuthenticationException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.providers.AuthoritiesPopulator;
/**
* <p>
* <i>Backwards compatible extension to the {@link AuthoritiesPopulator} interface.
* This interface has usefulness outside of the CAS usecase. Thus, the {@link AuthoritiesPopulator}
* interface was refactored in.</i>
* </p>
* <p>
* Populates the <code>UserDetails</code> associated with a CAS authenticated
* user.
* </p>
*
* <p>
* CAS does not provide the authorities (roles) granted to a user. It merely
@@ -33,18 +38,6 @@ import org.springframework.security.userdetails.UserDetails;
* </p>
*
* <p>
* A {@link UserDetails} is returned by implementations. The
* <code>UserDetails</code> must, at minimum, contain the username and
* <code>GrantedAuthority[]</code> objects applicable to the CAS-authenticated
* user. Note that Spring Security ignores the password and enabled/disabled
* status of the <code>UserDetails</code> because this is
* authentication-related and should have been enforced by the CAS server. The
* <code>UserDetails</code> returned by implementations is stored in the
* generated <code>CasAuthenticationToken</code>, so additional properties
* such as email addresses, telephone numbers etc can easily be stored.
* </p>
*
* <p>
* Implementations should not perform any caching. They will only be called
* when a refresh is required.
* </p>
@@ -52,19 +45,6 @@ import org.springframework.security.userdetails.UserDetails;
* @author Ben Alex
* @version $Id$
*/
public interface CasAuthoritiesPopulator {
//~ Methods ========================================================================================================
public interface CasAuthoritiesPopulator extends AuthoritiesPopulator {
/**
* Obtains the granted authorities for the specified user.<P>May throw any
* <code>AuthenticationException</code> or return <code>null</code> if the authorities are unavailable.</p>
*
* @param casUserId as obtained from the CAS validation service
*
* @return the details of the indicated user (at minimum the granted authorities and the username)
*
* @throws AuthenticationException DOCUMENT ME!
*/
UserDetails getUserDetails(String casUserId)
throws AuthenticationException;
}
@@ -15,47 +15,21 @@
package org.springframework.security.providers.cas.populator;
import org.springframework.security.AuthenticationException;
import org.springframework.security.providers.cas.CasAuthoritiesPopulator;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.security.providers.DaoAuthoritiesPopulator;
/**
* Populates the CAS authorities via an {@link UserDetailsService}.<P>The additional information (username,
* password, enabled status etc) an <code>AuthenticationDao</code> implementation provides about a <code>User</code>
* is ignored. Only the <code>GrantedAuthority</code>s are relevant to this class.</p>
*
* Backwards compatible placeholder.
* This class will be removed, use {@link DaoAuthoritiesPopulator} instead.
*
* @deprecated Use {@link org.springframework.security.providers.DaoAuthoritiesPopulator}
* @author Ben Alex
* @version $Id$
*/
public class DaoCasAuthoritiesPopulator implements CasAuthoritiesPopulator, InitializingBean {
//~ Instance fields ================================================================================================
private UserDetailsService userDetailsService;
//~ Methods ========================================================================================================
public class DaoCasAuthoritiesPopulator extends DaoAuthoritiesPopulator implements InitializingBean {
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.userDetailsService, "A UserDetailsService must be set");
}
public UserDetails getUserDetails(String casUserId)
throws AuthenticationException {
return this.userDetailsService.loadUserByUsername(casUserId);
}
public UserDetailsService getUserDetailsService() {
return userDetailsService;
}
public void setUserDetailsService(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
super.afterPropertiesSet();
}
}