gpt4 book ai didi

spring-mvc - LDAP - 上下文源的 AD 用户名密码配置

转载 作者:行者123 更新时间:2023-12-04 01:29:56 30 4
gpt4 key购买 nike

我正在使用 Spring Security 通过 LDAP 协议(protocol)对 Active Directory 进行身份验证。以下代码也适用于身份验证和设置 LDAP 模板 (springSecurity.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/ldap
http://www.springframework.org/schema/ldap/spring-ldap.xsd">

<http use-expressions="true">

<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/authenticated" access="isAuthenticated()" />

<form-login login-page="/login" default-target-url="/authenticated"
authentication-failure-url="/login?error=true" />
<logout />


</http>

<beans:bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location">
<beans:value>classpath:/ldap.properties</beans:value>
</beans:property>
<beans:property name="SystemPropertiesMode">
<beans:value>2</beans:value> <!-- OVERRIDE is 2 -->
</beans:property>
</beans:bean>

<beans:bean id="adAuthenticationProvider"
class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
<beans:constructor-arg value="${sample.ldap.domain}" />
<beans:constructor-arg value="${sample.ldap.url}" />
<beans:property name="useAuthenticationRequestCredentials"
value="true" />
<beans:property name="convertSubErrorCodesToExceptions"
value="true" />
</beans:bean>


<authentication-manager>
<authentication-provider ref="adAuthenticationProvider" />
</authentication-manager>

<!-- Ldap after authentication -->

<context:property-placeholder location="classpath:/ldap.properties"
system-properties-mode="OVERRIDE" />
<context:annotation-config />

<ldap:context-source id="contextSource"
password="${sample.ldap.password}"
url="${sample.ldap.url}"
username="${sample.ldap.userDn}"
base="${sample.ldap.base}"
referral="follow" />

<ldap:ldap-template id="ldapTemplate"
context-source-ref="contextSource" />

<ldap:repositories base-package="com.domain" />

<beans:bean class="com.service.UserService">
<beans:property name="directoryType" value="${sample.ldap.directory.type}" />
</beans:bean>

<!-- Required to make sure BaseLdapName is populated in UserService -->
<beans:bean
class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor" />

</beans:beans>

从 login.jsp 获取 j_username 和 j_password 时身份验证工作正常。要设置 ldap 模板,我使用属性文件中定义的用户名和密码属性,但我希望使用来自 spring security 的相同用户名和密码。请指导我如何将 ldap:context-source id="contextSource" 中的用户名和密码属性属性绑定(bind)到 spring 安全凭证。

代码有点乱,欢迎任何改进意见。

最佳答案

Configuration chapter 中所述在引用文档中,您可以通过在 ContextSource 的配置元素中指定自定义 authentication-source-ref 来为 ContextSource 使用 Spring Security 身份验证。在您的情况下,您将使用 Spring Security 附带的 SpringSecurityAuthenticationSource

<ldap:context-source id="contextSource" 
url="${sample.ldap.url}"
base="${sample.ldap.base}"
referral="follow"
authentication-source-ref="authenticationSource"/>

<bean id="authenticationSource"
class="org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource" />

关于spring-mvc - LDAP - 上下文源的 AD 用户名密码配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195967/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com