在项目对应的容器里tomcat-users.xml文件当中添加角色及用户:
<role rolename="admin"/> <user username="zhangsan" password="123" roles="admin"/>
对应的web.xml中配置如下:
<security-constraint> <web-resource-collection> <web-resource-name>my auth</web-resource-name> <url-pattern>/login/*</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> <security-role> <role-name>admin</role-name> </security-role> <login-config> <auth-method>BASIC</auth-method> <realm-name>aaa</realm-name> </login-config>