認証のテスト Howto/Testing secure code Howto

Last update : July 6 2002
Doc for : v1.3

Cactusについて
  • Cactus とは
  • ニュース
  • 変更履歴
  • 特徴/開発状況
  • 目標
  • ロードマップ/ToDo
  • 協力者
  • 協力者募集
  • Cactus ユーザ
  • テスト済環境 ...
  • ライセンス


  • ダウンロード
  • ダウンロード


  • ドキュメント
  • Cactus の仕組み
  • さぁ始めよう
  • モック対コンテナ
  • Javadocs
  • └Javadocs
  • よくある質問


  • Howto ガイド
  • クラスパス Howto
  • 設定 Howto
  • アップグレードHowto
  • テストケース Howto
  • セキュリティHowto
  • Ant Howto
  • HttpUnit Howto
  • サンプル Howto
  • EJB Howto
  • IDE Howto
  • JUnitEE Howto


  • サポート
  • Bug DB
  • メーリングリスト


  • その他
  • 名前の由来
  • ロゴコンテスト
  • 参考文献
  • アクセス状況
  • └WebAlizer


  • 開発者向け
  • CVS
  • コード規約
  • ビルドの結果


  • 認証を含むコードのテストの導入/Introduction to testing secure code

    Beginning with version 1.3, Cactus is able to unit test Servlet code that uses the Servlet Security API (getRemoteUser(), isUserInRole(), getUserPrincipal()).

    バージョン 1.3 より、 Servlet Security API (getRemoteUser(), isUserInRole(), getUserPrincipal())を使った Servlet コードの単体テストができるようになりました。

    The way to perform this is by securing a Servlet Redirector defined in your web.xml and then to specify user credentials in your beginXXX(), as defined below.

    これを行う方法は、下で示すように、 web.xml で定義された Servlet Redirector を保障されたものとし、 beginXXX() において セキュリティ属性(credential)を指定するというものです。

    Note The only currently supported authentication mechanism in Cactus 1.3 is the BASIC authentication.

    Note 現在、Cactus 1.3 でサポートされている認証メカニズムは BASIC 認証のみです。

    Note The Cactus sample application demonstrates how to unit test everything that is explained here.

    Note Cactus の例題では、 ここで説明した全ての事を単体テストする方法について示します。


    ステップ 1 : beginXXX() においてセキュリティ属性を渡す/Step 1 : Passing credentials in beginXXX()

    Let's start with an example :

    例から始めましょう :

    public void beginBasicAuthentication(WebRequest theRequest)
    {
        theRequest.setRedirectorName("ServletRedirectorSecure");
        theRequest.setAuthentication(
            new BasicAuthentication("testuser", "testpwd"));
    }
    
    public void testBasicAuthentication()
    {
        assertEquals("testuser", request.getUserPrincipal().getName());
        assertEquals("testuser", request.getRemoteUser());
        assertTrue("User not in 'test' role", request.isUserInRole("test"));
    }
    

    There are several things to understand here :

    ここで幾つか理解しておくべきことがあります :

    • The Servlet that is called on the server side is the Cactus redirector servlet and thus we'll need to secure it in our web.xml (see step 2 below).
    • サーバ側で呼ばれるサーブレットは Cactus リダイレクタサーブレットなので、 web.xml において、ユーザー認証に対応したものにしておく必要があります。 (ステップ 2 参照)
    • WebRequest.setRedirectorName() is an API that lets you override the redirector defined in cactus.properties. This is needed here because we want to test both code that is not secured (i.e. for which we don't want to have to pass credentials) and code that is secured and thus we need 2 redirectors. The ServletRedirectorSecure redirector will be secured in step 2 below.
    • WebRequest.setRedirectorName()cactus.properties で定義されたリダイレクタを上書きさせる API です。 これは、ここで必要です。 なぜなら、ユーザ認証に対応していないコード (即ち、セキュリティ属性を渡す必要の無いもの)も、 ユーザ認証に対応したコードも両方テストしたいからです。 ですから、2 つのリダイレクタが必要です。 ServletRedirectorSecure リダイレクタは 下のステップ 2 において、ユーザ認証に対応できます。
    • WebRequest.setAuthentication() is used to pass credentials to the server. It takes a parameter which is a BasicAuthentication object (the only type of authentication currently supported by Cactus).
    • WebRequest.setAuthentication() は、サーバーへのセキュリティ属性を渡すために使われます。 これは、 BasicAuthentication オブジェクト (現在 Cactus がサポートしている唯一の認証のタイプ) のパラメーターをとります。

    ステップ 2 : Cactus リダイレクタにユーザ認証をかける/Step 2 : Securing the Cactus Redirector

    All calls to the server side go through the Cactus Servlet Redirector and thus it is that servlet that needs to be secured in web.xml. It is performed as follows (example) :

    サーバ側への全ての呼び出しは、 Cactus サーブレットリダイレクタを通じて行われるので、 web.xmlでユーザ認証に対応する必要があるのは、そのサーブレットなのです。

    [...]
    
    <web-app>
    
        <servlet>
            <servlet-name>ServletRedirector</servlet-name>
            <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
        </servlet>
    
        <servlet>
            <servlet-name>ServletRedirectorSecure</servlet-name>
            <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
        </servlet>
    
        [...]
    
        <servlet-mapping>
            <servlet-name>ServletRedirector</servlet-name>
            <url-pattern>/ServletRedirector</url-pattern>
        </servlet-mapping>
    
        <servlet-mapping>
            <servlet-name>ServletRedirectorSecure</servlet-name>
            <url-pattern>/ServletRedirectorSecure</url-pattern>
        </servlet-mapping>
    
        [...]
    
      	<!-- Start Authentication -->
      	<!-- 認証の開始 -->
    
      	<security-constraint>
         	<web-resource-collection>
            	<web-resource-name>SecurityRestriction</web-resource-name>
             	<description>Protect the Cactus redirector servlet.</description>
             	<url-pattern>/ServletRedirectorSecure</url-pattern>
             	<http-method>GET</http-method>
             	<http-method>POST</http-method>
         	</web-resource-collection>
         	<auth-constraint>
             	<description>Authorized Users Group</description>
             	<role-name>test</role-name>
         	</auth-constraint>
         	<user-data-constraint>
            	<transport-guarantee>NONE</transport-guarantee>
        	</user-data-constraint>
       	</security-constraint>
    
        <login-config>
        	<auth-method>BASIC</auth-method>
       	</login-config>
    
    	<security-role>
    		<description>Test role</description>
    		<role-name>test</role-name>
    	</security-role>
    
      	<!-- End Authentication -->
      	<!-- 認証の終わり -->
    
    </web-app>
    

    ステップ 3 : ユーザ/ロールのマッピング/Step 3 : Map Users/Roles

    This step consists in defining authorized users and mapping them to the role defined in web.xml (e.g. in the example above, we have defined a test role).

    この段階では、権限をもつユーザの定義とweb.xml で定義されたロール(役割)との関連付けが含まれています。 (例えば、上の例では test ロールを定義しました)

    This step is completely container-dependent and there is no standard way of doing it. You'll have to learn how to do it for your container. For example, here is how you would do it for Tomcat 4.0 :

    このステップは、完全にコンテナに依存し、 これを行う標準的な方法はありません。 使っているコンテナで行うやり方を勉強しなければなりません。 例として、Tomcat 4.0 での設定法を示します :

    • Create a tomcat-users.xml file that you put in your Tomcat configuration directory (where you have server.xml defined).
    • Tomcat 設定ディレクトリに tomcat-users.xml ファイルを作成します。 (ここでは server.xml が定義されています。)

    Here is an example of tomcat-users.xml that matches the test we have defined in step 1 :

    ここに、ステップ 1 で定義した test とマッチする tomcat-users.xml の例を示します :

    <tomcat-users>
      <user name="testuser" password="testpwd" roles="test" />
    </tomcat-users>
    



    Copyright © 2000-2002 The Apache Software Foundation. All Rights Reserved.