2015-07-03 81 views
2

我创建使用弹簧MVC和angularjs的应用程序。对于身份验证,我已经在数据库中创建了一个表,并且我正在匹配用户输入数据库中的用户。但是现在我想使用LDAP进行身份验证。有人可以帮我解决如何使用angularjs进行LDAP身份验证的问题。我们如何使用Spring Security LDAP进行与angularjs认证作为客户端

在此先感谢。

+0

你检查有关LDAP身份验证 - http://docs.spring.io/spring-ldap/docs/current/reference/春天文档?甚至是本指南 - https://spring.io/guides/gs/authenticating-ldap/? – ingenious

回答

0

我使用LDAP身份验证与Spring MVC和我的应用程序AngularJs,这里是我想出...

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} 

然后我不得不创建一对夫妇的类来处理登录成功,sucess注销,用户映射等。

对于例如我的身份验证成功处理程序:

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控制器的方法来返回一些用户详细信息。

您可能还需要一个入口点来处理错误和UserContextMapper到LDAP中得到一些用户的详细信息。这是你如何去实现AuthenticationEntryPointLdapUserDetailsMapper

什么特别之处其他配置,只是简单的form-login和残疾人CSRF及其他安全头,因为我并不需要他们对我的应用程序。

请注意,从春季安全4.0.0你必须自己禁用这些标题,如果你不需要他们,他们becouse被默认启用,而在所有以前的版本,默认情况下,他们被禁用。

希望这会有所帮助。

+0

我一定要添加/使客户端的任何变化,使我的客户控制器能够知道它需要从写在服务器端的代码来检查?另外在我的应用程序中,我使用引导程序的模式服务登录页面,所以在我的应用程序/登录不会在网址中生成。是否需要在url中生成登录链接,或者我们可以使用相同的设置使用bootstrap的模式服务进行身份验证。 –

+0

是否有可能为你增添身份验证入口点代码和ldapuserdetailsmapper代码在这里有我在Java春季新品。 –

相关问题