我已经使用spring-mvc和angularjs创建了一个应用程序。对于身份验证,我在数据库中创建了一个表,并将用户输入与数据库中的用户进行匹配。但是现在我想使用LDAP进行身份验证。有人能帮我解决如何用angularjs做LDAP认证的问题吗?
提前谢谢。
发布于 2015-07-03 21:36:36
我在我的应用程序中使用了带有Spring MVC和AngularJs的LDAP身份验证,这是我想出来的……
securityContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd">
<bean id="contextSource"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="anonymousReadOnly" value="false"/>
<property name="password" value="${ldap.password}"/>
<property name="pooled" value="false"/>
<property name="userDn" value="${ldap.userdn}"/>
<property name="base" value="${ldap.base}"/>
<property name="referral" value="follow"/>
<property name="urls">
<list>
<value>${ldap.url}</value>
</list>
</property>
</bean>
<bean id="entryPoint" class="com.company.ldap.UserAuthenticationEntryPoint"/>
<bean id="logoutSuccessHandler" class="com.company.ldap.LdapLogoutSuccessHandler"/>
<bean id="userDetailsContextMapper" class="com.company.ldap.LdapUserDetailsContextMapper"/>
<bean id="authenticationSuccessHandler" class="com.company.ldap.LdapAuthenticationSuccessHandler"/>
<bean id="authenticationFailureHandler" class="com.company.ldap.LdapAuthenticationFailureHandler"/>
<sec:http use-expressions="true"
auto-config="false"
create-session="never"
entry-point-ref="entryPoint"
authentication-manager-ref="authenticationManager">
<sec:form-login
login-processing-url="/login"
authentication-success-handler-ref="authenticationSuccessHandler"
authentication-failure-handler-ref="authenticationFailureHandler"
username-parameter="username"
password-parameter="password"
login-page="/"/>
<sec:intercept-url pattern="/" access="permitAll"/>
<sec:intercept-url pattern="/secure/**" access="isAuthenticated()"/>
<sec:logout invalidate-session="true"
delete-cookies="JSESSIONID"
logout-url="/secure/logout"
success-handler-ref="logoutSuccessHandler"/>
<sec:csrf disabled="true"/>
<sec:headers disabled="true"/>
</sec:http>
<sec:authentication-manager id="authenticationManager">
<sec:ldap-authentication-provider user-search-filter="${ldap.search.filter}"
user-context-mapper-ref="userDetailsContextMapper"/>
</sec:authentication-manager>
</beans>首先,您需要定义LdapContextSource并设置所有批准的值,例如,我的如下所示:
ldap.url=ldap://ldap-host:389/
ldap.userdn=cn=some-cn,cn=Users,dc=dcname,dc=net
ldap.password=*******
ldap.base=ou=Users,ou=City,ou=Company,dc=dcname,dc=net
ldap.search.filter=sAMAccountName={0}然后我必须创建几个类来处理成功登录、成功注销、用户映射等。
例如,对于我的身份验证成功处理程序:
public class LdapAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/secure/user");
dispatcher.forward(request, response);
}
}在身份验证成功后,它只将请求转发到"/secure/user"。在这里,我已经为该路径配置了Spring控制器的方法,以返回一些用户详细信息。
您可能还需要一个EntryPoint来处理错误,并需要一个UserContextMapper来从Ldap获取一些用户详细信息。这取决于您如何实现AuthenticationEntryPoint和LdapUserDetailsMapper
其他配置没有什么特别之处,只是简单的form-login和禁用了CSRF和其他安全头文件,因为我的应用程序不需要它们。
请注意,从Spring Security 4.0.0开始,如果您不需要这些头文件,您必须自己禁用它们,因为它们默认情况下是启用的,而在所有以前的版本中,默认情况下它们都是禁用的。
希望这能有所帮助。
https://stackoverflow.com/questions/31207648
复制相似问题