天天看點

配置tomcat的jdbc安全域

 一:什麼是jdbc安全域?

    我的了解是:驗證資訊(角色、使用者名和密碼)通過jdbc橋梁到資料庫中去比對,如果驗證資訊正确,則讓使用者通路到受保護的頁面、資訊等!

二:實作步驟如下:---注:本文是将tomcat管理者的角色資訊存儲到資料庫中,進行驗證!

 1:資料庫采用sqlserver2005

 2:建立資料庫名為: zltomcat

 3:建立二張資料庫表名稱分别為:tomcat_user(使用者表)、tomat_user_role(角色表)

 4:tomcat_user表結構如下:

create table tomcat_user (      

  userName      varchar(15) not null primary key,

  pass          varchar(15) not null

);

   5:tomat_user_role表結構如下:

create table tomat_user_role (      
  userName         varchar(15) not null,      
  roleName         varchar(15) not null,      
  primary key (userName, roleName)      
);      

6:以上字段注釋如下:

 username:使用者名

    pass: 密碼

    roleName: 使用者的角色

7:打開~tomcat/conf/ server.xml檔案

8:在配置檔案Engine節點下添加如下配置檔案:

<Realm  className="org.apache.catalina.realm.JDBCRealm"

       driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://127.0.0.1:1433;databaseName=zltomcat"

                            connectionName="sa" connectionPassword="zl"

 userTable="tomcat_user" userNameCol="userName" userCredCol="pass"

 userRoleTable="tomat_user_role" roleNameCol="roleName"

/> 

 9:以上配置檔案重要參數注釋如下:

    driverName:驅動名,不同的資料庫不同

    databaseName:資料庫名

    connectionName:資料庫登陸名

    connectionPassword:資料庫登陸密碼

    userTable:使用者表名

    userNameCol:使用者名字段

    userCredCol:使用者密碼字段

    userRoleTable:角色表表名

    roleNameCol:角色字段(role)

10:将sqlserver2005的jdbc驅動包拷貝到tomcat的lib目錄下。

11:測試,啟動tomcat,在浏覽器位址欄輸入:http://localhost:8080/,點選:Tomcat Manager 輸入資料庫中設定的使用者名和密碼。登陸成功,進入到tomcat的管理者界面。

三:其他三種安全域介紹

   1:記憶體域(MemoryRealm),完全通過xml配置檔案完成, 對應資源通路權限有三種方式:BASIC、DIGEST、FORM。

   2: 資料源域(DataSourceRealm),通過JNDI資料源通路存在資料庫中的安全驗證資訊。

   3: JNDI域(JNDIRealm),通過JNDIproveider訪 問存放在基于LDAP的目錄伺服器中的安全驗證資訊。 

   4:以上更多的資訊請參考: http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html官方網站。 

轉載于:https://blog.51cto.com/itcome/930827