Tomcat uses JDBC Realm to configure Basic authentication

Source: Internet
Author: User

1. Download the MySQL JDBC driver (mysql-connector-java-x.x.x-bin.jar) into the Tomcat_home/lib directory.


2. Configure Tomcat_home/server.xml, add Jdbcrealm, comment out the original lockoutrealm.

<realm classname= "Org.apache.catalina.realm.JDBCRealm" drivername= "Org.gjt.mm.mysql.Driver" connectionurl= " jdbc:mysql://127.0.0.1:3306/authority?user=root&amp;password=123456 "usertable=" Users "userNameCol=" User_ Name "usercredcol=" User_pass "userroletable=" Roles "rolenamecol=" Role_name "/><!--<realm classname="               Org.apache.catalina.realm.LockOutRealm "> <realm classname=" Org.apache.catalina.realm.UserDatabaseRealm " Resourcename= "Userdatabase"/> </Realm>-->


3. Connecting to a database , create Baisc to access user tables and user role tables, and insert data.

create table  ' users '   (   ' user_id '  int (one)  not null auto_increment ,   ' user_name '  varchar ( NOT NULL,   ' User_pass '  varchar ( not)  NULL,   ' User_mark '  varchar (255)  DEFAULT  ',  primary key  (' user_id '))  ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; create table  ' roles '   (   ' role_id '  int (one)  not null auto_increment ,   ' Role_name '  varchar ( NOT NULL,   ' user_name '  varchar ( not)  NULL,  PRIMARY KEY  (' role_id ')  ENGINE=InnoDB AUTO_INCREMENT=3  default charset=utf8;insert into  ' authority '. ' Users ' (' user_id ', ' user_name ', ' user_pass ', ' User_mark ')  VALUES  (1,  ' admin ',  ' password ',   ');insert into  ' authority '. ' Users ' (' user_id ', ' user_name ', ' user_pass ', ' User_mark ')  VALUES  (2,  ' guest ',  ' password ',  ');insert into  ' authority '. ' Roles ' (' role_id ', ' role_name ', ' user_name ')  VALUES  (1, ' admin ', ' admin ');insert into  ' Authority '. ' Roles ' (' role_id ', ' role_name ', ' user_name ')  VALUES  (2, ' guest ', ' guest ');


4. Add the following under the Web-inf/web.xml web-app node of Web project

  <security-constraint>        < web-resource-collection>            < web-resource-name>web project name</web-resource-name>             <url-pattern>/*</url-pattern>         </web-resource-collection>        < Auth-constraint>            <role-name> admin</role-name>        </auth-constraint>     </security-constraint>    <login-config>         <auth-method>BASIC</auth-method>         <realm-name>favourite basic realm</realm-name>    </login-config>    <security-role>         <role-name>admin</role-name>     </security-role>


5. Launch Tomcat, enter the application's access address, and the user login input box will be displayed, and the correct user name and password will be used to access the protected resource.


It is important to note that login is only required for the first time that a protected resource is accessed because the relevant information is placed in the Tomcat server's cache after the user logs in, and the authentication information persists until the user closes the browser or clears the cache, and the user does not need to log in again.


This article is from the "This person's IT World" blog, be sure to keep this source http://favccxx.blog.51cto.com/2890523/1877588

Tomcat uses JDBC Realm to configure Basic authentication

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.