15.3. サーブレットでのロールベースのセキュリティーの使用
セキュリティーをサーブレットに追加するには、各サーブレットを URL パターンにマッピングし、セキュリティー確保が必要な URL パターンにセキュリティー制約を作成します。セキュリティー制約は URL へのアクセスをロールに制限します。認証および承認は、WAR の
jboss-web.xml で指定されたセキュリティードメインによって処理されます。
前提条件
サーブレットでロールベースのセキュリティーを使用する前に、アクセスの認証および承認に使用されるセキュリティードメインを JBoss EAP 6 コンテナーに設定する必要があります。
手順15.1 ロールベースのセキュリティーをサーブレットに追加
サーブレットと URL パターン間のマッピングを追加します。
web.xmlの <servlet-mapping> 要素を使用して、個別のサーブレットを URL パターンにマッピングします。以下の例では、DisplayOpResultというサーブレットを URL パターン/DisplayOpResultにマッピングします。<servlet-mapping> <servlet-name>DisplayOpResult</servlet-name> <url-pattern>/DisplayOpResult</url-pattern> </servlet-mapping>URL パターンにセキュリティー制約を追加します。
URL パターンをセキュリティー制約にマッピングするには、<security-constraint> を使用します。以下の例は、eap_adminロールを持つユーザーがアクセスする URL パターン/DisplayOpResultからのアクセスを制限します。ロールはセキュリティードメインに存在する必要があります。<security-constraint> <display-name>Restrict access to role eap_admin</display-name> <web-resource-collection> <web-resource-name>Restrict access to role eap_admin</web-resource-name> <url-pattern>/DisplayOpResult/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>eap_admin</role-name> </auth-constraint> </security-constraint> <security-role> <role-name>eap_admin</role-name> </security-role> <login-config> <auth-method>BASIC</auth-method> </login-config>認証方法を指定する必要があります。BASIC、FORM、DIGEST、CLIENT-CERT、SPNEGO のいずれかです。この例ではBASIC認証を使用します。WAR の
jboss-web.xmlでセキュリティードメインの指定サーブレットを設定済みのセキュリティードメインに接続するには、セキュリティードメインを WAR のjboss-web.xmlに追加します。これは、セキュリティー制約に対してプリンシパルを認証および承認する方法を認識します。次の例では、acme_domainというセキュリティードメインを使用します。<jboss-web> ... <security-domain>acme_domain</security-domain> ... </jboss-web>
例15.1 ロールベースのセキュリティーが設定された web.xml の例
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Use Role-Based Security In Servlets</display-name>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>DisplayOpResult</servlet-name>
<url-pattern>/DisplayOpResult</url-pattern>
</servlet-mapping>
<security-constraint>
<display-name>Restrict access to role eap_admin</display-name>
<web-resource-collection>
<web-resource-name>Restrict access to role eap_admin</web-resource-name>
<url-pattern>/DisplayOpResult/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>eap_admin</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>eap_admin</role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>