PDA

View Full Version : extending LdapAuthenticationProvider + using HibernateDaoSupport



RAVEN-I
Oct 30th, 2007, 04:43 AM
Hi Folks,

I find problem using HibernateDaoSupport when I try to get user information from database. My original design was getting ldap authentication through acegi security api and getting user role from my own database.

What I did was extending the LdapAuthenticationProvider and overriding the createUserDetails function inside.

My problem is that while executing the getHibernateTemplate(), a NullPointerException is triggered.

Here is my java class:


public class MyLdapAuthenticationProvider extends LdapAuthenticationProvider {
...
protected UserDetails createUserDetails(LdapUserDetails ldapUser, String username, String password) {
UserDetails userDetails = super.createUserDetails(ldapUser, username, password);
return new AppUserDetailsLdapImpl((LdapUserDetails) userDetails);
}
}


And the Hibernate implementation class is:


public class AppUserDetailsLdapImpl extends HibernateDaoSupport implements LdapUserDetails
{
private final LdapUserDetails ldapUserDetails;
private List userRoleList;


public AppUserDetailsLdapImpl(final LdapUserDetails ldapUserDetails)
{
this.ldapUserDetails = ldapUserDetails;
Attributes attributes = this.ldapUserDetails.getAttributes();
String userName = this.ldapUserDetails.getUsername();
LdapUserDetailsImpl.Essence user = new LdapUserDetailsImpl.Essence(ldapUserDetails);

ArrayList tempGrantedAuthorities = new ArrayList();
userRoleList = getUserRoleList(userName);
Iterator it = userRoleList.iterator();
while (it.hasNext())
{
UserRole userRole = (UserRole)it.next();
GrantedAuthority temp = new GrantedAuthorityImpl(userRole.getRoleID());
tempGrantedAuthorities.add(temp);
}
GrantedAuthority[] extraAuthorities = new GrantedAuthority[tempGrantedAuthorities.size()];
tempGrantedAuthorities.toArray(extraAuthorities);
for (int i = 0; i < extraAuthorities.length; i++) {
user.addAuthority(extraAuthorities[i]);
}
}

public List getUserRoleList(String userID) throws DataAccessException
{
logger.debug("getUserRoleList: " + userID);
return getHibernateTemplate().findByNamedQueryAndNamedPar am("getRolesAuthenticated", "user_id", userID);
}
...


I tried injecting the sessionFactory like the following but another error "No constructor with 0 arguments defined in class 'com.xyz.AppUserDetailsLdapImpl'" comes out:


<bean id="appUserDetailsLdapImpl" class="com.fimat.security.util.AppUserDetailsLdapImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


Grateful if somebody could beam a light on this issue. Thanks!

rasky
Oct 30th, 2007, 06:19 AM
Please post Spring Security questions in the Spring Security forum.