【发布时间】:2011-10-30 16:09:54
【问题描述】:
我们在我们的应用程序中使用 spring security 来验证来自 LDAP 的用户。认证部分工作正常,但授权部分不工作。
我们无法从 LDAP 中检索用户的角色。
摘自Peter Mularien所著的“Spring Security 3”一书
“这是因为 Active Directory 将组成员身份存储为 用户自己的 LDAP 条目。开箱即用(截至发布时), Spring Security 不提供 LdapAuthoritiesPopulator 配置为支持典型 Active Directory LDAP 树的结构。"
下面是我的 spring-security 配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http use-expressions="true" >
<intercept-url pattern="/resources/**" filters="none" />
<intercept-url pattern="/login" access="permitAll"/>
<intercept-url pattern="/**" access="isAuthenticated()" />
<form-login login-page="/login"
default-target-url="/home"
always-use-default-target="true"
authentication-failure-url="/login?login_error=1" />
<logout invalidate-session="true"
logout-success-url="/"
logout-url="/logout"/>
</http>
<authentication-manager alias="ldapAuthenticationManager">
<authentication-provider ref="ldapAuthenticationProvider"/>
</authentication-manager>
<beans:bean id="ldapAuthenticationProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
<beans:constructor-arg ref="ldapBindAuthenticator"/>
<beans:constructor-arg ref="ldapAuthoritiesPopulator"/>
<beans:property name="userDetailsContextMapper" ref="ldapUserDetailsContextMapper"/>
</beans:bean>
<beans:bean id="ldapServer" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<!-- MS Active Directory -->
<beans:constructor-arg value="ldap://localhost:389/dc=myOrg,dc=net"/>
<beans:property name="userDn" value="admin"/>
<beans:property name="password" value="admin"/>
<beans:property name="baseEnvironmentProperties">
<beans:map>
<beans:entry key="java.naming.referral" value="follow" />
</beans:map>
</beans:property>
</beans:bean>
<beans:bean id="ldapBindAuthenticator" class="org.springframework.security.ldap.authentication.BindAuthenticator">
<beans:constructor-arg ref="ldapServer"/>
<beans:property name="userSearch" ref="ldapSearchBean"/>
</beans:bean>
<beans:bean id="ldapSearchBean" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
<!-- MS Active Directory -->
<!-- user-search-base; relative to base of configured context source -->
<beans:constructor-arg value="ou=Software OU"/>
<!-- user-search-filter -->
<beans:constructor-arg value="(sAMAccountName={0})"/>
<beans:constructor-arg ref="ldapServer"/>
</beans:bean>
<beans:bean id="ldapAuthoritiesPopulator" class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
<beans:constructor-arg ref="ldapServer" />
<beans:constructor-arg value="" />
<beans:property name="groupSearchFilter" value="(sAMAccountName={0})"/>
<beans:property name="groupRoleAttribute" value="memberOf" />
<beans:property name="rolePrefix" value=""/>
<beans:property name="searchSubtree" value="true"/>
<beans:property name="convertToUpperCase" value="false"/>
<beans:property name="ignorePartialResultException" value="true"/>
</beans:bean>
<beans:bean class="org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper" id="ldapUserDetailsContextMapper"/>
</beans:beans>
请帮忙。
【问题讨论】:
-
您的代码中缺少什么?接受提供的解决方案/超链接是一回事,但指出缺失的部分对于帮助其他人(比如我)遇到完全相同的问题非常有用。感谢您分享详细的解决方案。
-
@CharlesMorin 我意识到我的回答低于标准,抱歉。为 AD 添加了我们的 Spring 配置。
-
@MarcelStör 谢谢。你用的是什么应用服务器?我试图在 JBoss AS 7.2 上进行同样的工作,但没有任何成功。将查看您的配置。
-
@CharlesMorin,我们使用 Tomcat。但是,我不明白为什么这会有所作为。容器不应该真正参与 Spring 和 AD 之间的通信。
-
@MarcelStör 是的,它有所不同,因为 JBoss 是一个 Java EE 容器,与 Tomcat 等标准 servlet 容器相比,它倾向于预先验证并增加一些其他复杂层。谢谢您的回答。我将尝试使其在 JBoss 上运行,然后在完成后在此处提供解决方案。
标签: ldap spring-security roles