生活随笔
收集整理的這篇文章主要介紹了
在 Tomcat 中设置 JDBCRealm
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
除了默認配置的 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 ?
轉載于:https://www.cnblogs.com/huey/p/4515340.html
總結
以上是生活随笔為你收集整理的在 Tomcat 中设置 JDBCRealm的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。