Tomcat配置使用数据进行HTTP Basic、Digest认证

总结&&疑问:

在Tomcat中配置摘要认证相对来说比较简单。但也需要注意一下几点:
1. 在使用JDBC Realm的时候,用户表和角色表的 用户名这一列的列名必须完全一致。
2. 在使用摘要认证的时候,参考tomcat的说明,貌似数据库中只能够存放明文(未测试)。
(Note that HTTP digest authentication is different from the storage of password digests in the repository for user information as discussed above).
3. 貌似无法为不同的路径设置不同的认证方式。
示例工程代码:

【Tomcat Basic、Digest认证】

HTTP FORM 认证的例子在Tomcat的发布包中自带的就有,参考:
apache-tomcat-6.0.29.zip/apache-tomcat-6.0.29/webapps/examples/jsp/security/protected/login.jsp

测试环境:

apache-tomcat-6.0.29
mysql-essential-5.1.50-win32.msi
eclipse 3.5

示例工程结构:

/test/src/auth.sql                    -- 在MySQL中创建schema、table、和数据
/test/WebContent/auth/index.html      -- 受保护的测试页面
/test/WebContent/META-INF/context.xml -- Tomcat中配置的Realm
/test/WebContent/WEB-INF/lib/mysql-connector-java-5.1.5-bin.jar
-- 使用Realm时连接数据库的jdbc jar包
/test/WebContent/WEB-INF/web.xml      -- web.xml

步骤:

1. 创建所需的表和数据:

auth.sql

  1. /* mysql tested. */
  2. create schema auth ;
  3. create table users (
  4.   user_name varchar(15) not null primary key,
  5.   user_pass varchar(15) not null
  6. );
  7. create table user_roles (
  8.   user_name varchar(15) not null,
  9.   role_name varchar(15) not null,
  10.   primary key (user_name, role_name)
  11. );
  12. insert into users (user_name, user_pass) values ( 'zhang3', '123456');
  13. insert into users (user_name, user_pass) values ( 'li4', '123456');
  14. insert into users (user_name, user_pass) values ( 'wang5', '123456');
  15. insert into user_roles (user_name, role_name) values ( 'zhang3', 'ADMIN');
  16. insert into user_roles (user_name, role_name) values ( 'zhang3', 'USER');
  17. insert into user_roles (user_name, role_name) values ( 'li4', 'USER');
  18. insert into user_roles (user_name, role_name) values ( 'wang5', 'GUEST');

2. 创建JDBC Realm

context.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Context>
  3.   <!-- digest="MD5" -->
  4.   <Realm className="org.apache.catalina.realm.JDBCRealm"
  5.          driverName="com.mysql.jdbc.Driver"
  6.          connectionURL="jdbc:mysql://localhost:3306/auth"
  7.          connectionName="root"
  8.          connectionPassword="123456"
  9.          userTable="users"
  10.          userNameCol="user_name"
  11.          userCredCol="user_pass"
  12.          userRoleTable="user_roles"
  13.          roleNameCol="role_name" />
  14. </Context>

3. 按需要修改 web.xml

web.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  5.   <!-- 设置需要认证的范围 -->
  6.   <security-constraint>
  7.     <display-name>Test Auth</display-name>
  8.     <web-resource-collection>
  9.       <web-resource-name>Protected Area</web-resource-name>
  10.       <url-pattern>/auth/*</url-pattern>
  11.       <http-method>DELETE</http-method>
  12.       <http-method>GET</http-method>
  13.       <http-method>POST</http-method>
  14.       <http-method>PUT</http-method>
  15.     </web-resource-collection>
  16.     <auth-constraint>
  17.       <role-name>ADMIN</role-name>
  18.       <role-name>USER</role-name>
  19.     </auth-constraint>
  20.   </security-constraint>
  21.   <!-- 设置该Web应用使用到的角色 -->
  22.   <security-role>
  23.     <role-name>ADMIN</role-name>
  24.   </security-role>
  25.   <security-role>
  26.     <role-name>USER</role-name>
  27.   </security-role>
  28.   <security-role>
  29.     <role-name>GUEST</role-name>
  30.   </security-role>
  31.   <!-- 设置认证方式 -->
  32.   <!--
  33.   <login-config>
  34.     <auth-method>BASIC</auth-method>
  35.     <realm-name>Basic Authentication</realm-name>
  36.   </login-config>
  37.    -->
  38.   <login-config>
  39.     <auth-method>DIGEST</auth-method>
  40.     <realm-name>Digest Authentication</realm-name>
  41.   </login-config>
  42. </web-app>

(可以分别启用不同的 login-config 进行basic或digest认证)

4. 在Tomcat中运行,在浏览器的URL中输入以下地址进行测试:

http://localhost:8080/test/auth/

用户 "zhang3", "li4" 可以登录,而 "wang5" 则不可以。(密码均为:"123456")


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。