0

I'm currently working on a System that makes use of Spring Security. I have the Authentication Provider setup but I'm having a problem creating a custom User Type which would store all the information I need.

In the Authentication Provider, I call a "User Service" which is a DAL to the users in the system. This then returns a "User" object. Which is fine and that's all working.

But I then have a MyCompanyUserDetails object defined which contains all the custom attributes I need. This Class extends the "User" class.

My Problem is I can't cast the User to CompanyUserDetails. Am I missing something here ?

The Custom User Details Type:

public class MyCompanyUserDetails extends User {

and in my Authentication Provider:

// Create / Update the user.
User raw_user_details = getUser(username_str, groups_of_user);

// Create our Custom user object filled with the parameters we need for the rest of the system.
MyCompanyUserDetails details = (MyCompanyUserDetails) raw_user_details;

The exception I receive:

java.lang.ClassCastException: User cannot be cast to MyCompanyUserDetails

Any help / guidance would be appreciated...

Johann du Toit
  • 2,609
  • 2
  • 16
  • 31
  • any error or exception that you can provide? – Shades88 Jul 21 '12 at 19:31
  • Which class is this `MyCompanyUserDetails`? You have mentioned `CompanyUserDetails` class that extends `User`. If `MyCompanyUserDetails` also extends class `User` then you are trying to downcast the instance variable, which I think is not possible, which is why you must be getting that exception – Shades88 Jul 21 '12 at 20:55
  • My apologies they are the same class. – Johann du Toit Jul 21 '12 at 21:38
  • then it must be the downcasting problem – Shades88 Jul 21 '12 at 21:45
  • I guess you need to implement your own UserDetailsService in a way that the loadUserByUsername method returns an instance of MyCompanyUserDetails – Ale Zalazar Jul 22 '12 at 00:30

1 Answers1

0

Was a simple DownCasting Problem as Stated by Shades88 in the Comments

Johann du Toit
  • 2,609
  • 2
  • 16
  • 31