Results 1 to 8 of 8

Thread: NullPointerException using getHibernateTemplate()

  1. #1
    Join Date
    Sep 2007
    Posts
    26

    Default NullPointerException using getHibernateTemplate()

    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:
    Code:
    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:
    Code:
    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().findByNamedQueryAndNamedParam("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:
    Code:
    	<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!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You are creating a new one yourself, you don't use the one configured by Spring.

    Code:
    return new AppUserDetailsLdapImpl((LdapUserDetails) userDetails);
    Here you are creating a new one outside the Spring container, so nothing gets injected...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Sep 2007
    Posts
    26

    Default any suggestions?

    Hi mdeinum,

    Do you have any suggestion?
    As I have already extended the LdapAuthenticationProvider in my class, how can I extend the HibernateDaoSupport?

    Or is there any better way to get the data other than the HibernateDaoSupport?

    Thx!

  4. #4
    Join Date
    Sep 2007
    Posts
    26

    Default more try

    I tried to add the HibernateTemplate but cannot solve th NullPointerException issue:
    Code:
    	private HibernateTemplate hibernateTemplate;
    	public void setSessionFactory(SessionFactory sessionFactory) {
    	  hibernateTemplate = new HibernateTemplate(sessionFactory);
    	}
    	public List getUserRoleList(String userID) {
    		return hibernateTemplate.findByNamedQueryAndNamedParam("getRolesAuthenticated", "user_id", userID);
    	}
    And here is the error:
    Code:
    [#|2007-10-30T18:34:17.854+0800|INFO|sun-appserver-pe9.0|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpWorkerThread-8080-1;|2007-10-30 18:34:17,854 DEBUG [org.acegisecurity.context.HttpSessionContextIntegrationFilter] - <SecurityContextHolder now cleared, as request processing completed>
    |#]
    
    [#|2007-10-30T18:34:17.854+0800|SEVERE|sun-appserver-pe9.0|javax.enterprise.system.container.web|_ThreadID=14;_ThreadName=httpWorkerThread-8080-1;_RequestID=9e7e089c-71a1-461c-be9b-2a9e857e6990;|StandardWrapperValve[default]: Servlet.service() for servlet default threw exception
    java.lang.NullPointerException
    	at com.fimat.security.util.LdapRoleAuthenticationProvider.createUserDetails(LdapRoleAuthenticationProvider.java:36)
    	at org.acegisecurity.providers.ldap.LdapAuthenticationProvider.retrieveUser(LdapAuthenticationProvider.java:235)
    	at org.acegisecurity.providers.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:119)
    	at org.acegisecurity.providers.ProviderManager.doAuthentication(ProviderManager.java:195)
    Would you suggest way to inject the sessionFactory? if needed?
    Because the ldapRoleAuthenticationProvider in config file is already defined the constructor-arg of BindAuthenticator and DefaultLdapAuthoritiesPopulator.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    As long as you are creating it yourself and you aren't setting the sessionfactory yourself nothing is going to work.

    The instance you create isn't managed by Spring and thus nothing will ever get injected.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Sep 2007
    Posts
    26

    Default thx

    In view of the complexity of extending the LdapAuthenticationProvider, for simplicity reason, I finally decideded to call the hibernate function directly. In other parts of the application, I'll reuse the hibernate functions exposed by spring.

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Why can't you simply inject the reference you need instead of creating a new one youself?
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  8. #8
    Join Date
    Sep 2007
    Posts
    26

    Default

    I tried to inject the sessionFactory into my own LdapAuthenticationProvider, but I find no place to do that. My LdapAuthenticationProvider is like:

    Code:
    	<bean id="ldapRoleAuthenticationProvider" class="com.xyz.security.util.LdapRoleAuthenticationProvider">
    		<constructor-arg>
    			<bean class="org.acegisecurity.providers.ldap.authenticator.BindAuthenticator">
    				<constructor-arg>
    					<ref local="initialDirContextFactory"/>
    				</constructor-arg>
    				<property name="userSearch" ref="userSearch"/>
    			</bean>
    		</constructor-arg>
    		<constructor-arg>
    			<bean class="org.acegisecurity.providers.ldap.populator.DefaultLdapAuthoritiesPopulator">
    				<constructor-arg>
    					<ref local="initialDirContextFactory"/>
    				</constructor-arg>
    				<constructor-arg>
    					<value>OU=Users,OU=My City</value>
    				</constructor-arg>
    				<property name="groupRoleAttribute">
    					<value>cn</value>
    				</property>
    			</bean>
    		</constructor-arg>
    	</bean>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •