![在 Tomcat 中设置 JDBCRealm 在 Tomcat 中设置 JDBCRealm](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
除了默认配置的 DataSourceRealm,Tomcat 还支持 JDBCRealm,它通过 JDBC 来访问记录在关系数据库里的认证信息。
JDBCRealm 的配置步骤如下:
- 在 $TOMCAT_HOME\conf\server.xml 配置 <Reaml/> 元素。
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/tomcat"
connectionName="root" connectionPassword="root"
userTable="users" userNameCol="username" userCredCol="userpass"
userRoleTable="roles" roleNameCol="userrole" /><Reaml /> 元素属性说明:
属性 说明 className Tomcat 的 JDBCRealm 实现类 driverName JDBC 驱动类 connectionURL 数据库连接地址 connectionName 数据库登录用户 connectionPassword 数据库登录密码 userTable 用户表的表名 userNameCol 用户表中用户列的列名 userCredCol 用户表中密码列的列名 userRoleTable 角色表的表名 roleNameCol 角色表中的角色列 注:<Realm/> 元素可以放在 <Engine/> 元素中,这时该 Realm 会被所有应用共享。 放在 <Host/> 元素中,会被该 Host 下的应用程序共享。放在 <Context/> 元素中,则只有对应的应用程序能被访问。
- 将 JDBC 驱动 jar 文件放置在 $TOMCAT_HOME\lib 目录中。
- 在数据库中创建用户表与角色表,表名和命名要与上述的配置一致。
CREATE TABLE `users` (
`username` varchar(32) NOT NULL,
`userpass` varchar(32) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `roles` (
`username` varchar(32) NOT NULL,
`userrole` varchar(32) NOT NULL,
PRIMARY KEY (`username`,`userrole`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; - 在表中配置用户与角色信息。
INSERT INTO `tomcat`.`users` (`username`, `userpass`) VALUES ('admin', 'admin');
INSERT INTO `tomcat`.`users` (`username`, `userpass`) VALUES ('huey', 'huey');
INSERT INTO `tomcat`.`users` (`username`, `userpass`) VALUES ('suer', 'suer'); INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('admin', 'admin');
INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('admin', 'common');
INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('huey', 'common');
INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('suer', 'common');
INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('suer', 'vip'); - 新建一个 Java Web 工程,编辑 web.xml 文件。
- 配置 <security-role/> 元素来定义角色。
<security-role>
<role-name>admin</role-name>
</security-role>
<security-role>
<role-name>common</role-name>
</security-role>
<security-role>
<role-name>vip</role-name>
</security-role> - 配置 <security-constraint/> 元素,指定角色可访问的资源集和可使用的 HTTP 方法。
<security-constraint>
<web-resource-collection>
<web-resource-name>Public resources</web-resource-name>
<url-pattern>/home/*</url-pattern>
<http-method>HEAD</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>common</role-name>
</auth-constraint>
</security-constraint> <security-constraint>
<web-resource-collection>
<web-resource-name>Secret resources</web-resource-name>
<url-pattern>/blog/*</url-pattern>
<url-pattern>/photo/*</url-pattern>
<http-method>HEAD</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
<role-name>vip</role-name>
</auth-constraint>
</security-constraint> - 配置 <login-config/> 元素,指定认证方式为基本认证,并指定安全域。
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>hueyhome</realm-name>
</login-config> - 测试。
C:\Users\huey>curl -I -u "suer:suer" http://localhost:8080/helloweb/blog/index.html
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 08:00:00 CST
Accept-Ranges: bytes
ETag: W/"261-1431758220107"
Last-Modified: Sat, 16 May 2015 06:37:00 GMT
Content-Type: text/html
Content-Length: 261
Date: Tue, 19 May 2015 11:44:20 GMT