Handling roles when authenticated to active directory with spring security 3.1

前端 未结 2 1135
天涯浪人
天涯浪人 2020-12-13 03:10

I\'m trying to use a authenticate with an Active directory using Spring Security 3.1. I get authenticated and all is well.



        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 03:35

    You can also inject a GrantedAuthoritiesMapper which was introduced in 3.1 as a general strategy for modifying the authorites. Plus you might want to use SimpleGrantedAuthority for the GrantedAuthority implementation. Alternatively, you could use an enum since you have a fixed set of values:

    enum MyAuthority implements GrantedAuthority {
        ROLE_ADMIN,
        ROLE_USER;
    
        public String getAuthority() {
            return name();
        }
    }
    
    
    class MyAuthoritiesMapper implements GrantedAuthoritiesMapper {
    
        public Collection mapAuthorities(Collection authorities) {
            Set roles = EnumSet.noneOf(MyAuthority.class);
    
            for (GrantedAuthority a: authorities) {
                if ("MY ADMIN GROUP".equals(a.getAuthority())) {
                    roles.add(MyAuthority.ROLE_ADMIN);
                } else if ("MY USER GROUP".equals(a.getAuthority())) {
                    roles.add(MyAuthority.ROLE_USER);
                }
            }
    
            return roles;
        }
    }
    

提交回复
热议问题