weidagang2046的專欄

          物格而后知致
          隨筆 - 8, 文章 - 409, 評(píng)論 - 101, 引用 - 0
          數(shù)據(jù)加載中……

          為tomcat頁面設(shè)置訪問權(quán)限(j_security_check)

          web應(yīng)用中,對(duì)頁面的訪問控制通常通過程序來控制,流程為:
          登錄 -> 設(shè)置session -> 訪問受限頁面時(shí)檢查session是否存在,如果不存在,禁止訪問

          對(duì)于較小型的web應(yīng)用,可以通過tomcat內(nèi)置的訪問控制機(jī)制來實(shí)現(xiàn)權(quán)限控制。采用這種機(jī)制的好處是,程序中無需進(jìn)行權(quán)限控制,完全通過對(duì)tomcat的配置即可完成訪問控制。

          為了在tomcat頁面設(shè)置訪問權(quán)限控制,在項(xiàng)目的WEB-INFO/web.xml文件中,進(jìn)行如下設(shè)置:

          <web-app>

          <!--servlet等其他配置-->

          <security-constraint>
            <web-resource-collection>
                <display-name>Example Security Constraint</display-name>
                <web-resource-name>My Test</web-resource-name>
            
                <url-pattern>/ddly/admin/*</url-pattern>
            </web-resource-collection>
           
          <auth-constraint>
              <role-name>role1</role-name>
              <role-name>tomcat</role-name>
          </auth-constraint>

          </security-constraint>

          <login-config>
              <auth-method>BASIC</auth-method>
              <realm-name>My Test</realm-name>
          </login-config>

          </web-app>

          其中,<url-pattern>中指定受限的url,可以使用通配符*,通常對(duì)整個(gè)目錄進(jìn)行訪問權(quán)限控制。
          <auth-constraint>
          中指定哪些角色可以訪問<url-pattern>指定的url,在<role-name>中可以設(shè)置一個(gè)或多個(gè)角色名。

          使用的角色名來自tomcat的配置文件${CATALINA_HOME}/conf/tomcat-users.xml

          <login-config>中設(shè)置登錄方式,<auth-method>的取值為BASICFORM。如果為BASIC,瀏覽器在需要登錄時(shí)彈出一個(gè)登錄窗口。如果為FORM方式,需要指定登錄頁面和登錄失敗時(shí)的提示信息顯示頁面。

          使用FORM方式的配置樣例如下:

          <login-config>
             <auth-method>FORM</auth-method>
             <realm-name>Example 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>

          其中的<form-login-page>指定登錄頁面url<form-error-page>指定登錄失敗時(shí)的提示頁面url
          登錄頁面中,formaction,以及其中的用戶名和密碼兩個(gè)參數(shù)的名稱,都應(yīng)取固定的值。登錄的后臺(tái)處理程序?yàn)?SPAN lang=EN-US>j_security_check
          ;用戶名和密碼的參數(shù)名稱分別為:j_usernamej_password
          如下是登錄頁面(如:login.jsp)的一段示例代碼:

          <form method="POST" action='<%= response.encodeURL("j_security_check") %>' >
            <table border="0" cellspacing="5">
              <tr>
                <th align="right">Username:</th>
                <td align="left"><input type="text" name="j_username"></td>
              </tr>
              <tr>
                <th align="right">Password:</th>
                <td align="left"><input type="password" name="j_password"></td>
              </tr>
              <tr>
                <td align="right"><input type="submit" value="Log In"></td>
                <td align="left"><input type="reset"></td>
              </tr>
            </table>
          </form>

          posted on 2005-07-28 12:42 weidagang2046 閱讀(5044) 評(píng)論(1)  編輯  收藏 所屬分類: Java

          評(píng)論

          # re: 為tomcat頁面設(shè)置訪問權(quán)限(j_security_check)   回復(fù)  更多評(píng)論   

          注意這里的url-pattern <url-pattern>/ddly/admin/*</url-pattern>其實(shí)是以本web app為根的相對(duì)路徑。
          2005-09-03 20:45 | weidagang2046

          # re: 為tomcat頁面設(shè)置訪問權(quán)限(j_security_check)   回復(fù)  更多評(píng)論   

          自己測(cè)試了一下,下面是我的web.xml配置文件:

          <?xml version="1.0" encoding="ISO-8859-1"?>

          <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          version="2.4">

          <security-constraint>
          <web-resource-collection>
          <display-name>Example Security Constraint</display-name>
          <web-resource-name>welcome_info</web-resource-name>
          <url-pattern>/*</url-pattern>
          </web-resource-collection>
          <auth-constraint>
          <role-name>role1</role-name>
          <role-name>tomcat</role-name>
          </auth-constraint>
          </security-constraint>
          <login-config>
          <auth-method>BASIC</auth-method>
          <realm-name>welcome_info</realm-name>
          </login-config>
          </web-app>

          2005-09-03 20:47 | weidagang2046
          主站蜘蛛池模板: 伊金霍洛旗| 新巴尔虎左旗| 洪洞县| 佳木斯市| 利辛县| 安化县| 南岸区| 平南县| 青河县| 厦门市| 通许县| 陆川县| 邯郸市| 嘉鱼县| 静宁县| 宁海县| 集贤县| 陆川县| 商丘市| 武冈市| 恩施市| 龙海市| 蕲春县| 万年县| 大荔县| 临武县| 神池县| 麻栗坡县| 平利县| 沾化县| 维西| 湘阴县| 斗六市| 习水县| 马山县| 宝坻区| 栾川县| 米林县| 荃湾区| 瓮安县| 健康|