简体   繁体   中英

Spring security role assignment

JEE containers typically provide a mechanism using proprietary deployment descriptors to map external to internal user roles. That is, the application declares and uses the internal roles in web.xml and there is a file (eg weblogic.xml for weblogic) that maps the actual roles assigned to a user to the internal ones.

How can such a mapping be achieved when using Spring Security? I am using Spring Security 3.0.x.

Spring Security 3.0.x. does not provide such a mapping out of the box.

But you can implement it by your own, by extending the Authentication Provider that is used for your authentication method.

If you use a DaoAuthenticationProvider (that use internaly a UserDetailsService ) then you can override the addCustomAuthorities(String username, List<GrantedAuthority> authorities) method to add new/mapped roles according to the already granted once.

for example a Extended UserDetailsService :

...
@Override
protected void addCustomAuthorities(String username, List<GrantedAuthority> authorities) {
    super.addCustomAuthorities(username, authorities);

    List<GrantedAuthority> additional = new ArrayList<GrantedAuthority>();
    for (GrantedAuthority role : authorities) {
        additional .addAll(vourMappingService.getAdditionalForRole(role));
    }
    authorities.addAll(additional );
}

that use the YourMappingService in to map the roles (by adding new roles to the existing once)

public class YourMappingService


 /**
     * Property bases mapping of roles to privileges.
     * Every role is one line, the privileges are comma separated.
     */
    private Properties roleToPrivileges;

    public YourMappingService(Properties roleToPrivileges) {
        if (roleToPrivileges == null) {
            throw new IllegalArgumentException("roleToPrivileges must not be null");
        }
        this.roleToPrivileges = roleToPrivileges;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAdditionalForRole(GrantedAuthority role) {

        String authority = role.getAuthority();
        if(authority != null) {
            String commaSeparatedPrivileges = roleToPrivileges.getProperty(role.getAuthority());
            if (commaSeparatedPrivileges != null) {
                List<GrantedAuthority> privileges = new ArrayList<GrantedAuthority>();
                for(String privilegeName : StringUtils.commaDelimitedListToSet(commaSeparatedPrivileges)) {
                    privileges.add(new GrantedAuthorityImpl(privilegeName.trim()));
                }                
                return privileges;
            } else {
                return Collections.emptyList();
            }
        } else {
            return Collections.emptyList();
        }
    }   
}

configuration:

<bean id="myUserDetailsService" class="de.humanfork.springsecurityroles.impl.JdbcDaoPrivilegesImpl">
    <constructor-arg ref="yourMappingService"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="usersByUsernameQuery" value="SELECT login,encryptedPassword,loginEnabled FROM user WHERE login = ?"/>
    <property name="enableAuthorities" value="true"/>
    <property name="authoritiesByUsernameQuery" value="SELECT u.login, r.securityRoles FROM user u, user2security_roles r WHERE u.login= ? AND u.id = r. User_fk;"/>
</bean>


  <bean id="yourMappingService" class="ZourMappingService">
    <constructor-arg>
        <props>
        <prop key="ROLE_ADMIN">
                ROLE_backend_access,
                ROLE_user_mngt,
                ROLE_passwordLostRequest_mngt,
                ROLE_log_mngt
            </prop>
            <prop key="ROLE_USER">
            </prop>
        </props>
    </constructor-arg>
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM