Results 1 to 5 of 5

Thread: Programaticly authentifie

  1. #1

    Default Programaticly authentifie

    Hi all,

    When a user create a new account, I'd like him to be automaticly authentified. I mean I don't want to ask him to authenticate when he already gave all the information about it's account.

    How can I do that ?

    Thanks.

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

    Default

    I've not tried it, but couldn't you just set the authentication on the context?

    Code:
    Authentication auth = null; // build it.
    SecurityContextHolder.getContext().setAuthentication(auth);

  3. #3

    Default

    It sounds good, the point is how to build an auth, as Authentication is an interface, I've got to find the factory....

    I have to dig, thanks for this hint.

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

    Default

    On second thoughts if your up in the MVC layer, couldn't you just mimic what the login page does? Put the username and password parameters in the request under the named parameters and post to the URL?

  5. #5

    Default

    Well it's a solution, but I found the programmatic solution.

    I add some property to my controller coming from the applicationContext-security.xml (authenticationManager and rememberMeServices)

    Code:
    <bean id="createAccountController" class="springshop.web.CreateAccountController">
    
            <!-- property coming from the security context -->
            <property name="authenticationManager" ref="authenticationManager"/>
            <property name="rememberMeServices" ref="rememberMeServices"/>
    
            <!-- property related to the controller -->
            <property name="sessionForm">
                <value>true</value>
            </property>
            <property name="commandName">
                <value>user</value>
            </property>            
            <property name="formView">
                <value>createAccount</value>
            </property>
            <property name="validator">
                <bean class="springshop.web.form.validator.AccountValidator" />
            </property>
            <property name="userDBService">
                <ref bean="userDBService"/>
            </property>
            <property name="mailerService">
                <ref bean="mailerService"/>
            </property>        
    </bean>
    And Here is the code I add in the onSubmit() of my controller.


    Code:
            //that's it let's activate the account
            GrantedAuthorityImpl[] arrayAuths = new GrantedAuthorityImpl[roles.size()];
            int index = 0;
            for (Iterator it=roles.iterator(); it.hasNext(); ) {
                r =   (Role) it.next();
                GrantedAuthorityImpl authority = new GrantedAuthorityImpl(r.getName());
                arrayAuths[index++] = authority;
            }
            UsernamePasswordAuthenticationToken spa = new UsernamePasswordAuthenticationToken(user.getLogin(),user.getPassword(),arrayAuths);
            SecurityContextHolder.getContext().setAuthentication(spa);
            request.getSession().setAttribute(ACEGI_SECURITY_LAST_USERNAME_KEY, user.getLogin());
            AuthenticationManager authenticationManager = getAuthenticationManager();
            authenticationManager.authenticate(spa);
            getRememberMeServices().loginSuccess(request, response, spa);
    That's it the user is authenticated, I don't publish event, but I should...

Posting Permissions

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