Securing Enterprise Beans - The Java EE 6 Tutorial (original) (raw)

Document Information

Preface

Part I Introduction

1. Overview

2. Using the Tutorial Examples

Part II The Web Tier

3. Getting Started with Web Applications

4. JavaServer Faces Technology

5. Introduction to Facelets

6. Expression Language

7. Using JavaServer Faces Technology in Web Pages

8. Using Converters, Listeners, and Validators

9. Developing with JavaServer Faces Technology

10. JavaServer Faces Technology: Advanced Concepts

11. Using Ajax with JavaServer Faces Technology

12. Composite Components: Advanced Topics and Example

13. Creating Custom UI Components and Other Custom Objects

14. Configuring JavaServer Faces Applications

15. Java Servlet Technology

16. Uploading Files with Java Servlet Technology

17. Internationalizing and Localizing Web Applications

Part III Web Services

18. Introduction to Web Services

19. Building Web Services with JAX-WS

20. Building RESTful Web Services with JAX-RS

21. JAX-RS: Advanced Topics and Example

Part IV Enterprise Beans

22. Enterprise Beans

23. Getting Started with Enterprise Beans

24. Running the Enterprise Bean Examples

25. A Message-Driven Bean Example

26. Using the Embedded Enterprise Bean Container

27. Using Asynchronous Method Invocation in Session Beans

Part V Contexts and Dependency Injection for the Java EE Platform

28. Introduction to Contexts and Dependency Injection for the Java EE Platform

29. Running the Basic Contexts and Dependency Injection Examples

30. Contexts and Dependency Injection for the Java EE Platform: Advanced Topics

31. Running the Advanced Contexts and Dependency Injection Examples

Part VI Persistence

32. Introduction to the Java Persistence API

33. Running the Persistence Examples

34. The Java Persistence Query Language

35. Using the Criteria API to Create Queries

36. Creating and Using String-Based Criteria Queries

37. Controlling Concurrent Access to Entity Data with Locking

38. Using a Second-Level Cache with Java Persistence API Applications

Part VII Security

39. Introduction to Security in the Java EE Platform

40. Getting Started Securing Web Applications

41. Getting Started Securing Enterprise Applications

Examples: Securing Enterprise Beans

The cart-secure Example: Securing an Enterprise Bean with Declarative Security

Annotating the Bean

To Run the cart-secure Example Using NetBeans IDE

To Run the cart-secure Example Using Ant

The converter-secure Example: Securing an Enterprise Bean with Programmatic Security

Modifying ConverterBean

Modifying ConverterServlet

To Build, Package, and Deploy the converter-secure Example Using NetBeans IDE

To Build, Package, and Deploy the converter-secure Example Using Ant

To Run the converter-secure Example

42. Java EE Security: Advanced Topics

Part VIII Java EE Supporting Technologies

43. Introduction to Java EE Supporting Technologies

44. Transactions

45. Resources and Resource Adapters

46. The Resource Adapter Example

47. Java Message Service Concepts

48. Java Message Service Examples

49. Bean Validation: Advanced Topics

50. Using Java EE Interceptors

Part IX Case Studies

51. Duke's Bookstore Case Study Example

52. Duke's Tutoring Case Study Example

53. Duke's Forest Case Study Example

Index

Enterprise beans are Java EE components that implement EJB technology. Enterprise beans run in the EJB container, a runtime environment within the GlassFish Server. Although transparent to the application developer, the EJB container provides system-level services, such as transactions and security to its enterprise beans, which form the core of transactional Java EE applications.

Enterprise bean methods can be secured in either of the following ways:

Some of the material in this chapter assumes that you have already read Chapter 22, Enterprise Beans, Chapter 23, Getting Started with Enterprise Beans, and Chapter 39, Introduction to Security in the Java EE Platform.

As mentioned earlier, enterprise beans run in the EJB container, a runtime environment within the GlassFish Server, as shown in Figure 41-1.

Figure 41-1 Java EE Server and Containers

Diagram of Java EE server showing web container and EJB container

This section discusses securing a Java EE application where one or more modules, such as EJB JAR files, are packaged into an EAR file, the archive file that holds the application. Security annotations will be used in the Java programming class files to specify authorized users and basic, or user name/password, authentication.

Enterprise beans often provide the business logic of a web application. In these cases, packaging the enterprise bean within the web application’s WAR module simplifies deployment and application organization. Enterprise beans may be packaged within a WAR module as Java class files or within a JAR file that is bundled within the WAR module. When a servlet or JavaServer Faces page handles the web front end and the application is packaged into a WAR module as a Java class file, security for the application can be handled in the application’s web.xmlfile. The EJB in the WAR file can have its own deployment descriptor,ejb-jar.xml, if required. Securing web applications using web.xml is discussed in Chapter 40, Getting Started Securing Web Applications.

The following sections describe declarative and programmatic security mechanisms that can be used to protect enterprise bean resources. The protected resources include enterprise bean methods that are called from application clients, web components, or other enterprise beans.

For more information on this topic, read the Enterprise JavaBeans 3.1 specification. This document can be downloaded from http://jcp.org/en/jsr/detail?id=318. Chapter 17 of this specification, “Security Management,” discusses security management for enterprise beans.

Securing an Enterprise Bean Using Declarative Security

Declarative security enables the application developer to specify which users are authorized to access which methods of the enterprise beans and to authenticate these users with basic, or username-password, authentication. Frequently, the person who is developing an enterprise application is not the same person who is responsible for deploying the application. An application developer who uses declarative security to define method permissions and authentications mechanisms is passing along to the deployer a security view of the enterprise beans contained in the EJB JAR. When a security view is passed on to the deployer, he or she uses this information to define method permissions for security roles. If you don’t define a security view, the deployer will have to determine what each business method does to determine which users are authorized to call each method.

A security view consists of a set of security roles, a semantic grouping of permissions that a given type of users of an application must have to successfully access the application. Security roles are meant to be logical roles, representing a type of user. You can define method permissions for each security role. A method permission is a permission to invoke a specified group of methods of an enterprise bean’s business interface, home interface, component interface, and/or web service endpoints. After method permissions are defined, user name/password authentication will be used to verify the identity of the user.

It is important to keep in mind that security roles are used to define the logical security view of an application. They should not be confused with the user groups, users, principals, and other concepts that exist in the GlassFish Server. An additional step is required to map the roles defined in the application to users, groups, and principals that are the components of the user database in the file realm of the GlassFish Server. These steps are outlined in Mapping Roles to Users and Groups.

The following sections show how an application developer uses declarative security to either secure an application or to create a security view to pass along to the deployer.

Specifying Authorized Users by Declaring Security Roles

This section discusses how to use annotations to specify the method permissions for the methods of a bean class. For more information on these annotations, refer to the Common Annotations for the Java Platform specification at http://jcp.org/en/jsr/detail?id=250.

Method permissions can be specified on the class, the business methods of the class, or both. Method permissions can be specified on a method of the bean class to override the method permissions value specified on the entire bean class. The following annotations are used to specify method permissions:

The following code snippet demonstrates the use of the @DeclareRoles annotation with the isCallerInRole method. In this example, the @DeclareRoles annotation declares a role that the enterprise bean PayrollBean uses to make the security check by using isCallerInRole("payroll") to verify that the caller is authorized to change salary data:

@DeclareRoles("payroll") @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"
    Principal callerPrincipal = ctx.getCallerPrincipal();
    if (info.salary != oldInfo.salary && !ctx.isCallerInRole("payroll")) {
        throw new SecurityException(...);
    }
    ...
}
...

}

The following example code illustrates the use of the @RolesAllowed annotation:

@RolesAllowed("admin") public class SomeClass { public void aMethod () {...} public void bMethod () {...} ... }

@Stateless public class MyBean extends SomeClass implements A {

@RolesAllowed("HR")
public void aMethod () {...}

public void cMethod () {...}
...

}

In this example, assuming that aMethod, bMethod, and cMethod are methods of business interface A, the method permissions values of methods aMethod and bMethodare @RolesAllowed("HR") and @RolesAllowed("admin"), respectively. The method permissions for method cMethod have not been specified.

To clarify, the annotations are not inherited by the subclass itself. Instead, the annotations apply to methods of the superclass that are inherited by the subclass.

Specifying an Authentication Mechanism and Secure Connection

When method permissions are specified, basic user name/password authentication will be invoked by the GlassFish Server.

To use a different type of authentication or to require a secure connection using SSL, specify this information in an application deployment descriptor.

Securing an Enterprise Bean Programmatically

Programmatic security, code that is embedded in a business method, is used to access a caller’s identity programmatically and uses this information to make security decisions within the method itself.

Accessing an Enterprise Bean Caller’s Security Context

In general, security management should be enforced by the container in a manner that is transparent to the enterprise bean’s 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, such as when you want to restrict access to a particular time of day.

The javax.ejb.EJBContext interface provides two methods that allow the bean provider to access security information about the enterprise bean’s caller:

You would use programmatic security in this way to dynamically control access to a method, for example, when you want to deny access except during a particular time of day. An example application that uses the getCallerPrincipal andisCallerInRole methods is described in The converter-secure Example: Securing an Enterprise Bean with Programmatic Security.

Propagating a Security Identity (Run-As)

You can specify whether a caller’s security identity should be used for the execution of specified methods of an enterprise bean or whether a specific run-as identity should be used. Figure 41-2 illustrates this concept.

Figure 41-2 Security Identity Propagation

Diagram of security identity propagation from client to intermediate container to target container

In this illustration, an application client is making a call to an enterprise bean method in one EJB container. This enterprise bean method, in turn, makes a call to an enterprise bean method in another container. The security identity during the first call is the identity of the caller. The security identity during the second call can be any of the following options.

Configuring a Component’s Propagated Security Identity

You can configure an enterprise bean’s run-as, or propagated, security identity by using the @RunAs annotation, which defines the role of the application during execution in a Java EE container. The annotation can be specified on a class, allowing developers to execute an application under a particular role. The role must map to the user/group information in the container’s security realm. The @RunAs annotation specifies the name of a security role as its parameter.

Here is some example code that demonstrates the use of the @RunAsannotation.

@RunAs("Admin") public class Calculator { //.... }

You will have to map the run-as role name to a given principal defined on the GlassFish Server if the given roles are associated with more than one user principal.

Trust between Containers

When an enterprise bean is designed so that either the original caller identity or a designated identity is used to call a target bean, the target bean will receive the propagated identity only. The target bean will not receive any authentication data.

There is no way for the target container to authenticate the propagated security identity. However, because the security identity is used in authorization checks (for example, method permissions or with the isCallerInRole method), it is vitally important that the security identity be authentic. Because no authentication data is available to authenticate the propagated identity, the target must trust that the calling container has propagated an authenticated security identity.

By default, the GlassFish Server is configured to trust identities that are propagated from different containers. Therefore, you do not need to take any special steps to set up a trust relationship.

Deploying Secure Enterprise Beans

The deployer is responsible for ensuring that an assembled application is secure after it has been deployed in the target operational environment. If a security view has been provided to the deployer through the use of security annotations and/or a deployment descriptor, the security view is mapped to the mechanisms and policies used by the security domain in the target operational environment, which in this case is the GlassFish Server. If no security view is provided, the deployer must set up the appropriate security policy for the enterprise bean application.

Deployment information is specific to a web or application server.

Previous Contents Next

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices