在Tomcat5.5中使用 LDAP 进行用户认证
在配置 Tomcat 之前需要先把 LDAP 服务器安装并设置好,并创建一些组和用户。
编辑 Tomcat 服务器配置文件 (server.xml)
1. 注释掉 UserDatabase 相关设置
a) 找到 Tomcat 服务器配置文件 server.xml并将其备份,通常这个文件位于$CATALINA_HOME/conf 目录下,其中 $CATALINA_HOME 为 Tomcat 的安装目录。
b) 用文本编辑器打开这个文件,并找到以下部分
<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" />
c) 将其注释掉:
<!--
<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" />
-->
d) 然后再找到对 UserDatabase 的 Realm 设置
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
e) 按照上面的方法将其注释(在前面加 <!--,在后面加 -->)。保存文件。
2. 添加 JNDI Realm 信息
a) 在 server.xml 文件中找到如下文本:
<!-- Replace the above Realm with one of the following to get a Realm
stored in a database and accessed via JDBC -->
b) 在后面加上如下文本
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionURL="ldap://localhost:389"
connectionName="cn=Directory Manager"
connectionPassword="secret"
userPattern="uid={0},ou=People,dc=example,dc=com"
userPassword="userPassword"
roleBase="ou=Groups,dc= example,dc= com "
roleName="cn"
roleSubtree="false"
roleSearch="(uniqueMember={0})"
/>
| 参数名 | Realm 属性 | 参数值 | 注释 |
| LDAP 链接 URL | connectionURL | ldap://localhost:389 | LDAP服务器和 Tomcat 在同一台机器上。389 为 LDAP服务器的端口 |
| LDAP 绑定用户 | connectionName | cn=Directory Manager | Sun Directory Server 的目录管理员 |
| 绑定用户密码 | connectionPassword | Secret |
|
| 用户搜索模板 | userPattern | uid={0},ou=People,dc=example,dc=com |
|
| 用户密码 | userPassword | userPassword | 用户登录时的密码, 为 LDAP 中条目的 userPassword 属性 |
| 角色搜索路径 | roleBase | ou=Groups,dc= example,dc= com |
|
| 角色属性 | roleSearch | (uniqueMember={0}) | 对应角色中的成员 |
| 搜索角色子目录 | roleSubtree | False | 可以不设定 |
准备用户登录和登录失败的文件
我们这里假设让用户通过表单的方式通过验证,需要2 个 html 文件: login.html和 loginerr.html。
创建登录页面文件 login.html
将下面的文本保存为 login.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"> <input value="Clear" type="reset">
</form>
</body>
</html>
请注意表单的内容。表单提交方式为 POST,提交的 action 为 j_security_check,用户名字段为 j_username,密码字段为 j_password。这些字段必须在这个表单中出现。
创建出错页面文件 loginerr.html
将下面的文本保存为 loginerr.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>
在 LDAP 服务器中创建必要的组和用户
假设我们在后面的 web 应用中需要一个 tomcat 组,其中一个用户为 tomcat。在 Sun Directory Server 管理控制台中依次创建一个 tomcat 用户和一个 tomcat 组,并将此用户添加到 tomcat 组中。
创建 LDAP 用户
1. 展开 dc=example, dc=com 文件夹,在展开的项目中选择 People 并单击鼠标右键
2. 选择 New à User…

3. 在 Create New User 窗口中填写 tomcat 用户信息,假设用户名和密码均为 tomcat

创建 LDAP 组
1. 选中 Groups 并单击鼠标右键
2. 选择 New à Group

3. 在 Group Name 字段中输入 tomcat

4. 在左侧选择 Members,在随后出现的界面中选择 Static Group 选项卡
5. 单击 Add… 按钮
6. 在 for 字段中输入 tomcat并单击 Search

7. 选择 tomcat 用户并单击 OK 关闭搜索窗口。Tomcat 用户会出现在 Static Group 中

8. 单击 OK 关闭 Crete New Group 窗口。
在现有组中添加或删除用户和以上操作相似。
编辑 Web 应用的配置文件 web.xml
为了练习以及验证 LDAP 设置,假设我们需要保护的内容为:
l 此 web 应用中的所有内容
l 任何访问方式都需要事先登录(HTTP GET, PUT, POST, DELETE)
权限:只有角色为 tomcat 的用户才可以访问此应用
为了简单起见,可以将以下内容放在需要保护的 web 应用的 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>
测试 Tomcat LDAP 的配置
编辑应用的 web.xml 文件
Tomcat 5.5.25 的默认安装中有一个 balancer 的应用,我们需要对这个应用的访问进行权限控制。权限控制通过编辑 balancer 应用的 web.xml 实现。将以下文本加在 web.xml 的最后部分,即 </web-app> 之前:
<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>
测试对受控资源的访问
Balancer 的 web.xml 编辑完成后,启动 tomcat。在浏览器中访问 balancer 应用(http://localhost:8080/balancer/)将会先进入登录页面,只有符合 web.xml 中权限设置的用户才能成功登录。

参考资源
l http://cymulacrum.net/writings/adv_tomcat/c302.html (Tomcat and LDAP Directories)
l http://cymulacrum.net/writings/adv_tomcat/c487.html (Tomcat JNDI Realm)
l http://tomcat.apache.org/tomcat-5.5-doc/realm-howto.html#JNDIRealm
l
本文详细介绍了如何在Tomcat 5.5中配置使用LDAP进行用户认证,包括编辑server.xml文件,创建登录及错误页面,设置LDAP服务器中的用户和组,以及在Web应用的web.xml中进行权限配置。通过这些步骤,可以实现只有拥有特定角色的用户才能访问受保护的应用。
2211

被折叠的 条评论
为什么被折叠?



