1 , implement basic validation
(1) Add roles and users to the Tomcat-users.xml file under C:\jakarta-tomcat-5.0.19\conf (multiple users can be added at the same time)
<role rolename= "Users"/>
<user name= "Yang" password= "12345678" roles= "Users"/>
<user name= "Zhang" password= "12345678" roles= "Users"/>
(2) Add the following items to the Web. xml file
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Web-app
Public "-//sunmicrosystems, INC.//DTD Web application 2.3//en"
"Http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
.....
<security-constraint>
<web-resource-collection>
<web-resource-name>
Protected Resource
</web-resource-name>
<url-pattern>/BasicVerify/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>users</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Default</realm-name>
</login-config>
<security-role>
<description>this is a user</description>
<role-name>users</role-name>
</security-role>
.....
</web-app>
(3) Restart the Tomcat server
and enter the protected directory http://127.0.0.1:8080/WebMis/BasicVerify directly in the browser, the following login page appears
Enter user name: Yang (see the settings for the previous Tomcat-users.xml file)
Password: 12345678
The following will appear:
If there is an error in the user name or password, the input is forced.
Implementation of basic HTTP authentication in Tomcat