sunyke 发表于 2017-1-29 12:29:26

使用Tomcat验证LDAP by openDJ

  在单位的应用系统,几乎全部都是使用LDAP来进行身份认证的。
所以,我也在家里自己配了一下,感觉还行。

首先,介绍一下什么是LDAP。
其实我完全不知道什么是LDAP,后来发现,其实就是个身份管理数据库,不过只对内部实现。
比如,我们使用FTP服务器的时候,需要输入账号密码,这个就可以绑定LDAP认证。
说白了,LDAP就是一个存储身份数据的数据库。
你登陆的系统只要和这个数据库连上了,就可以用同一个账号密码登陆了。(为啥不用Portal呢?)

首先,去这个地址下载openDJ,一个开源的LDAP服务器。
http://www.forgerock.org/opendj.html
然后,如果你和我一样就是使用windows,那么下载zip,点击setup.bat。

然后下一步下一步就可以了。
(。。。本来想搞点截图的,没这个功能啊。扔到附件上吧。)

选择这将是一台独立的服务器。


按照Example使用。

安装好以后,启动服务。也可以选择导入官方已有的配置文件ldif。


登陆后可以看到openDJ相关的基本信息和运行状态。

点击控制面板的“管理条目”,便可以打开创建时的基dn的管理界面。


新建一个组织单元(OrganizationUnit)People

在里面创建如下用户,密码自理。



dn: uid=bjensen,ou=People,dc=example,dc=com

uid: bjensen

cn: Barbara Jensen

cn: Babs Jensen

mail: bjensen@example.com

  
下面创建一个tomcat的分组,作为最终登陆的角色。
  在tomcat里面。找到conf文件夹。 
注释掉

<!--
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
-->
  还有这个

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
  然后插入

<Realm   className="org.apache.catalina.realm.JNDIRealm"
connectionURL="ldap://localhost:1389"
userPattern="uid={0},ou=people,dc=example,dc=com"
userRoleName="isMemberOf"
roleBase="ou=groups,dc=example,dc=com"
roleName="cn"
roleSearch="(uniqueMember={0})"
/>
  
至于以上字段有什么含义嘛。。

自行理解吧。。其实,在Apache Tomcat的网站上面有详细解释的。

英语不好?百度吧。。。这里略过。
  还有一个地方要更改的,就是tomcat-users.xml
这里就说一句话,把原本注释的部分,取消注释就是了。
  tomcat-users.xml对tomcat来说就是一种身份验证。
  
完成后,来准备用户登录和登录失败的文件:

我们这里假设让用户通过表单的方式通过验证,需要2 个 html 文件: login.html和 loginerr.html。
  


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Login to My Web Application</h1>
<p>
If you have been issued a username and password, key them in here now!
</p>
<form method="POST" action="j_security_check">
Username : <input type="text" size="15" maxlength="25" name="j_username"><br><br>
Password : <input type="password" size="15" maxlength="25" name="j_password"><br><br>
<input value="Login" type="submit">&nbsp;&nbsp;&nbsp;&nbsp;<input value="Clear" type="reset">
</form>
</body>
</html>
 还有错误页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Authentication Error!</title>
</head>
<body>
<h1>Authentication Error!</h1>
<p>
Oops! You either keyed in the wrong username or password.
</p>
<a href="javascript:history.back(1)">Try again ?</a>
</body>
</html>
  我自己登陆时使用的是
  账号:bjensen  《=在openDJ里面的用户名 uid
  密码:123456 《= 在openDJ服务里面输入的
  
对了,还有一点非常重要。
  就是在你的项目的web.xml里面的最后加入以下代码:
  


<security-constraint>
<web-resource-collection>
<web-resource-name>balancer</web-resource-name>
<description> accessible by authenticated users of the tomcat role</description>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<description>These roles are allowed access</description>
<role-name> tomcat </role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>YourWebApp Protected Area</realm-name>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/autherr.html</form-error-page>
</form-login-config>
</login-config>
<security-role>
<description>Only 'tomcat' role is allowed to access this web
application</description>
<role-name> tomcat </role-name>
</security-role>
  知道web.xml的人都知道 web.xml的配置格式是:
  <web-app>
  各种<context-param>
  各种<filter>和<filter-mapping>
  各种<listener>
  各种<servlet>和<servlet-mapping>
  <session-config>
  <welcome-list>
  各种<error-page>
  <tag-lib>
  <resource-ref>
  <security-constrain>
  <login-config>
  <security-role>
  </web-app>
  这样才能正确解析。
  说到这里,启动你的服务试试吧~~!!
页: [1]
查看完整版本: 使用Tomcat验证LDAP by openDJ