Which annotation allows a request to be executed with another Principal's identity?
Click on the arrows to vote for the correct answer
A. B. C. D.C.
https://docs.oracle.com/cd/E19798-01/821-1841/6nmq2cpig/index.htmlThe correct answer is B. @UseIdentity
.
Explanation: In Java EE, the @UseIdentity
annotation can be used to allow a request to be executed with another Principal's identity. This annotation is used in conjunction with the runAs
method of the EJBContext
interface. The runAs
method can be called to switch the identity of the current thread to the identity of the specified user.
For example, consider the following code snippet:
java@Stateless public class MyService { @Resource private EJBContext context; @RolesAllowed({"admin"}) public void doAdminTask() { // perform administrative task } @UseIdentity public void doTaskAsUser(String username) { context.getCallerPrincipal(); // returns the principal of the calling user context.getEJBContext().runAs(username); context.getCallerPrincipal(); // returns the principal of the specified user doAdminTask(); // execute task as the specified user } }
In this example, the doTaskAsUser
method is annotated with @UseIdentity
. This allows the method to execute with the identity of the specified user, rather than the identity of the calling user. The runAs
method is used to switch the identity of the current thread to the specified user, and the doAdminTask
method is then executed with the identity of the specified user.
Note that the @RolesAllowed
annotation is used to restrict access to the doAdminTask
method to users with the "admin" role. This annotation is not related to switching the identity of the current thread.
Option A, "It is not possible to execute a request with another Principal's identity," is incorrect because it is possible to do so using the @UseIdentity
annotation and the runAs
method.
Option C, @RolesAllowed
, is incorrect because it is not related to switching the identity of the current thread.
Option D, @Runs
, is not a valid annotation in Java EE.