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

The Archive Base Latest Questions

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

I have the following UserDetailsService implementation. The authentication process is working great so far.

  • 0

I have the following UserDetailsService implementation.
The authentication process is working great so far.
How do I store my “MyUser bean” (that logged in successfully ) in the “session” so i can get access to it in other areas in my application

Thanks.

@Transactional(readOnly = true)
public class CustomUserDetailsService implements UserDetailsService {


    private EmployeesApi employeesApi = new EmployeesApi();

    /**
     * Retrieves a user record containing the user's credentials and access. 
     */
    public UserDetails loadUserByUsername(String userName)
            throws UsernameNotFoundException, DataAccessException {

        // Declare a null Spring User
        UserDetails user = null;

        try {


            MyUser employee = employeesApi.getByUserName(userName);



            user =  new User(
                    employee.getUserName(), 
                    employee.getPassword().toLowerCase(),
                    true,
                    true,
                    true,
                    true,
                    getAuthorities(1) );

        } catch (Exception e) {
            logger.error("Error in retrieving user");
            throw new UsernameNotFoundException("Error in retrieving user");
        }


    }
    ....
  • 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-19T17:32:34+00:00Added an answer on May 19, 2026 at 5:32 pm

    Spring Security already stores UserDetails of authenticated user in session for you.

    So, the easiest way to store MyUser in session is to implement a custom UserDetails that contains a reference to MyUser:

    public class MyUserDetails extends User {
        private MyUser myUser;
        public MyUserDetails(..., MyUser myUser) {
            super(...);
            this.myUser = myUser;
        }
        public MyUser getMyUser() {
            return myUser;
        }
        ...
    }
    

    And return it from your UserDetailsService:

    MyUser employee = employeesApi.getByUserName(userName);
    user =  new MyUserDetails(..., myUser);
    

    Then you can easily access MyUser via security context:

    MyUser myUser = ((MyUserDetails) SecurityContextHolder
        .getContext().getAuthentication().getPrincipal()).getMyUser();
    

    In Spring MVC controller:

    @RequestMapping(...)
    public ModelAndView someController(..., Authentication auth) {
        MyUser myUser = ((MyUserDetails) auth.getPrincipal()).getMyUser();
        ...
    }
    

    In JSP:

    <security:authentication var = "myUser" property="principal.myUser" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following script that executes all the .reg files in the current directory.
I have following code that creates Linq query. I've never used Linq until today
I have following Html file with javascript. This gives me error that testCircle is
I have following example string that needs to be filtered 0173556677 (Alice), 017545454 (Bob)
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following goal: From a list in main activity that extends ListActivity, I
Have following Java code,that creates StringBuilder with \n,i.e. carriage return delimiters: while (scanner.hasNextLine()){ sb.append(scanner.nextLine()).append(\n);
I have following code that is executed when doing a mouseenter on a div
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
I have following source. In insertMessage(..), it calls selectMessage to check whether duplicate record

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.