Announcement Announcement Module
Collapse
No announcement yet.
Custom UserDetails, UserDetailsService impl fails to getPrincipal as expected. Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom UserDetails, UserDetailsService impl fails to getPrincipal as expected.

    Ok,

    before flaming me, I searched and searched this and other forums. No matter what I try getPrincipal only returns a String. I have tried all the options that I could find to resolve this issue without luck. I have to be missing something in my configuration or implementation classes. I have audited and reviewed my files for several hours and still can't find the error (code excerpts below).

    I found the older post:
    http://forum.springframework.org/arc...hp/t-9886.html

    jameli
    Junior Member Join Date: Aug 2004
    Posts: 6
    Why the type of auth.getPrincipal() is always String ?
    ...
    and Ben's reply:
    Ben Alex
    03-29-2005, 07:01 PM
    We've covered this on the acegisecurity-developers list. For the benefit of the forums, see http://www.mail-archive.com/acegisec.../msg00876.html.

    but no matter what I try I can't the the UserDetails information as anything but a String.

    My impl / config information:
    Excerpt from class trying to get the Member (UserDetails) object:

    Code:
            Member member =                  
               (Member)SecureContextUtils.getSecureContext()
                   .getAuthentication().getPrincial();
            
            // Fails  ClassCastException  getPrincipal() returning String
    
            /** have tried from one example 
            Authentication auth = null;
            if ((auth = SecurityContextHolder.getContext().getAuthentication()) == null) {
                return null;
            }
    
            logger.debug("PRINCIPAL = " + auth.getPrincipal());
            logger.debug("DETAILS = " + auth.getDetails());
            
            Member member = (Member)auth.getPrincipal();
    
            // fails  getPrinciple still returning String
            */
    
            /** from another example
            String username = auth.getPrincipal().toString();
            String password = auth.getCredentials().toString();
            GrantedAuthority[] authorities = auth.getAuthorities();
            UsernamePasswordAuthenticationToken authTok = new                
                UsernamePasswordAuthenticationToken(username,
                                                    password, authorities);
            Member member = (Member)authTok.getPrincipal();
    
            // fails  getPrinciple still returning String
            */
    UserDetailsService Impl:

    Code:
    public class AuthenticationService implements UserDetailsService {
    
    .
    .
    .
        public Member loadUserByUsername(String username) {
    
            Member member = memberService.getMember(username);
    
            if (member != null && member.getPassword() != null) {
                // add additional ACEGI values
                member.setEnabled(true);
                member.setAccountNonExpired(true);
                member.setCredentialsNonExpired(true);
                member.setAccountNonLocked(true);
            } else {
                throw new UsernameNotFoundException(messageSource.getMessage(
                        "login.user.unknown", null, "Invalid user", Locale
                                .getDefault()));
            }
            logger.debug(member.toString());
            return member;
        }
    .
    .
    .
    }
    Member Object:

    Code:
    public class Member implements UserDetails {
    
        .
        .
        .
    
        private GrantedAuthority[] authorities;
    
        // Acegi related fields
    
        public GrantedAuthority[] getAuthorities() {
            return authorities;
        }
    
        public void setAuthorities(GrantedAuthority[] authorities) {
            this.authorities = authorities;
        }
    
        public boolean isAccountNonExpired() {
            return accountNonExpired;
        }
    
        public void setAccountNonExpired(boolean expired) {
            accountNonExpired = expired;
        }
    
        public boolean isAccountNonLocked() {
            return accountNonLocked;
        }
    
        public void setAccountNonLocked(boolean locked) {
            accountNonLocked = locked;
        }
    
        public boolean isCredentialsNonExpired() {
            return credentialsNonExpired;
        }
    
        public void setCredentialsNonExpired(boolean expired) {
            credentialsNonExpired = expired;
        }
        .
        .
        .
    }
    Application Context:

    Code:
      <bean id="daoAuthenticationProvider" 
    	class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="authenticationService" />
        <!-- <property name="passwordEncoder" ref="shaHexPasswordEncoder" /> -->
        <property name="hideUserNotFoundExceptions" value="false" />
        <property name="messageSource" ref="messageSource" />
        <property name="forcePrincipalAsString" value="false"/>
      </bean>
    
      <bean id="authenticationService" class="org.myorg.security.AuthenticationService">
        <property name="memberService" ref="memberService" />
        <property name="messageSource" ref="messageSource" />
      </bean>

    Any information is appreciated, this is driving me crazy .

    Thanks, Phil

  • #2
    additional note

    I did not include all of the definition of my Member class that implements UserDetails, however, it has all of the data members expected (i.e. username, password, etc and all respective getters and setters).

    Comment


    • #3
      Philc,

      It's returning a string because initially your principal is a string variable. You can't covert a string to a Member (I would assume it would be User) object.

      Comment


      • #4
        java.lang.ClassCastException: org.acegisecurity.userdetails.User

        Same thing happened for me also. I was trying to configure cas. In my case my settings contained the follwing code


        <bean id="casAuthoritiesPopulator" class="org.acegisecurity.providers.cas.populator.D aoCasAuthoritiesPopulator">
        <property name="userDetailsService"><ref bean="jdbcDaoImpl"/></property>
        </bean>


        <bean id="jdbcDaoImpl"
        class="org.acegisecurity.userdetails.jdbc.JdbcDaoI mpl">
        <property name="dataSource">
        <ref bean="dataSource" />
        </property>
        <property name="usersByUsernameQuery">
        <value>
        SELECT USER_NAME as username, PWD as password, 1 as
        enabled FROM SA_USERAUTH_V WHERE USER_NAME = ?
        </value>
        </property>
        <property name="authoritiesByUsernameQuery">
        <value>
        SELECT USER_NAME as username, CONCAT('ROLE_', USER_TYPE)
        as rolename FROM SA_USERAUTH_V WHERE USER_NAME
        = ?
        </value>
        </property>
        </bean>


        note the jdbcDaoImpl. This was the code cousing the error.

        I changed the code as bellow:

        <bean id="casAuthoritiesPopulator" class="org.acegisecurity.providers.cas.populator.D aoCasAuthoritiesPopulator">
        <property name="userDetailsService"><ref bean="userDao"/></property>
        </bean>

        <bean id="userDao"
        class="com.mycompany.myapp.dao.hibernate.UserDaoHi bernate">
        <property name="sessionFactory">
        <ref local="sessionFactory"/>
        </property>
        </bean>


        Hope this will help.

        rubel ahammad

        Comment


        • #5
          It looks like "forcePrincipalAsString"="true". In your configuration this property value is "false", but...
          Try to debug. You need to trace 4 last lines of the method "org.acegisecurity.providers.dao.AbstractUserDetai lsAuthenticationProvider.authenticate(Authenticati on)" code.

          Comment


          • #6
            Did you found a solution? I have the same problem...
            Thanks
            Cathy

            Comment

            Working...
            X