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 6642413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:55:39+00:00 2026-05-25T23:55:39+00:00

I have a grails application that uses spring-security-core and spring-security-ldap, with authentication against Active

  • 0

I have a grails application that uses spring-security-core and spring-security-ldap, with authentication against Active Directory. I have a custom UserDetails and UserDetailsContextMapper.

I have a use case where a user can temporarily take on additional responsibilities for a single session. I would like to be able to assign a role to the user after they have already logged in (i.e. mid-session), and then remove that role when the session expires.

I have a user_role database table that stores relationships between users and roles. In this case, it does not matter whether the relationship is added to the database and then removed when the session expires, or simply exists in memory only. Either way, I am looking for a way to assign the role (and have it immediately apply) at a point after the user has logged in. Is this possible?

  • 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-25T23:55:39+00:00Added an answer on May 25, 2026 at 11:55 pm

    The UserDetails.getAuthorities() holds all roles of currently logged in user. As You already have a custom UserDetails You only need to add the additional role to the values returned by getAuthorities() on the mid-session start, and then remove it on the mid-session end. (see edit below)

    ie:

    public class MyUserDetails implements UserDetails {
        // holds the authorities granted to the user in DB
        private List<GrantedAuthority> authorities;
        // holds the temporarily added authorities for the mid-session
        private List<GrantedAuthority> extraAuthorities;
    
       public GrantedAuthority[] getAuthorities() {
           List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
           auths.addAll(this.authorities);
           auths.addAll(this.extraAuthorities);
           return auths;
       }
    
       public void addExtraAuthorities(GrantedAuthority...auths) {
           for (GrantedAuthority a : auths) {
               this.extraAuthorities.add(a);
           }
       }
    
       public void clearExtraAuthorities() {
           this.extraAuthorities.clear();
       }
    }
    

    Or You could create a wrapper around the original UserDetails that will hold the extra authorities and wrap it and set to the current Authentication object on the mid-session start and unwrap on the mid-session end.


    Edit:

    As Ben pointed out in the comment the UserDetails.getAuthorities() does get called only once, at the login, when the Authentication object is created – I forgot about that. But that brings us to the right answer, and this time I’m sure it’s gonna work, cause I did it myself this way. The Authentication.getAuthorities() is the method to attend to, not the UserDetails.getAuthorities(). And I really recommend a wrapper for that, not the custom implementaion of Authentication interace, as it’ll be more flexible and allow to transparentlysupport diffrent authentication mechanisms underneath.

    ie:

    public class MidSessionAuthenticationWrapper implements Authentication {
    
        private Authentication wrapped;
        private List<GrantedAuthority> authorities;
    
        public MidSessionAuthenticationWrapper(
                Authentication wrapped, Collection<GrantedAuthority> extraAuths) {
            this.wrapped = wrapped;
            this.authorities = new ArrayList<GrantedAuthority>(wrapped.getAuthorities());
            this.authorities.addAll(extraAuths);
        }
    
        public Authentication getWrapped() {
            return this.wrapped;
        }
    
        @Override
        public Collection<GrantedAuthority> getAuthorities() {
            return this.authorities;
        }
    
        // delegate all the other methods of Authentication interace to this.wrapped
    }
    

    Then all You need to do is on the mid-session start:

    Authentication authentication = 
        SecurityContextHolder.getContext().getAuthentication();
    Collection<GrantedAuthority> extraAuths = 
        null; // calculate the extra authorities
    MidSessionAuthenticationWrapper midSessionAuthentication =
        new MidSessionAuthenticationWrapper(authentication, extraAuths);
    SecurityContextHolder.getContext().setAuthentication(midSessionAuthentication);
    

    And on the mid-session end:

    MidSessionAuthenticationWrapper midSessionAuthentication =
        (MidSessionAuthenticationWrapper) SecurityContextHolder.getContext().getAuthentication();
    Authentication authentication = midSessionAuthentication.getWrapped();
    SecurityContextHolder.getContext().setAuthentication(authentication);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing grails application that uses spring-security plugin for authentication. I would
I'm developing a Grails (Version 1.3.3) Web-Application using the Grails Spring-Security Plugin, Spring-Security-Core-1.0.1 (which,
I have a Grails application that uses ThreadLocal. Currently ThreadLocal populating and clearing is
I have integrated spring security plugin with my grails application, which has hibernate as
I have one grails application.In that I have one model class named Book. From
I have a groovy/grails application that needs to serve images It works fine on
I have a Java application (generic) that uses a database via hibernate. I ship
I have a Grails application that I am migrating from 1.0.3 to 1.3.7 It
I am maintaining a Grails application that I did not write(I have no experience
In my grails application I have an sql query that selects column data from

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.