1 回答
TA贡献1851条经验 获得超3个赞
最后,我发现以下配置运行良好。
下面<bean id="ldapAuthProvider"> 构造两个参数,
第一个参数:它将验证最终用户在前端填写的用户名和密码。
第二个参数:一旦成功通过验证器,它将调用我们自己的填充器 ( <bean class="com.my.own.util.MyCustomLdapAuthPopulator">) 来分配适当的角色或做其他你想做的事情。
<sec:authentication-manager>
<sec:authentication-provider
ref="ldapAuthProvider"
>
</sec:authentication-provider>
</sec:authentication-manager>
<bean id="ldapAuthProvider"
class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider" >
<constructor-arg>
<bean id="authenticator"
class="org.springframework.security.ldap.authentication.BindAuthenticator">
<constructor-arg ref="contextSource" />
<property name="userSearch">
<bean
class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
<constructor-arg value="dc=aaa,dc=bbb,dc=ccc" />
<constructor-arg value="(sAMAccountName={0})" />
<constructor-arg ref="contextSource" />
</bean>
</property>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="com.my.own.util.MyCustomLdapAuthPopulator">
<constructor-arg ref="contextSource" />
<constructor-arg value="dc=aaa,dc=bbb,dc=ccc" />
<property name="searchSubtree" value="true" />
<property name="ignorePartialResultException" value="true" />
<property name="groupSearchFilter" value="(member={0})" />
</bean>
</constructor-arg>
</bean>
<bean id="contextSource"
class="org.springframework.security.ldap.DefaultSpringSecurityContextSource" >
<constructor-arg value="ldap://xx.xx.xx.xx:389/" />
<property name="userDn" value="domain\user1" />
<property name="password" value="user1_password" />
</bean>
下面是我们自己的填充器的一个简单实现。
import org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator;
public class MyCustomLdapAuthPopulator extends DefaultLdapAuthoritiesPopulator {
@Autowired
public MyCustomLdapAuthPopulator(ContextSource contextSource, String groupSearchBase) {
super(contextSource, groupSearchBase);
// TODO Auto-generated constructor stub
}
@Override
protected Set<GrantedAuthority> getAdditionalRoles(DirContextOperations user, String username) {
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
authorities.add((new SimpleGrantedAuthority("ROLE_XXX")));
return authorities;
}
}
添加回答
举报