http://robblog.iteye.com/blog/554450


Tomcat配置: 
1 在tomcat的webapps下新建一个目录authen,再建立子目录subdir,下面放一个index.jsp 

2 在authen目录下建立WEB-INF目录,下放web.xml文件,内容如下 

Xml代码  收藏代码

  1. <security-constraint>  

  2.     <web-resource-collection>  

  3.         <web-resource-name>  

  4.             My App  

  5.         </web-resource-name>  

  6.         <url-pattern>/subdir/*</url-pattern>  

  7.     </web-resource-collection>  

  8.     <auth-constraint>  

  9.         <role-name>test</role-name>  

  10.     </auth-constraint>  

  11. </security-constraint>  

  12.   

  13. <login-config>  

  14.     <auth-method>BASIC</auth-method>  

  15.     <realm-name>My Realm</realm-name>  

  16. </login-config>  


3 在tomcat的tomcat-users.xml文件中添加一个用户名密码为test,test的用户,角色test。 


客户端访问: 
访问http://localhost:port/authen/subdir/index.jsp 
会弹出对话框提示认证,输入test test可以登录。 
d9bb95a9-5e35-304b-a8c1-b910a8b33a48.jpg