1

Spring Security を使用し、LDAP データベースから認証しています。認証は正常に機能しますが、役割を利用できません!

spring-security.xml には、次のタグがあります。

<security:intercept-url pattern="/app/main/admin" access="hasRole('ROLE_USER')"/>

私の質問は、「ROLE_USER」はどこに定義されているのですか? ユーザーを特定のロールに所属させるにはどうすればよいですか? これは LDAP データベースで発生しますか? はいの場合、どうすればそれを行うことができますか? 私は LDAP についてほとんど知りません。役割を定義する別の構成ファイルですか?

ご協力ありがとうございました。

4

1 に答える 1

1

LDAP 認証後にユーザーに役割を割り当てる 2 つの方法を知っています。

  1. LDAP データベースでのロールに従ってユーザーを異なるグループに分割し、それらのグループを異なるロールにマップできます。

ここを参照してください:ADグループをユーザーロールSpring Security LDAPにマップする方法

  1. LDAP 認証を使用してユーザーを認証し、ローカル データベースを使用して承認できます。

構成:

    <beans:bean id="ldapAuthProvider"  class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
    <beans:constructor-arg name="authenticator">
        <beans:bean
            class="org.springframework.security.ldap.authentication.BindAuthenticator">
            <beans:constructor-arg ref="contextSource" />
            <beans:property name="userSearch">
                <beans:bean
                    class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
                    <beans:constructor-arg name="searchBase"
                        value="ou=example,dc=springframework,dc=org" />
                    <beans:constructor-arg name="searchFilter"
                        value="(uid={0})" />
                    <beans:constructor-arg name="contextSource"
                        ref="contextSource" />
                </beans:bean>
            </beans:property>
        </beans:bean>
    </beans:constructor-arg>
    <beans:constructor-arg name="authoritiesPopulator"
        ref="myLDAPAuthPopulator" />
</beans:bean>


<authentication-manager alias="authenticationManager">
<authentication-provider ref="ldapAuthProvider" />
</authentication-manager>

myLDAPAuthPopulatorの実装:

@Component("myLDAPAuthPopulator")
public class MyLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator   {

@Autowired
private UserDao userDao;

@Override
public Collection<? extends GrantedAuthority> getGrantedAuthorities(
    DirContextOperations userData, String username) {

List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

List<String> roleList = userDao.getRoles(username);
if (!roleList.isEmpty()) {
     for (String role : roleList) {
        System.out.println(role);
        authorities.add(new SimpleGrantedAuthority(role));
    }
}
else
{
 //We know that user is authenticated. So you can add a role here and save it in the database. 
}
return authorities;
}
于 2017-01-12T17:22:04.610 に答える