Cấu hình JDBC Realm cho Tomcat
Trong bài này, tôi sẽ hướng dẫn từng bước cấu hình JDBC Realm thay vì authen qua tomcat-users.xml. Cơ sở dữ liệu ở đây tôi dùng là Microsoft SQL server.
1. Đầu tiên, bạn tạo 1 database với tên bất kỳ (chẳng hạn: SecurityRealm).
2. Tạo các bảng như sau:
Dữ liệu thử như sau:
3. Cấu hình tomcat:
Trong thư mục %TOMCAT_HOME%\conf, mở tập tin server.xml (nên dùng notepad++).
Thêm phân đoạn xml sau vào:
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"connectionURL="jdbc:sqlserver://localhost:1433;databaseName=SecurityRealm"connectionName="sa" connectionPassword=""userTable="users" userNameCol="user_name" userCredCol="user_pass"userRoleTable="user_roles" roleNameCol="role_name"/>Chú ý: thuộc tính connectionName là tên đăng nhậpp vào cơ sở dữ liệu sql của bạn và password cũng vậy. Sau đây là danh sách các thuộc tính khả dĩ.
|
attribute |
Meaning |
|
driverName |
The name of the driver needed to connect to the database |
|
connectionURL |
The connection URL used to connect to the database |
|
userTable |
The user's tables |
|
userNameCol |
The column in the user's table that contains the name |
|
userCredCol |
The column in the user's table that contains the password |
|
userRoleTable |
The user's roles table |
|
roleNameCol |
The column in the user's table that contains a role given to a user |
|
connectionName |
The name to use when connecting to the database. (Optional) |
|
connectionPassword |
The password to use when connecting to the database. (Optional) |
|
digest |
The algorithm used for digest passwords or "No" for plain passwords, the values can be "MD5", "MD2", "SHA", etc... (Optional) |
4. Copy driver của SQL server(sqljdbc.jar) vào thư mục %TOMCAT_HOM%\lib.
OK. Bây giờ bạn secure ung7n1g dụng của bạn. Thay vì bạn dùng username-password trong file tomcat-users.xml, bạn dùng username-password trong bảng user của cơ sở dữ liệu .
Chúc thành công!