[英]Authenticating users using an exsiting ldap server in a java web application
我正在研究一个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做这个吗?
有几个图书馆
导入一个:在netbeans中:像那样: 如何在NetBeans中使用.jar文件?
您可以使用:unboundid sdk
进口:
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();
那么,对于密码,一个解决方案:连接:
ldap = new LDAPConnection("ldap.example.com", 389, dn, password);
还有javax.naming
如果您在Tomcat上使用应用程序,您可以使用“领域身份验证”作为您提供的链接。
您不用说必须将您的特定实现细节应用于Realm Configuration ,然后声明应用程序的安全约束 。
“领域身份验证”将不需要对应用程序进行任何更改,也不会编写任何程序。
对于仅检查用户是否存在,这是您可能想要开始的更一般的配置:(在您的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>
当然,您需要再次修改您的具体实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.