使用java Web应用程序中的现有ldap服务器对用户进行身份验证

时间:2015-12-19 08:13:16

标签: java tomcat authentication servlets ldap

我正在研究一个java Web应用程序,应该使用现有的ldap服务器对其进行身份验证。我不需要从服务器获取用户的信息。我只需要检查ldap中是否存在用户名和密码。我使用tomcat服务器作为Web应用程序。

这是我发现在互联网上搜索的内容,应该包含在server.xml文件中,并在tomcat服务器中包含相关参数。我在这篇特别指南中做了一切。

http://ldapwiki.willeke.com/wiki/Tomcat%20And%20LDAP

<Realm className="org.apache.catalina.realm.JNDIRealm" debug="10"
                                connectionURL="ldaps://192.168.0.8:636"
                                alternateURL="ldap://192.168.0.7:636"
                                userBase="ou=people,dc=willeke,dc=com"
                                userSearch="(cn={0})"
                                userSubtree="true"
                                userRoleName="dictcrole"
                                connectionName="cn=admin,ou=...,dc=willeke,dc=com"
                                connectionPassword="removed"
                /> 

我对xml和服务器不太了解。有人可以指导我用servlet做这个吗?

2 个答案:

答案 0 :(得分:2)

有几个图书馆

导入一个:在netbeans中:像那样: How to use .jar files in NetBeans?

您可以使用:unboundid sdk

进口:

ON_UPDATE_COMMAND_UI_RANGE(id1, id2, memberFxn)

你做了一个连接:

import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchScope;

您搜索用户名

  LDAPConnection ldap = new LDAPConnection("ldap.example.com", 389);

你得到了尊贵的名字:

    SearchResult sr = ldap.search("dc=People,dc=example,dc=com", SearchScope.SUB, "(uid=" + username + ")");
    if (sr.getEntryCount() == 0)
        System.out.println("KO");

然后,对于密码,一个解决方案:连接:

    String dn = sr.getSearchEntries().get(0).getDN();

还有javax.naming

看到这个: https://www.ldap.com/unboundid-ldap-sdk-for-java

https://code.google.com/p/ldap-sample-code/source/browse/trunk/src/main/java/samplecode/bind/SimpleBindExample.java

这也是: How do a LDAP search/authenticate against this LDAP in Java

答案 1 :(得分:0)

如果您在Tomcat上使用应用程序,则可以使用&#34; Realm Authentication&#34;作为您提供的链接。将工作。

您无需说必须将具体的实施细节应用于Realm Configuration,然后为应用程序声明Security Constraints

&#34;领域认证&#34;将不需要更改应用程序,也不会编写任何程序。

对于ONLY检查是否存在用户,这里是您可能想要开始的更一般的配置:(在您的WEB App web.xml中)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="tomcat-demo" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>test.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Wildcard means whole app requires authentication</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <!-- a * implies any user that can authenticate -->
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>

        <user-data-constraint>
            <!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
    </login-config>

</web-app>

当然,您再次需要修改您的具体实施。