Tom小貓,讓我看清你的五臟六腑(二)

安全域
這是Tomcat服務器用來保護Web應用資源的一種機制。一個用戶可以擁有一個或多個角色,每個角色限定了可訪問的Web資源,這樣就將用戶和Web資源對應起來了。在org.apache.catalina.Realm接口中聲名了將用戶名、口令和角色相管理的方法,Tomcat5提供了4個實現這一接口的類,分別爲:MemoryRealm(XML文件讀取)、JDBCRealm(JDBC驅動程序讀取)、DataSourceRealm(JNDI數據源讀取)、JNDIRealm(JNDI provider讀取LDAP的目錄服務器信息)。

Web資源的設置
需要在web.xml文件中加入<security-constraint>、<login-config>、<security-role>元素。
例如在Tomcat的admin應用中的配置:

<security-constraint>
    <display-name>Tomcat Server Configuration Security Constraint</display-name>
    <web-resource-collection>
    <web-resource>Protected Area</web-resource>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.do</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

上面的代碼表明:只有admin角色才能訪問admin應用中的*.jsp、*.do和*.html資源。
另一個例子是jsp-examples應用:

<sercurity-constraint>
    <display-name>Tomcat Server Configuration Security Constraint</display-name>
    <web-resource-collection>
        <web-resource>Protected Area</web-resource>
        <url-pattern>/security/protected/*</url-pattern>
        <http-method>DELETE</http-method>
     
<http-method>GET</http-method>
     
<http-method>POST</http-method>
     
<http-method>PUT</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>tomcat</role-name>
     
<role-name>role1</role-name>
    
</auth-constraint>
</security-constraint>

上面的代碼表明:只要tomcat和role1角色纔可以以DELETE、GET、POST和GET方式訪問jsp-exzmples應用URL爲/security/protected/下的資源。
在web.xml中加入<login-config>元素-系統會以對話框的方式進行登陸

<login-config>
    <auth-method>FORM</auth-method>
 
<realm-name>Tomcat Configuration Form-Baseed Authenticaton Area</realm-name>
 
<from-login-config>
       
<from-login-page>/login/login.jsp</from-login-page>

    
<from-error-page>/error.jsp</from-error-page>
 
<from-login-config>
</login-config>

<auth-method>有三個可選項:BASIC、DIGEST、FORM。
BASIC-基本驗證:訪問受保護資源時,會彈出一對話框。要求輸入用戶名和密碼,如果連續3次失敗後,會顯示一個錯誤頁面。這個方法的缺點是用戶名和密碼的數據傳輸採用的是Base64編碼(可讀文本),是非常不安全的。
DIGEST-摘要驗證:數據採用MD5對用戶名和密碼進行加密,然後再傳輸,顯然這種方法很安全。
FORM-表單驗證:可以使用自定義的登陸頁面,但用戶名對應的文本框名稱必須是j_username,密碼爲j_password,且表單action值爲j_security_check。
在web.xml中加入<security-role>元素-指明這個Web應用應用的所有角色的名字

<security-role>
    <description>The role that is required to lon in to the Administration Application.</description>
 
<role-name>admin</role-name>
 
<role-name>friend</role-name>
</security-role>

你可以調用HttpRequeset接口的getRemoteUser()方法返回當前用戶的名字:<%=request.getRemoteUser()%>


內存域-由org.apache.catalina.realm.MemoryRelam類實現
小貓啓動時,自動讀取<%CATALINA_HOME%>/conf/tomcat-users.xml文件,要在Web應用中使用,可以在對應的<Context>元素內加入如下內容:<Realm className="org.apache.catalina.realm.MemoryRelam"/>

JDBC域-通過JDBC驅動從數據庫中直接讀取驗證信息,通過驗證後,信息會存儲在session中。
在mysql中新建兩張表:

create table users{user_name varchar(15not null primary key,user_pass varchar(15not null };
create table usr_roles{usr_name varchar(15not null,role_name varchar(15not null
,
  primary key(user_name,role_name)};

然後在server.xml中加入:

<Realm className="org.apache.catalina.realm.JDBCRealm" driverName="com.mysql.jdbc.Driver" 
             debug
="0" connectionURL="jdbc:mysql://localhost/tomcatusers" connectionName="roor"
 
             connectonPassword
="" userTable="users" userNameCol="user_name"
 
             userCredCol
="user_pass" userRoleTable="user_roles" roleNameCol="role_name">

-和JDBC域很類似,只不過訪問數據庫的方式不同,這個是使用JNDI DataSource來訪問數據庫的。
先在web.xml中加入安全約束,在和JDBC域一樣新建兩張表,然後在server.xml文件的<GlobalNamingResources>元素下添加如下內容:

DataSource域

<Resource name="jdbc/tomcatusers" auth="Container" type="javx.sql.DataSource"/>
<ResourceParams name="jdbc/tomcatusers">
    <parameter>
    
<name>factory</name>
    
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
 
</parameter>
 
<parameter>
     
<name>maxActiove</name>
    
<value>100</value>
 
</parameter>
 
<parameter>
    
<name>maxIdle</name>
    
<value>30</value>
 
</parameter>
 
<parameter>
     
<name>maxWait</name>
    
<value>10000</value>
 
</parameter>
 
<parameter>
     
<name>username</name>
     
<value>root</value>
 
</parameter>
 
<parameter>
     
<name>password</name>
    
<value></value>
 
</parameter>
 
<parameter>
     
<name>driverClassName</name>
    
<value>com.mysql.jdbc.Driver</value>
 
</parameter>
 
<parameter>
     
<name>url</name>
    
<value>jdbc:mysql://localhost/tomcatusers?autoReconnect=true</value>
  
</parameter>
</ResourceParams>

注意:Tomcat的JNDI資源必須配置在<GlobalNamingResources>元素下,服務器才能找到,否則會出現NameNotFoundException;低於Tomcat5.0.12的版本,即使正確配置了DataSourceRealm,也會出現找不到JNDI DataSource的異常,這個小貓的一個bug;在web.xml中是不需要配置<resource-ref>元素的,因爲Web應用並不會訪問這個DataSource。
當然server.xml還需要添加和JDBC域幾乎相同的代碼:

<Realm className="org.apache.catalina.realm.DataSourceRealm" 
             driverName
="com.mysql.jdbc.Driver" debug="0"
 
             connectionURL
="jdbc:mysql://localhost/tomcatusers" connectionName="roor"
 
             connectonPassword
="" userTable="users" userNameCol="user_name"
 
             userCredCol
="user_pass" userRoleTable="user_roles" roleNameCol="role_name">

Tomcat閥
由org.apache.Catalina.Value接口定義,能夠對Catalina容器接收的HTTP Request進行預處理,是小貓特有的功能,可以加入到3種容器中(Engine、Host、Context)。

客戶訪問日誌閥(Access Log Value)- 能夠將可以的Request信息寫入到日誌中。可以記錄頁面訪問的次數、用戶Session活動和用戶驗證信息等。
例如:<Value className="org.apache.catalina.AccessLogValue" directory="logs" prifix="localhost_access_log" suffix=".txt" pattern="%h%l%u%t%s%r%s%b" resolveHost="true">
上面的pattern值可以用common,一個默認的值。
pattern屬性規定日誌的格式和內容:%a-遠程IP地址;%A-本地IP地址;%b-發送的字節數,不包括HTTP Header;%h-遠程主機名;%H-客戶請求所用的協議;%l-"-";%m-請求的方法;%p-接受請求的本地服務器斷開;%q-查詢字符串;%r-用戶請求的第一行內容;%s-響應HTTP Request的狀態碼;%S-用戶Session ID;%t-時間;%u-驗證的用戶名;%U-請求URL路徑;%v-本地服務器名。

遠程地址過濾器(Remote Address Filter)-根據IP地址決定是否接受客戶的請求。
例如:<Value className="org.apache.catalina.RemoteAddrValue" allow="127.0.0.1" deny="127.111.*"/>

遠程主機過濾器(Remot Host Filter)-根據主機名決定是否接受請求。
<Value className="org.apache.catalina.RemoteHostValue" allow="localhost" deny="monster*"/>

客戶請求記錄器(Request Dumper)-把客戶請求的詳細信息記錄在日誌文件中,這裏的日誌文件是指<Logger>元素。
假定在server.xml中localhost的<Host>元素下已經配置了<Logger>元素:

<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="localhos_log." 
              suffix
=".txt" timestamp="true"/>

然後再添加<Value>元素:

<Value className="org.apache.catalina.RequestDumperValue"/>

 


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章