1.在<tomcat_home>/conf目录下的tomcat-users.xml文件中添加如下内容:
<role rolename="admin"/>
<user username="yangjing" password="test@123" roles="admin"/>
备注:rolename,username,password可以自定义
2.新建自己的工程文件,我这边新建basic和form代表各自认证服务工程文件夹,准备好带有相关认证的html,jsp等web源码放在对应的路径下:
在工程文件下新建WEB-INF目录,里面存放web.xml文件用来初始化配置信息
basic认证的web.xml文件如下:
<web-app>
<security-constraint>
<web-resource-collection>
<web-resource-name>
Member Area
</web-resource-name>
<description>
Only registered members can access this area.
</description>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
</web-app>
form的web.xml文件如下:
<web-app>
<security-constraint>
<web-resource-collection>
<web-resource-name>
Member Area
</web-resource-name>
<description>
Only registered members can access this area.
</description>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login/login.html
</form-login-page>
<form-error-page>/login/error.html
</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
</web-app>
备注:
A. role-name为自己在tomcat-users.xml新建的角色
B./*----控制访问路径
C.节点控制认证方式
这些准备工作做完后,重启tmocat服务,接下来我们验证下是否搭建成功:、
basic认证:
FROM认证:
以上验证成功,今日もやる気まんまんだよ!!!