posts - 2,  comments - 0,  trackbacks - 0

          TOMCAT 中的訪問控制策略

          TOMCAT的安全控制策略是根據(jù)Servlet 2.4規(guī)范來實現(xiàn)的。

          1.在$CATALINA/conf/server.xml文件中配置:

          <Realm className="org.apache.catalina.realm.UserDatabaseRealm"  debug="0" resourceName="UserDatabase"/>

          這里UserDatabase是一個jndi的名稱,也需要在server.xml中配置,對應(yīng)于$CATALINA/conf/tomcat-users.xml文件

          2.tomcat-users.xml文件里面定義了用戶和角色

          <?xml version='1.0' encoding='utf-8'?>
          <tomcat-users>
            <role rolename="tomcat"/>
            <role rolename="role1"/>
            <role rolename="manager"/>
            <role rolename="admin"/>
            <user username="tomcat" password="tomcat" roles="tomcat"/>
            <user username="both" password="tomcat" roles="tomcat,role1"/>
            <user username="manager" password="tomcat" roles="manager"/>
            <user username="admin" password="tomcat" roles="admin"/>
          </tomcat-users>

          3.在相應(yīng)的應(yīng)用的web.xml文件中加入<security-constraint><login-config> <security-role>標(biāo)簽,如下所示:
            <!-- Security is active on entire directory -->
            <security-constraint>
              <display-name>Tomcat Server Configuration Security Constraint</display-name>
              <web-resource-collection>
                <web-resource-name>Protected Area</web-resource-name>
                <!-- Define the context-relative URL(s) to be protected -->
                <url-pattern>/*</url-pattern>
              </web-resource-collection>
              <auth-constraint>
                <!-- Anyone with one of the listed roles may access this area -->
                <role-name>admin</role-name>
              </auth-constraint>
            </security-constraint>

            <!-- Login configuration uses form-based authentication -->
            <login-config>
              <auth-method>FORM</auth-method>
              <realm-name>Tomcat Server Configuration Form-Based Authentication Area</realm-name>
              <form-login-config>
                <form-login-page>/login.jsp</form-login-page>
                <form-error-page>/error.jsp</form-error-page>
              </form-login-config>
            </login-config>

            <!-- Security roles referenced by this web application -->
            <security-role>
              <description>
                The role that is required to log in to the Administration Application
              </description>
              <role-name>admin</role-name>
            </security-role>

          4.在 <login-config>標(biāo)簽的<auth-method>FORM</auth-method>屬性中,可以看到這里的authentication method 設(shè)置為FORM,這是一種基于表單的用戶認(rèn)證方式。基于form的用戶認(rèn)證需要在<form-login-page>/login.jsp</form-login-page>定義的登陸頁面中提供一個包括用戶名和密碼的html表單,這個表單相對應(yīng)于用戶名和密碼的元素必須是j_username和j_password,并且表單的ACTION必須為j_security_check。譬如: 
          <form method="POST" action="j_security_chack"> 
          <input type="text" name="j_username"> 
          <input type="password" name="j_password"> 
          </form> 

          在驗證通過之后,login頁面會自動轉(zhuǎn)向該應(yīng)用的默認(rèn)頁面(index.html,index.jsp等等)。

          除了FORM驗證方式之外,還有BASIC和CLIENT-CERT這兩種用戶認(rèn)證方式,前者是基本的用戶認(rèn)證方式,要求瀏覽器彈出一個對話框,錄入用戶名和密碼。后者是使用客戶數(shù)字證書來認(rèn)證請求。

          5.以上四步完成之后便可以通過在tomcat-users.xml文件中添加用戶和角色來實現(xiàn)訪問控制了。還是比較方面的。

          posted on 2008-12-26 17:17 sunnyiric 閱讀(1492) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 遵义县| 昌吉市| 古交市| 奉贤区| 深州市| 上蔡县| 望奎县| 湄潭县| 天气| 佛学| 深州市| 明星| 靖远县| 固阳县| 三河市| 鄂托克前旗| 南丰县| 辉县市| 潮安县| 郧西县| 婺源县| 琼中| 武宣县| 淅川县| 兖州市| 房产| 平罗县| 南雄市| 抚远县| 离岛区| 阳西县| 班戈县| 高邮市| 迭部县| 南漳县| 安陆市| 喜德县| 武功县| 苗栗市| 皋兰县| 中宁县|