2009-05-05 15 views
2

私はJava-webappを持っています。 webappはwarファイルとしてパッケージ化されています。これらのwarファイルは、HTTP経由で直接配信される静的コンテンツを許可します。この戦争のサーブレットでは、HTTP認証を行うことができます(サーブレット自体で実装します)。しかし、静的コンテンツに対してHTTP-authも必要です。どうすればこれを実現できますか?Java-webapp(war)では、静的コンテンツにHTTP-authを使用するにはどうすればよいですか?

答えて

2

javax.servlet.Filterを実装するクラスを作成します。 The Essentials of Filters

主な方法は、ServletRequest、ServletResponse、およびFilterChainオブジェクトに渡されるdoFilterです。ここで認証を実施します。

次に(すべての要求にマップ)を以下のようにweb.xmlおよびフィルタマッピングであなたのフィルタを宣言

<filter> 
      <filter-name>Authentication Filter</filter-name> 
      <filter-class> 
        com.nfsdsystems.security.filters.AuthenticationFilter</filter-class> 
    </filter> 
    <filter-mapping> 
      <filter-name>Authentication Filter</filter-name> 
      <url-pattern>/*</url-pattern> 
    </filter-mapping> 
+0

それは私にとって素晴らしい作品です。私は自分のフィルターを書いた。 – Mnementh

3

direcotryであなたの静的なhtmlファイルを入れて、あなたのweb.xmlにあなたのセキュリティ制約を定義します。適切なロールに制約をマップします。

<security-constraint> 
     <display-name>securedResources</display-name> 
     <web-resource-collection> 
      <web-resource-name>securedRes</web-resource-name> 
      <url-pattern>/secured/*</url-pattern> 
      <http-method>GET</http-method> 
      <http-method>PUT</http-method> 
      <http-method>HEAD</http-method> 
      <http-method>TRACE</http-method> 
      <http-method>POST</http-method> 
      <http-method>DELETE</http-method> 
      <http-method>OPTIONS</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <description> 
      authenticatedUser_securedRes</description> 
      <role-name>authenticatedUsed</role-name> 
     </auth-constraint> 
    </security-constraint> 
+0

それは本当にレルムを使ってそれを行うのが好ましい方法でしょう。 – Nathan

+0

このようにユーザー名/パスワードをどのように定義できますか? – Mnementh

+0

これは、使用しているアプリケーションサーバーによって異なります。 Tomcatについては、google "jdbc realm configuration"を参照してください。 websphereの場合、推奨される方法はLDAPになります。 – svachon

関連する問題