JSP的安全性

如下內容引用自http://wiki.jikexueyuan.com/project/jsp/security.htmlhtml

JavaServer Pages和Servlets有幾種可用的機制可使Web開發人員用來保護應用程序。資源能夠經過在應用程序部署描述中對它們進行識別而且爲它們分配一個角色來聲明式地保護它們。java

有幾種級別的身份驗證是可用的,從使用基本標示符的基本驗證到複雜的使用證書的密碼驗證。web

1、基本角色的驗證apache

Servlet規範中的認證機制使用的是一項被稱爲基於角色的安全技術。該想法是經過角色來建立角色和限制資源,而不是限制用戶級別的資源。tomcat

能夠定義在文件tomcat-users.xml中定義不一樣的角色,該文件位於Tomcat的主頁目錄中的conf.中。此文件的一個示例以下所示:安全

<?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="role1" password="tomcat" roles="role1"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="admin" password="secret" roles="admin,manager"/>
</tomcat-users>

這個文件在用戶名稱、密碼和角色之間定義了一個簡單的映射。請注意,一個給定的用戶可能有多個角色,例如,在「tomcat」角色中的用戶名=「both」,角色是「role1」。服務器

一旦識別和定義了不一樣的角色,一個基於角色的安全限制能夠經過使用<security-constraint>元素被放置在不一樣的Web應用程序中,該元素在WEB-INF目錄中的web.xml文件中是可用的。cookie

下面是web.xml中一個簡單的示例:session

<web-app>
...
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>
               SecuredBookSite
            </web-resource-name>
            <url-pattern>/secured/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <description>
            Let only managers use this app
            </description>
            <role-name>manager</role-name>
        </auth-constraint>
    </security-constraint>
    <security-role>
       <role-name>manager</role-name>
    </security-role>
    <login-config>
      <auth-method>BASIC</auth-method>
    </login-config>
...
</web-app>

以上條目將意味着:app

  • 任何經過/secured/*對一個URL匹配的HTTP GET或者POST請求都將被安全限制所接受。

  • 一個有着管理員角色的人是能夠訪問被保護的資源的。

  • 最後,login-config元素是用來描述身份驗證的BASIC形式。

如今,若是試圖瀏覽任何包含/security目錄的URL,它將顯示一個詢問用戶名和密碼的對話框。若是提供一個用戶「admin」和密碼「secrer」,那麼能夠經過/secured/*訪問上面的URL,由於已經定義了用戶爲管理員角色,而該角色是有權訪問該資源的。

2、基於表單的身份驗證

當時使用表單身份驗證方法時,必須提供一個登陸表單來提示用戶輸入用戶名和密碼。下面是一個簡單登陸頁面login.jsp的代碼,用來建立一個相同目的的表單:

<html>
<body bgcolor="#ffffff">
   <form method="POST" action="j_security_check">
      <table border="0">
      <tr>
      <td>Login</td>
      <td><input type="text" name="j_username"></td>
      </tr>
      <tr>
      <td>Password</td>
      <td><input type="password" name="j_password"></td>
      </tr>
      </table>
      <input type="submit" value="Login!">
      </center>
   </form>
</body>
</html>

在這裏,必須確保登陸表單中必須包含以j_username和j_password命名的表單元素。在<form>標籤中的動做必須是j_security_check。POST必須以表單的方法來使用。同時必須修改<login-config>標籤來指定auth-method做爲表單:

<web-app>
...
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>
               SecuredBookSite
            </web-resource-name>
            <url-pattern>/secured/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <description>
            Let only managers use this app
            </description>
            <role-name>manager</role-name>
        </auth-constraint>
    </security-constraint>
    <security-role>
       <role-name>manager</role-name>
    </security-role>
    <login-config>
      <auth-method>FORM</auth-method>
      <form-login-config>
        <form-login-page>/login.jsp</form-login-page>
        <form-error-page>/error.jsp</form-error-page>
      </form-login-config>
    </login-config>
...
</web-app>

如今,當試圖用URL:/secured/*訪問任何資源時,它將顯示以上的表單,要求用戶id和密碼。當容器看到「j_security_check」動做時,它會使用一些內部機制來對調用方進行身份驗證。

若是登陸成功,調用者會被受權訪問安全資源,那麼從那時起容器會用一個session-id來識別調用者的登陸會話。容器會用一個包含session-id的cookie來保持這個登陸會話。服務器將cookie發送回客戶端,而且只要調用者使用後續的請求顯示這個cookie時,那麼容器就會知道這個調用者是誰。

若是登陸失敗,那麼服務器將發回由form-error-page設置識別的頁面。

這裏,j_security_check是登陸表單中使用基於表單登陸的應用程序必須指定的一個動做。在相同的表單中,應該有一個名爲j_username的文本輸入控件和一個名爲j_password的密碼輸入控件。當看到這,這意味着表單中包含的信息將會被提交到服務器,服務器將會檢查用戶名和密碼。這一步如何實現是由服務器指定的。

檢查 Standard Realm Implementations來來了解j_security_check是如何爲Tomcat容器工做的。

3、Servlet/JSP中的程序性安全

HttpServletRequest對象提供瞭如下方法,它能夠在運行時用於挖掘安全信息:

方法 描述

String getAuthType()

getAuthType()方法返回一個字符串對象,表明用於保護Servlet的身份驗證方案的名稱。

boolean isUserInRole(java.lang.String role)

isUserInRole()返回一個布爾型的值:若是用戶在給定的角色中值爲真,不然值爲假。

String getProtocol()

getProtocol()方法返回一個字符串對象,表明用戶發送請求的協議。它的值能夠用來檢查肯定一個保護協議是否被使用。

boolean isSecure()

isSecure()方法返回一個布爾型的值,表明請求是不是使用的HTTPS協議。真值表明是而且鏈接是安全的。假值表明不是。

Principle getUserPrinciple()

getUserPrinciple()方法返回一個java.security.Principle對象,該對象包含當前已驗證用戶的用戶名。

例如,一個JavaServer Page鏈接的是管理員頁面,可能會使用如下代碼:

<% if (request.isUserInRole("manager")) { %><a href="managers/mgrreport.jsp">Manager Report</a><a href="managers/personnel.jsp">Personnel Records</a><% } %>

在一個JSP或者Servlet裏,經過檢查用戶的角色,能夠自定義Web網頁來顯示只有用戶本身能夠訪問的條目。若是須要用戶名做爲身份驗證表單的輸入,那麼能夠在請求對象中調用getRemoteUser方法。

相關文章
相關標籤/搜索