私はすでに開発されたコードの上に構築しており、LDAP を介して認証するように求められましたが、これは実行できますが、Active Directory グループに基づいてアクセス許可を設定するように求められました。問題は、自分が持っているものをどのように使用して構築するかがわかりません。私はSpringの経験がまったくないので、[これ][1]を含むいくつかのチュートリアルを使用してActive Directoryをゼロから始めようとしましたが(しかし成功しませんでした)、ヘルプを[ここ][2]で探しました。 [ここ][3]しかし、成功しませんでした。まず、Spring 3.1 を使用できず、3.0 しか使用できず、上記の例のいずれも適応できませんでした。
既に持っているものから Active Directory グループ (およびその他の属性) を取得する方法はありますか?
これが私がこれまでに行ったことです:
spring-security.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/loginfailed" access="permitAll" />
<intercept-url pattern="/resources/images/**" access="permitAll" />
<intercept-url pattern="/resources/css/**" access="permitAll" />
<intercept-url pattern="/**" access="hasRole('CUSTOMADMIN')" />
<form-login login-page="/login" default-target-url="/index" authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>
<ldap-server id="ldapServer" url="ldap://url:portnumber/ou=People,dc=abc,dc=com" manager-dn="dn" manager-password="password" />
<authentication-manager>
<authentication-provider ref="ldapAuthProvider" />
</authentication-manager>
<beans:bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
<beans:constructor-arg>
<beans:bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
<beans:constructor-arg ref="ldapServer" />
<beans:property name="userDnPatterns">
<beans:list>
<beans:value>uid={0}</beans:value>
</beans:list>
</beans:property>
</beans:bean>
</beans:constructor-arg>
<beans:constructor-arg>
<beans:bean class="com.company.group.appname.ldap.RolesPopulator">
<beans:constructor-arg ref="userRoleService" />
</beans:bean>
</beans:constructor-arg>
</beans:beans>
RolesPopulator.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;
import com.company.group.appname.service.IUserRoleService;
public class RolesPopulator implements LdapAuthoritiesPopulator
{
private static Logger log = Logger.getLogger(RolesPopulator.class);
@Autowired
private IUserRoleService userRoleService;
public RolesPopulator(IUserRoleService userRoleService)
{
this.userRoleService = userRoleService;
}
@Override
public Collection<GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username)
{
List<GrantedAuthority> userPerms = new ArrayList<GrantedAuthority>();
log.debug("UserPermsions: "+userPerms.toString());
//get users permissions from service
List<String> userRoles = userRoleService.getPermissions(username);
for (String string : userRoles)
{
userPerms.add(new GrantedAuthorityImpl(string));
}
return userPerms;
}
}
UserRoleServiceImpl.java (IUserRoleService の実装)
package com.company.group.appname.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import com.company.group.appname.service.IUserRoleService;
@Service("userRoleService")
public class UserRoleServiceImpl implements IUserRoleService {
private static Logger log = Logger.getLogger(UserRoleServiceImpl.class);
public List<String> getPermissions(String username) {
List<String> roles = new ArrayList<String>();
roles.add("CUSTOMADMIN");
return roles;
}
}
これはすべて正常に認証されますが、ここから Active Directory グループを取得する方法がわかりません。私ができるようにしたいのはgetPermissions(username)
、Active Directory からユーザー名に関連付けられたグループのリストを取得できるようにしたいメソッドからです。特定のグループ名が含まれている場合はロールを返し、それ以外の場合は null (または他のロール) を返します。 )。
正直に言うと、Active Directory を介して認証を行い、グループを取得できる多くのコード例を見てきましたが、それらのいずれも動作させることができませんでした (見つけた例のほとんどは、 spring security 3.1、これは残念ながらオプションではありません)そして、この方法でアプローチする例も見つけていません。
どんなガイダンスや助けも素晴らしいでしょう