Announcement Announcement Module
Collapse
No announcement yet.
ClassCastException with ctx.getAuthentication() Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException with ctx.getAuthentication()

    Hi,

    I need your help. I just don't get it!

    I am using Acegi 1.0.4 with Spring 2.0 and iBatis as ORM. Everything ist working fine, except...


    When I try to get my custom UserDetails object in my MVC-controller with

    SecurityContext ctx = SecurityContextHolder.getContext();
    AcegiUserDetails userDetails = (AcegiUserDetails) ctx.getAuthentication();

    then I get a ClassCastException:

    java.lang.ClassCastException: org.acegisecurity.providers.UsernamePasswordAuthen ticationToken

    BTW, with changing the second line into

    UserDetails userDetails = (UserDetails) ctx.getAuthentication();

    I get the same exception.


    With debugging I can see that all data of my custom UserDetails object AcegiUserDetails actualls IS there, when I call ctx.getAuthentication(). I just don't get it out of there.

    Any ideas???

  • #2
    Please use the [ code][ /code] tags when posting code/errormessages/stacktraces you make a lot of the (regular) people around here a lot happier.

    The ClassCastException should have triggered you that there is something wrong. If you call SecurityContext.getAuthentication () a Authentication instance is returned NOT a UserDetails instance. You cannot cast a Authentication object into a UserDetails object, hence the classcast.

    If you want to have the UserDetails you will need to call getPrincipal() on the Authentication Object.

    With debugging I can see that all data of my custom UserDetails object AcegiUserDetails actualls IS there, when I call ctx.getAuthentication(). I just don't get it out of there.
    It is there only INSIDE the Authentication object, the fact that something is INSIDE an object doesn't mean that you can simply cast the enclosing object into the object you want.

    Comment


    • #3
      Originally posted by mdeinum View Post
      Please use the [ code][ /code] tags when posting code/errormessages/stacktraces you make a lot of the (regular) people around here a lot happier.
      Thanks for the the hint. Actually, I was wondering how it is done with the "code style" in other posts.

      Originally posted by mdeinum View Post
      If you want to have the UserDetails you will need to call getPrincipal() on the Authentication Object.
      That did it. That simple. I tried so much around that I missed this. Thanks!

      Comment

      Working...
      X