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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:29:59+00:00 2026-05-29T19:29:59+00:00

I have multiple projects that need to pass user login information to common-api project

  • 0

I have multiple projects that need to pass user login information to common-api project that they call. The common-api does not have user credentials passed in via method calls, so I am hoping to make this information available via Threadlocal variables.

The first such project that needs to make this information available is a rest-web-service-api project that uses Spring Security, and sits on top of my common-api project.

I see that Spring Security stores the user information in a ThreadLocal variable… is there a way to access this from the common-api project without necessarily needing to know that Spring Security is who set it? I am quite unfamiliar with Spring Security and new to using Threadlocals so any help would be greatly appreciated.

  • 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-29T19:30:01+00:00Added an answer on May 29, 2026 at 7:30 pm

    Posting my own solution here in case it’s useful down the road to someone else.

    Spring security (by default) does in fact store the user information in a Threadlocal variable, but the common-api will not have access to it unless it adds a spring-security dependency -if it does, the user information can be accessed (assuming you’re executing in the same thread) using the following snippet:

    SecurityContext secureContext = SecurityContextHolder.getContext();
    Authentication auth = secureContext.getAuthentication();
    Object principal = auth.getPrincipal();
    
    String userName = null;
    if (principal instanceof UserDetails) {
        UserDetails userDetails = (UserDetails) principal;
        userName = userDetails.getUsername();
    } else {
        userName = principal.toString();
    }
    

    The solution I implemented was different however, because I did not want to introduce Spring Security to the common-api project, and I couldn’t always rely on Spring Security being in the user of the API. So, I created a Singleton bean in the common-api that contains my own defined ThreadLocal variables that can be set from other projects and accessed from the common-api project (again, assuming they’re running in the same thread).

    Code Snippet:

    public class CommonAPIThreadLocalStorageManager {
        private static CommonAPIThreadLocalStorageManager instance;
    
        // Typical singleton private constructor, static getInstance here
    
        private ThreadLocal<String> userID = new ThreadLocal<String>();
    
        public String getUserID() {
            return userID.get();
        }
    
        public void setUserID(String userID) {
            this.userID.set(userID);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project where there are multiple applications that have some common configuration
I have a project that consists of multiple projects, something like: my_project\ proj_A\ (code,
Background: I have a project where i need to create multiple objects that consume
I have multiple projects that need to share resource files (.resx) Suggestions have been
Many times I have seen Visual Studio solutions which have multiple projects that share
I have a large Java app that is split up into multiple projects. Each
For example, I have a document library that hold contracts for multiple projects. So:
I have a Visual Studio 2008 C++ project that has support for using multiple
I have multiple rave reports(projects) in the project(the delphi project) and I want to
I have a solution with multiple projects and we need to do some serious

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.