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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:21:00+00:00 2026-06-10T05:21:00+00:00

This is a spring security question. I want to be able to retrieve my

  • 0

This is a spring security question.

I want to be able to retrieve my custom User object object using

SecurityContextHolder.getContext().getAuthentication().getPrincipal();

because I need additional details like user id and stuff for further business logic in my classes.

For this, I have a custom class derived from org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl where I’ve overriden loadUsersByUsername() to populate additional user fields and return an object of MyUser with the extra fields like user id etc.

Code –

public class MyJdbcDaoImpl extends JdbcDaoImpl {

@Override
protected List<UserDetails> loadUsersByUsername(String username) {
    return getJdbcTemplate().query(getUsersByUsernameQuery(), new String[] {username}, new RowMapper<UserDetails>() {
        public UserDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
            Long id = rs.getLong(1);
            String username = rs.getString(2);
            String password = rs.getString(3);
            boolean enabled = rs.getBoolean(4);
            MyUser user = new MyUser(username, password, enabled, true, true, true, AuthorityUtils.NO_AUTHORITIES);
            user.setId(id);
            return user;
        }

    });
}
}

My application context –

<beans:bean id="myJdbcDaoImpl" class="package.MyJdbcDaoImpl">
        <beans:property name="dataSource" ref="securityDB"/>
        <beans:property name="authoritiesByUsernameQuery" value="select username,role_id from app_user,user_role where app_user.id = user_role.user_id and username=?"/>
        <beans:property name="usersByUsernameQuery" value="select id,username,password,account_enabled from app_user where username = ?"/>
</beans:bean>

<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="myJdbcDaoImpl">
       <password-encoder hash="sha"/>
</authentication-provider>

The issue is, when I do a

MyUser user = (MyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

I get an exception
java.lang.ClassCastException: org.springframework.security.core.userdetails.User cannot be cast to package.MyUser

What should I do to access my user object with additional fields from SecurityContextHolder?

Thanks

  • 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-10T05:21:01+00:00Added an answer on June 10, 2026 at 5:21 am

    You’ve overriden wrong method, additionally override createUserDetails which creates final User object from these fetched via loadUsersByUsername:

    @Override
    protected UserDetails createUserDetails(final String username,
        final UserDetails userFromUserQuery,
        final List<GrantedAuthority> combinedAuthorities) {
      String returnUsername = userFromUserQuery.getUsername();
    
      if (!isUsernameBasedPrimaryKey()) {
        returnUsername = username;
      }
    
      final MyUser userToReturn = new MyUser(returnUsername,
          userFromUserQuery.getPassword(), userFromUserQuery.isEnabled(), true,
          true, true, combinedAuthorities);
      userToReturn.setId(((MyUser) userFromUserQuery).getId());
      return userToReturn;
    }
    

    userToReturn.setId(((MyUser) userFromUserQuery).getId()); will work if you used your code, or you can remove loadUsersByUsername and fetch id right inside createUserDetails which doc’s says:

    Can be overridden to customize the creation of the final
    UserDetailsObject which is returned by the loadUserByUsername
    method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a Spring Security question. In my application, I have a User entity
Using Spring 3 MVC and JSP, I simply want to test if a user
I'm using Spring 3.1.0.RELEASE with Spring Security 3.1. I want to inject my Spring
I have this security question answer input field validate function where I want to
I have this inside my spring xml file: <object type=Test.Web.Utilities.TestClass, Test.Web singleton=false id=TestClass> <property
This is an example of using ThrowawayController in Spring MVC: public class DisplayCourseController implements
This is a basic question - when spring bean is loaded and if i
I am using the new Spring Security 3.1 and have a few questions to
Question: Is there a way to make a function object in python using strings?
I use Spring Security and Apache proxy for a web app. When using standard

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.