Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Create users using Spring Security and DMI

    **** EDIT: solved in the post below ****

    Hello,

    Maybe this is a Spring Security question not relevant for this forum, but my problem might be caused by smartGWT.

    I am building a user management module, and I created a form that creates a new user in a mysql user table when submitted. I call dynamicform.saveData() for this, and I get the data through to the add(..) method. This add method internally does a call to getJdbcTemplate(), which returns null, and thus a NullPointerException. So my question is:

    Question: if you call a method using DMI, are all the Spring beans initiated like specified in my applicationContext-security.xml? I am not really experienced in Spring, but do have some basic knowledge.

    Anyone knows what is going wrong? If I actually start the server and watch the jdbcTemplate variable it is initialized. When the DMI comes in, it is null. I am basically using the implementaiton from the SpringSecurity thread at http://forums.smartclient.com/showth...pring+security ...

    My code looks like this:
    nl.sytematic.projects.Persons.server.security.SytematicJdbcDaoImpl:
    Code:
    public class SytematicJdbcDaoImpl extends JdbcUserDetailsManager
    		implements
    			IChangePassword {
    ...
    	public DSResponse add(DSRequest req) {
    		String username = (String) req.getFieldValue("username");
    		String password = (String) req.getFieldValue("password");
    		Boolean enabled = (Boolean) req.getFieldValue("enabled");
    
    		if (!userExists(username)) {
    			SytematicSaltedUser user = new SytematicSaltedUser(username,
    				password, enabled, true, true, true,
    					AuthorityUtils.NO_AUTHORITIES, "");
    			createUser(user);
    
    			//this makes that the password gets encoded
    			changePassword(username, password);
    		}
    ...
    }
    my users.ds.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <DataSource ID="users" serverType="sql" tableName="users"> 
      <fields> 
        <field name="username" type="text" length="50" primaryKey="true" required="true"/>  
        <field name="password" type="password" length="50" required="true"/>  
        <field name="salt" type="hidden"/>  
        <field name="enabled" type="boolean" required="false"/> 
      </fields>  
      
      <serverObject lookupStyle="new" className="nl.sytematic.projects.Persons.server.security.SytematicJdbcDaoImpl"/> 
    </DataSource>

    applicationContext-security.xml
    Code:
    ...
      <security:authentication-manager alias="authenticationManager"> 
        <security:authentication-provider user-service-ref="jdbcUserService"> 
          <security:password-encoder ref="passwordEncoder"> 
            <security:salt-source ref="saltSource"/> 
          </security:password-encoder> 
        </security:authentication-provider> 
      </security:authentication-manager>  
      <beans:bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource"> 
        <beans:property name="userPropertyToUse" value="salt"/> 
      </beans:bean>  
    
      <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
        <beans:property name="url" value="jdbc:mysql://localhost:3306/sytematic"/>  
        <beans:property name="username" value="test"/>  
        <beans:property name="password" value="test"/> 
      </beans:bean>  
      <beans:bean class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" id="passwordEncoder"/>  
    
      <beans:bean id="jdbcUserService" class="nl.sytematic.projects.Persons.server.security.SytematicJdbcDaoImpl"> 
        <beans:property name="dataSource" ref="dataSource"/>  
        <beans:property name="authenticationManager" ref="authenticationManager"/>  
        <beans:property name="enableGroups" value="true"/>  
        <beans:property name="enableAuthorities" value="false"/>  
        <beans:property name="usersByUsernameQuery"> 
          <beans:value>select username, password, enabled, salt from users where username = ?</beans:value> 
        </beans:property> 
      </beans:bean>  
    ...
    Last edited by Kah0ona; 4 Oct 2010, 07:14.

    #2
    Hmm after reading some more, I tried to put the lookupStyle="spring". Sounds logical, once you know it of course, but I just failed to realize this. If you use 'lookupStyle="new"', the server-methods are called, but the whole spring context will not be loaded.


    Took me an afternoon to figure out but now I am happy. Hope this helps at least someone :-)

    Comment

    Working...
    X