Tuesday, October 15, 2013

Why SimpleGrantedAuthority cannot be resolve to a type?

I am using Spring Security 3.0.8, and have the following code:
    public class MyAuthServiceProvider implements UserDetailsService {
        ...
        ...
        public static List< GrantedAuthority > getGrantedAuthorities(List< String > roles) {
         List< GrantedAuthority > authorities = new ArrayList< GrantedAuthority >();
         for( String role:roles ) {
          authorities.add(new SimpleGrantedAuthority(role));
         }
         return authorities;
        }
    }
As mention in the title, SimpleGrantedAuthority couldn't be resolve. There are 2 solutions on this, the first one is to replace SimpleGrantedAuthority with GrantedAuthorityImpl as shown below:
    ...
    authorities.add(new GrantedAuthorityImpl(role));
    ...
The second one is to replace SimpleGrantedAuthority with SwitchUserGrantedAuthority, but I need to implements AuthenticationProvider. I refuse this solution because I feel that (correct me if I'm wrong) this doesn't sound right for MyAuthServiceProvider to implement AuthenticationProvider and UserDetailsService at the same time.

I'm sure I have include spring-security-core library in MAVEN. May I know what else was missing causing this error on SimpleGrantedAuthority? It has been confusing me for a long time, now I got the answer. SimpleGrantedAuthortiy is a replacement of GrantedAuthorityImpl, and this class will be deprecated in Spring Security 3.1. In addition, both of this class are implementing GrantedAuthority, have a look here for SimpleGrantedAuthority and GrantedAuthorityImpl. Thus it has no issue since both of them implementing the same function.

To conclude this, since I'm using version 3.0, then I am safe to use GrantedAuthorityImpl class.

No comments: