转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html

接上例:namespce的作用除了在前面提到的避免协同开发名字冲突外,还为认证提供一个条件。比如jack开发的东西所关联到的页面需要权限才能被访问。由于多为tomcat中的内容,下面只列出步骤。
步骤一,tomcat的conf目录下tomcat-users.xml内容如下:
<?xml version='1.0' encoding='utf-8'?>

<tomcat-users>

  <role rolename="manager"/>

  <role rolename="admin"/>

  <user username="jack" password="jack" roles="admin,manager"/>

  <user username="tom" password="tom" roles="manager"/>

</tomcat-users>
步骤二,在web.xml中增加如下内容:
<security-constraint>

              <web-resource-collection>

                     <web-resource-name>jack</web-resource-name>

                     <url-pattern>/jack/*</url-pattern>

                     <http-method>POST</http-method>

                     <http-method>GET</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>input authentication message</realm-name>

       </login-config>

这样配置完成后,当我们访问.../jack中的任何内容都会要求输入密码认证信息,认证时输入tomcat-users.xml配置的admin权限的用户名和密码即可访问(这里就只有jack用户名可以访问)

posted on 2016-06-13 14:39  Sharpest  阅读(150)  评论(0编辑  收藏  举报