Announcement Announcement Module
Collapse
No announcement yet.
0.8.2: User.toString() change breaks Authentication.getName( Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 0.8.2: User.toString() change breaks Authentication.getName(

    After upgrading to Acegi 0.8.2 I can't get the user name from an Authentication object anymore.
    The following code worked with 0.8.2

    Code:
            User user = null;
            Object authObject = event.getPortletRequest().getPortletSession().getAttribute(
                    HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY);
            if (authObject instanceof SecureContext) {
                Authentication auth = ((SecureContext) authObject).getAuthentication();
                if (auth.getName() != null) {
                    String userName = auth.getName();
                    //retrieve user by userName
                }
            }
    (Authentication)auth is an instance of UsernamePasswordAuthenticationToken which extends AbstractAuthenticationToken. There getName() calls this.getPrincipal().toString(). Since the principal is an instance of net.sf.acegisecurity.providers.dao.User toString() returns a verbose message, not only the user name (getUsername() would be appropriate, but is not part of the Principal interface).

    That was a little bit complicated :-)
    To summarize:
    In version 0.8.0 net.sf.acegisecurity.providers.dao.User toString() returned the username, in 0.8.2 it returnes much more information. Is this a bug in Acegi, or is there another way I can get the user name in the above scenareio?

    Rainer

  • #2
    It's a bug I just fixed in CVS. I modified AbstractAuthenticationToken's getName() method as follows:

    Code:
        public String getName() {
            if (this.getPrincipal() instanceof UserDetails) {
                return ((UserDetails) this.getPrincipal()).getUsername();
            }
    
            return this.getPrincipal().toString();
        }
    You could alternatively access the usernamePasswordAuthenticationToken.getPrincipal() and cast it to UserDetails, then calling getUsername().

    Comment

    Working...
    X