サーブレット・セキュリティー動的アノテーション
プログラマチック API を使用してサーブレットを追加または作成する場合、セキュリティー・アノテーション RunAS、declareRoles、および ServletSecurity を、setRunAsRole()、declareRoles()、および setServletSecurity() メソッドをそれぞれ使用して動的に更新できます。
アプリケーションの開始時に、 Web コンテナーは、RunAs、declareRoles、および ServletSecurity アノテーションが付いたサーブレットをすべて検査し、ServletRegistration アノテーションの setServletSecurity() メソッドでこれらのアノテーションを設定します。 Web コンテナーは、セキュリティー・コンポーネントに対し、URL パターンとセキュリティー制約が含まれているすべての ServletRegistration アノテーションを検査するように通知します。セキュリティー・コンポーネントは、デプロイメント記述子内に URL パターンが定義されているか どうかを判別します。デプロイメント記述子内で完全に一致する URL パターンが既に定義されている場合、デプロイメント記述子の URL パターンのセキュリティー制約と RunAs ロールが、動的データの代わりに使用されます。

セキュリティー動的アノテーションに、ServletSecurity アノテーションに正確に一致する URL パターンがある場合、セキュリティー動的アノテーション内の URL パターンのセキュリティー制約が優先されます。同じ URL パターンを使用して setServletSecurity() メソッドを複数回呼び出す場合、最後に呼び出されたメソッドが優先されます。
- ServletRegistration.Dynamic.setRunAsRole(String roleName) は、このサーブレット登録の RunAs ロールの名前を設定します。
- ServletContext.declareRoles(String roleNames) は、isUserInRole() メソッドでテストされるロール名を宣言します。
- ServletRegistration.Dynmaic.setServletSecurity(ServletSecurityElement constraint) は、このサーブレット登録の ServletSecurityElement を設定します。
以下の 2 つの例は、setServletSecurity() メソッドを使用して動的サーブレットのセキュリティー制約と RunAs ロールを設定する際に使用できます。
HttpConstraintElement constraint = new HttpConstraintElement(TransportGuarantee.NONE,
new String[]{"Employee"});
List<HttpMethodConstraintElement> methodConstraints =
new ArrayList<HttpMethodConstraintElement>();
methodConstraints.add(new HttpMethodConstraintElement("PUT",
new HttpConstraintElement(TransportGuarantee.CONFIDENTIAL, new String[]{"Manager"})));
ServletSecurityElement servletSecurity =
new ServletSecurityElement(constraint, methodConstraints);
HttpConstraintElement constraint = new HttpConstraintElement();
List<HttpMethodConstraintElement> methodConstraints =
new ArrayList<HttpMethodConstraintElement>();
methodConstraints.add(new HttpMethodConstraintElement("CUSTOM",
new HttpConstraintElement(TransportGuarantee.NONE, new String[]{"Manager"})));
methodConstraints.add(new HttpMethodConstraintElement("GET",
new HttpConstraintElement(TransportGuarantee.CONFIDENTIAL, new String[]{"Employee"})));
ServletSecurityElement servletSecurity = new ServletSecurityElement(constraint,
methodConstraints);