Configuring Security Roles Using Annotations in Web Applications

Use

There are two annotation configuring security settings:

  • @DeclareRoles

    This annotation declares the security roles defined by the application.

  • @RunAs

    This annotation maps the application's security role to an existing security role.

Procedure

Using the @DeclareRoles Annotation

Use this annotation at class level, passing the role name as an attribute.

The @DeclareRole("Customer") statement would be equivalent to defining the following security role in the web.xml deployment descriptor.

               <web-app>
…
<security-role>
<role-name>Customer</role-name>
</security-role>
…
</web-app>

            

Using the @RunAs Annotation

Use this annotation at class level, passing the role name as an attribute.

The @RunAs("Customer") statement would be equivalent to defining the following security role in the web.xml deployment descriptor.