Tomcat web程序安全機制 (轉載)

在tomat web服務器中,可以有兩種主要的方式來對用戶,角色,以及領域進行定義。一種使用簡單的xml用戶定義的內存realm的驗證方式,另外一種是建立數據庫連接的jdbc realm驗證方式。這兩種安全機制定製用戶信息來源。
 
一,內存(memoryrealm)驗證方式:
web程序的擁護,角色,分組在tomcat的/conf/tomat-users.xml文件中定義。這個xml文件列出web服務器允許的用戶名稱,密碼以及對應的分組等。
例如:
 

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


 

這裏就定義了兩個角色,三個用戶以及對應的密碼。

要使以上的配置文件作用於定義域,就要在server.xml把這個文件定義成爲一個數據資源。其目的就是高速web服務器能夠在這個文件中找到相關的用戶信息。這個定義在tomcat是默認定義的。定義在<GlobalNamingResources>元素中:

<!-- Global JNDI resources -->
  <GlobalNamingResources>

    <!-- Test entry for demonstration purposes -->
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>

    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
       description="User database that can be updated and saved"
           factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />

  </GlobalNamingResources>

可以看到pathname="conf/tomcat-users.xml"調用上面的那個配置好的文件。雖然這種方式比較簡單,但存在着一定問題,比如手動輸入,一旦數據多了就很麻煩等。而且保密效果不是很好,xml各式的文件誰都可以打開直接看到。

二,JDBC realm

首先也是在server.xml文件中<Realm>元素中配置一個使用mysql數據庫的realm 

在TOMCAT的server.xml中配置JDBC域驗證
      <Realm className="org.apache.catalina.realm.JDBCRealm"
             driverName="com.mysql.jdbc.Driver"
          connectionURL="jdbc:mysql://localhost:3306/mydb"
         connectionName="root" connectionPassword="novell"
              userTable="users" userNameCol="user_name" userCredCol="user_pass"
          userRoleTable="user_roles" roleNameCol="role_name" />
<!--當然別望了在Mysql中建立相應的數據表和字段 -->

當中的係數名字不解釋了...

對應的表格

create table users(user_name varchar(15) null primary key,

                   user_pass varhcar(20) not null);

create tabel user_roles( user_name varchar(15) not null,

                        role_naem varchar(20) not null,

primary key(user_name,role_name));

現在簡單介紹一下基本的驗證方式

首先我們應該在web.xml文件中對需要保護的資源定義一個<security-constraint>比如你要限制url爲當前web目中的所有文件/*

 

 

<security-constraint>
     <web-resource-collection>
         <web-resource-name>BasicLogin</web-resource-name>
                   
         <url-pattern>/*</url-pattern>
        

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

     </web-resource-collection>
    <auth-constraint>
        <!-- NOTE: This role is not present in the default users file -->
        <role-name>tomcat</role-name>
    </auth-constraint>
    <user-data-constraint>
    <description> no description</description>
    <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>
    
<login-config>
      <auth-method>BASIC</auth-method>
      <realm-name>default</realm-name>
</login-config>
   

 其中<web-resource-collection>標記中url-pattern制定保護了url,http-method定義了限定web的請求方法,<auth-constrain>標記了role-name制定了合法用戶的角色。<user-data-constraint>中定義了不需要通信的保證。當然了還需要申明使用了基本的驗證方式和使用默認的realm.聲明部分在上面紅色部分給出,即<login-config>.這樣的話當瀏覽器請求

發佈了16 篇原創文章 · 獲贊 0 · 訪問量 1932
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章