Identify correct and incorrect statements or examples about use of the isCallerInRole and getCallerPrincipal EJB programmatic security APIs.

[Note]

Note: In general, security management should be enforced by the container in a manner that is transparent to the enterprise beans' business methods. The security API described in this section should be used only in the less frequent situations in which the enterprise bean business methods need to access the security context information.

The javax.ejb.EJBContext interface provides two methods (plus two deprecated methods that were defined in EJB 1.0) that allow the Bean Provider to access security information about the enterprise bean's caller:

public interface javax.ejb.EJBContext {
	...
	//
	// The following two methods allow the EJB class
	// to access security information.
	//
	java.security.Principal getCallerPrincipal();
	boolean isCallerInRole(String roleName);
	
	//
	// The following two EJB 1.0 methods are DEPRECATED.
	//
	java.security.Identity getCallerIdentity();
	boolean isCallerInRole(java.security.Identity role);
	...
}					
					

The Bean Provider can invoke the getCallerPrincipal and isCallerInRole methods ONLY in the enterprise bean's business methods. If they are otherwise invoked when no security context exists, they should throw the java.lang.IllegalStateException runtime exception.

The getCallerIdentity() and isCallerInRole(Identity role) methods were deprecated in EJB 1.1. The Bean Provider MUST use the getCallerPrincipal() and isCallerInRole(String roleName) methods for new enterprise beans.

Use of getCallerPrincipal

The purpose of the getCallerPrincipalmethod is to allow the enterprise bean methods to obtain the current caller principal's name. The methods might, for example, use the name as a key to information in a database.

An enterprise bean can invoke the getCallerPrincipal method to obtain a java.security.Principal interface representing the current caller. The enterprise bean can then obtain the distinguished name of the caller principal using the getName method of the java.security.Principal interface. If the security identity has not been established, getCallerPrincipal returns the container's representation of the UNAUTHENTICATED identity.

Note that getCallerPrincipal returns the principal that represents the CALLER of the enterprise bean, NOT the principal that corresponds to the run-as security identity for the bean, if any.

The meaning of the current caller, the Java class that implements the java.security.Principal interface, and the realm of the principals returned by the getCallerPrincipal method depend on the operational environment and the configuration of the application.

The following code sample illustrates the use of the getCallerPrincipal() method:

@Stateless public class EmployeeServiceBean implements EmployeeService {

	@Resource SessionContext ctx;
	@PersistenceContext EntityManager em;

	public void changePhoneNumber(...) {
		...
		// obtain the caller principal.
		callerPrincipal = ctx.getCallerPrincipal();

		// obtain the caller principalтАЩs name.
		callerKey = callerPrincipal.getName();

		// use callerKey as primary key to find EmployeeRecord
		EmployeeRecord myEmployeeRecord = em.find(EmployeeRecord.class, callerKey);

		// update phone number
		myEmployeeRecord.setPhoneNumber(...);
		...
	}
}
					

In the previous example, the enterprise bean obtains the principal name of the current caller and uses it as the primary key to locate an EmployeeRecord entity. This example assumes that application has been deployed such that the current caller principal contains the primary key used for the identification of employees (e.g., employee number).

Use of isCallerInRole

The main purpose of the isCallerInRole(String roleName) method is to allow the Bean Provider to code the security checks that cannot be easily defined declaratively in the deployment descriptor using method permissions. Such a check might impose a role-based limit on a request, or it might depend on information stored in the database.

The enterprise bean code can use the isCallerInRole method to test whether the current caller has been assigned to a given security role. Security roles are defined by the Bean Provider or the Application Assembler, and are assigned to principals or principal groups that exist in the operational environment by the Deployer.

Note that isCallerInRole(String roleName) tests the principal that represents the CALLER of the enterprise bean, NOT the principal that corresponds to the run-as security identity for the bean, if any.

The following code sample illustrates the use of the isCallerInRole(String roleName) method:

					
@Stateless public class PayrollBean implements Payroll {
	
	@Resource SessionContext ctx;

	public void updateEmployeeInfo(EmplInfo info) {
		oldInfo = ... read from database;
		
		// The salary field can be changed only by callers
		// who have the security role "payroll"
		if (info.salary != oldInfo.salary && !ctx.isCallerInRole("payroll")) {
			throw new SecurityException(...);
		}
		...
	}
	...
}		
			
					

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine