Set Jdbcrealm in Tomcat

Source: Internet
Author: User

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:

  1. 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.

  2. Place the JDBC driver jar file in the $TOMCAT _home\lib directory.
  3. 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;
  4. 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');
  5. Create a new Java Web project and edit the. xml file.
  6. 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> 
  7. 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>
  8. 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  >  
  9. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.