Announcement Announcement Module
Collapse
No announcement yet.
Programaticly authentifie Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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);

    Comment


    • #3
      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.

      Comment


      • #4
        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?

        Comment


        • #5
          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...

          Comment

          Working...
          X