In addition to the default configuration, the Datasourcerealm,tomcat also supports Jdbcrealm, which accesses the authentication information recorded in the relational database through JDBC.
The configuration steps for Jdbcrealm are as follows:
- In the $TOMCAT _home\conf\server.xml configuration <Reaml/> elements.
<RealmClassName= "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/> Element Property Description:
| Properties |
description |
| classname |
tomcat Jdbcrealm implementation class |
| drivername |
&NBSP;JDBC driver class |
| connectionurl |
database connection address |
| connectionname |
Database login user |
| connectionpassword& nbsp; |
Database login password |
| usertable |
table name for user table |
| usernamecol |
column names for user columns in user tables |
| usercredcol |
column names for password columns in user tables |
| userroletable |
table name for role table |
| rolenamecol |
role columns in the Roles table |
Note:<realm/> elements can be placed in the <Engine/> element, and the Realm will be shared by all apps. Placed in the <Host/> element, it is shared by the application under that Host. Placed in the <Context/> element, only the corresponding application can be accessed.
- Place the JDBC driver jar file in the $TOMCAT _home\lib directory.
- Create a user table and a role table in the database, and the table name and name are consistent with the configuration described above.
CREATE TABLE' users ' (' username ' )varchar( +) not NULL, ' Userpass 'varchar( +) not NULL, PRIMARY KEY(' username ')) ENGINE=InnoDBDEFAULTCHARSET=UTF8;CREATE TABLE' roles ' (' username ' )varchar( +) not NULL, ' userrole 'varchar( +) not NULL, PRIMARY KEY(' username ', ' userrole ')) ENGINE=InnoDBDEFAULTCHARSET=UTF8;
- Configure user and role information in the table.
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');
- Create a new Java Web project and edit the. xml file.
- Configuration <security-role/> elements to define roles.
<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>
- Configuration <security-constraint/> elements that specify the set of resources accessible to the role and the HTTP methods that can be used.
<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>
- configuration <login-config/> elements, specify the authentication method as Basic authentication, and specify the security domain.
< login-config > < auth-method > basic</ auth-method Span style= "color: #0000ff;" >> < realm-name > hueyhome</ realm-name Span style= "color: #0000ff;" >> </ login-config >
- Test.
c:\users\huey>curl-i-U "suer:suer" http://localhost:8080/helloweb/blog/index.htmlhttp/1.1 OK Server:apache-coyote/1.1pragma:no-cachecache-control:no-cacheexpires:thu, Jan 1970 08:00:00 cstaccept-ranges:bytesetag:w/"261-1431758220107" Last-modified:sat, 06:37:00 gmtcontent-type:text/ Htmlcontent-length:261date:tue, 11:44:20 GMT
Set Jdbcrealm in Tomcat