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

  • Home
  • SEARCH
  • 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 8046397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:46:24+00:00 2026-06-05T05:46:24+00:00

I am trying to handle a situation when after an successful authentication with openId

  • 0

I am trying to handle a situation when after an successful authentication with openId provider I discover that there is no account in my db associated with user openId identifier.

Can you tell me how should I handle the situation. Now, I am displaying register form and ask a user for creating an account. However, I have a problem with user authentication status, he is now being seen as authenticated by spring SecurityContext class.

How do I deauthenticate user in my controller action before redirecting to ”register new user page”? Is this approach a good one or should I do it in some other way?

  • 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-06-05T05:46:26+00:00Added an answer on June 5, 2026 at 5:46 am

    Ok, so separating authentication from authorization as was mentioned in Samuel’s post was really helpful. However there are still many gotchas and I found deauthentication still a must because there is no easy way in spring to add to user new roles. So the easiest way is to force user to login again and let spring handle role assignment during login.

    In order to deauthenticate user in spring security you have to invoke:

    SecurityContextHolder.clearContext();
    

    as an alternative you can throw an exception in your UserDetailsService implementation (see below). It has the downside that you would deauthenticate user and lose user context data so it would be impossible to match new user accout with openid account during process of creating new local account. And you have to match those account after user login with traditional username and password. My solution was to deauthenticate user just after creating new account.

    In order to grant user roles(privileges) you have to override UserDetailsService, in case someone find this useful here is my implementation:

    public final class MyUserDetailsService implements UserDetailsService {
        private final UsersDao usersDao;
    
        @Autowired
        public UserDetailsServiceImpl(final UsersDao usersDao) {
            this.usersDao = usersDao;
        }
    
        @Override
        public UserDetails loadUserByUsername(final String username) {      
                UserEntity user = usersDao.getUserByOpenIdIdentifier(username);
                if (user == null) {
                        // there is no such user in our db, we could here throw
                        // an Exception instead then the user would also be deuthenticated 
                        return new User(username, "", new ArrayList<GrantedAuthority>());
                }
    
                //here we are granting to users roles based on values from db
                final Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
                authorities.add(new SimpleGrantedAuthority(user.getUserType().toString()));
    
                final UserDetails result = new User(username, "", authorities);
    
                return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how to handle this situation. I have the following
I am using AJAX in asp:net and am trying to handle a situation where
I am trying to handle flow control situation on producer end. I have a
I'm trying to handle errors that have occurred on other threads the .NET CF
I'm trying to set up Devise 1.2 to allow user authentication via GitGub. As
I'm trying this gem but there is a situation where 2 models should share
I'm trying handle bad json data when parsed through json_decode(). I'm using the following
I am trying to handle groups which meet either once a week, twice a
I've been trying to handle the onkeydown event across multiple frames (no, I unfortunately
I am trying to handle the click of custom button in a jqgrid. I

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.