Java-webapp(war)で、静的コンテンツにHTTP-authを使用するにはどうすればよいですか?
-
05-07-2019 - |
質問
Java-webappがあります。 webappはwarファイルとしてパッケージ化されています。これらのwarファイルは静的コンテンツを許可します。これはHTTP経由で直接配信されます。この戦争のサーブレットの場合、HTTP認証を行うことができます(サーブレット自体で実装します)。しかし、静的コンテンツのHTTP認証も必要です。どうすればこれを実現できますか?
解決
javax.servlet.Filterを実装するクラスを作成します。 フィルターの要点
を参照してください。メインメソッドは、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>
他のヒント
静的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>
所属していません StackOverflow