Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3680366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:34:06+00:00 2026-05-19T03:34:06+00:00

After a new user submits a ‘New account’ form, I want to manually log

  • 0

After a new user submits a ‘New account’ form, I want to manually log that user in so they don’t have to login on the subsequent page.

The normal form login page going through the spring security interceptor works just fine.

In the new-account-form controller I am creating a UsernamePasswordAuthenticationToken and setting it in the SecurityContext manually:

SecurityContextHolder.getContext().setAuthentication(authentication);

On that same page I later check that the user is logged in with:

SecurityContextHolder.getContext().getAuthentication().getAuthorities();

This returns the authorities I set earlier in the authentication. All is well.

But when this same code is called on the very next page I load, the authentication token is just UserAnonymous.

I’m not clear why it did not keep the authentication I set on the previous request. Any thoughts?

  • Could it have to do with session ID’s not being set up correctly?
  • Is there something that is possibly overwriting my authentication somehow?
  • Perhaps I just need another step to save the authentication?
  • Or is there something I need to do to declare the authentication across the whole session rather than a single request somehow?

Just looking for some thoughts that might help me see what’s happening here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:34:07+00:00Added an answer on May 19, 2026 at 3:34 am

    I had the same problem as you a while back. I can’t remember the details but the following code got things working for me. This code is used within a Spring Webflow flow, hence the RequestContext and ExternalContext classes. But the part that is most relevant to you is the doAutoLogin method.

    public String registerUser(UserRegistrationFormBean userRegistrationFormBean,
                               RequestContext requestContext,
                               ExternalContext externalContext) {
    
        try {
            Locale userLocale = requestContext.getExternalContext().getLocale();
            this.userService.createNewUser(userRegistrationFormBean, userLocale, Constants.SYSTEM_USER_ID);
            String emailAddress = userRegistrationFormBean.getChooseEmailAddressFormBean().getEmailAddress();
            String password = userRegistrationFormBean.getChoosePasswordFormBean().getPassword();
            doAutoLogin(emailAddress, password, (HttpServletRequest) externalContext.getNativeRequest());
            return "success";
    
        } catch (EmailAddressNotUniqueException e) {
            MessageResolver messageResolvable 
                    = new MessageBuilder().error()
                                          .source(UserRegistrationFormBean.PROPERTYNAME_EMAIL_ADDRESS)
                                          .code("userRegistration.emailAddress.not.unique")
                                          .build();
            requestContext.getMessageContext().addMessage(messageResolvable);
            return "error";
        }
    
    }
    
    
    private void doAutoLogin(String username, String password, HttpServletRequest request) {
    
        try {
            // Must be called from request filtered by Spring Security, otherwise SecurityContextHolder is not updated
            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
            token.setDetails(new WebAuthenticationDetails(request));
            Authentication authentication = this.authenticationProvider.authenticate(token);
            logger.debug("Logging in with [{}]", authentication.getPrincipal());
            SecurityContextHolder.getContext().setAuthentication(authentication);
        } catch (Exception e) {
            SecurityContextHolder.getContext().setAuthentication(null);
            logger.error("Failure in autoLogin", e);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such view that handles user registration. After creating new user i want
I have a simple, dynamic email form field. After the user submits their email,
I have a form that a user submits. The form is for a user.
Basically, I have a page that contains a form. When a user submits that
I was wondering how I give the user the new add-ons after they purchase
I want to redirect_to slider_path after a user submits their email. Currently, only the
I have a simple form that generates a new photo gallery, sending the title
I have a tweets_controller #called when user submits twitter form def message unless current_user
I have the following situation: the user submits a form, I fetch the data
Im trying to test my successfully creates a new user after login (using authlogic).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.