Articles
Java Platform, Enterprise Edition
|
| By Shing Wai Chan, July 2006 |
|
| |
Security is very important in the enterprise environment. In the Java EE 5 / GlassFish environment, you can achieve security using the following options:
This article discusses authentication and authorization. References [ 1], [ 2], and [ 3] discuss how to set up the SSL environment in both clients and servers for Enterprise JavaBeans and Web Services. Message level security for Web Services will be discussed in a future article.
Authentication verifies the identity of a given user, typically by requiring the user to enter a username and password. In the Java EE environment, authentication is associated with a realm. The realm can store user identity information in many ways, including files, LDAP directories, and even databases accessed through JDBC (see Reference [4]). It can also work with Solaris PAM (Pluggable Authentication Modules) framework.
Authorization grants access control permissions based not only on what software is running but also on identity of the authenticated user who is running it. Each time a user logs in, he or she is granted a set of permissions for each application.
Prior to Java EE 5, if you wanted to use authorization for a given application, you needed to specify authorization information in the application deployment descriptors ejb-jar.xml or web.xml. One of the main focuses of Java EE 5 is to simplify development of Java EE applications. Starting in Java EE 5, developers can specify annotations in Java source files instead of putting metadata in deployment descriptors. Annotations simplify the development of Java EE applications, shortening development cycles and reducing the total cost of ownership.
JSR 250 (see Reference [5]) defines common annotations for the Java platform. This article discusses the security annotations defined in JSR 250 and demonstrates how to use them for securing an application with authentication and authorization.
| - | Basic Definitions and Examples |
| - | Examples of Invalid Use of Annotations |
| - | Inheritance of Security Annotations |
| - | Using Deployment Descriptors |
| - | Conclusion |
| - | References |
| - | Acknowledgments |
| |
An annotation is a special kind of modifier, and can be used anywhere that other modifiers can be used. Annotations consist of an at-sign (
@) followed by an annotation type and a parenthesized list of element-value pairs.
This section discusses the common security annotations defined in JSR 250. Five security annotations are defined (see reference [6]):
javax.annotation.security.PermitAll
javax.annotation.security.DenyAll
javax.annotation.security.RolesAllowed
javax.annotation.security.DeclareRoles
javax.annotation.security.RunAs
The annotations
@PermitAll,
@DenyAll and
@RolesAllowed are defined for specifying permissions of EJB business methods.
@DeclareRoles and
@RunAs are TYPE-level annotations defined for roles-related metadata.
For a web module, you still need to specify a
<security-constraint> in the web.xml application deployment descriptor in order to have authorization constraints, just as you did in J2EE 1.4. In the Java EE 5 environment, the permissions-related annotations are only defined for EJB modules. The following table summarizes the basic usage of these annotations. More details can be found in the JSR 250 specification (
reference [5]).
|
Annotations |
Target |
EJB or its super classes |
Servlet or web libraries |
Descriptions |
|
|---|---|---|---|---|---|
|
TYPE |
METHOD |
||||
|
|
X |
X |
X |
|
Indicates that the given method or all business methods of the given EJB are accessible by everyone. |
|
|
|
X |
X |
|
Indicates that the given method in the EJB cannot be accessed by anyone. |
|
|
X |
X |
X |
|
Indicates that the given method or all business methods in the EJB can be accessed by users associated with the list of roles. |
|
|
X |
|
X |
X |
Defines roles for security checking. To be used by EJBContext.isCallerInRole, HttpServletRequest.isUserInRole, and WebServiceContext.isUserInRole. |
|
|
X |
|
X (not for non-EJB super classes) |
X (for servlet only) |
Specifies the run-as role for the given components. |
Notes:
@PermitAll,
@DenyAll, and
@RolesAllowed, a class-level annotation applies to a class and a method-level annotation applies to a method. Method-level annotation overrides the behavior of class level annotation.
@Stateless
@RolesAllowed("javaee")
public class HelloEJB implements Hello {
@PermitAll
public String hello(String msg) {
return "Hello, " + msg;
}
public String bye(String msg) {
return "Bye, " + msg;
}
}
|
hello() method is accessible by everyone, and the
bye() method is accessible by users of role javaee.
@DeclareRoles annotation defines a list of roles to be used by a given component. In the Java EE 5 environment, you can look up the resource by using the
@javax.annotation.Resource and verify whether the user is of the given role by invoking the following APIs:
|
Component |
API to check role |
|---|---|
|
EJB |
|
|
Servlet |
|
|
Web Service |
|
@PermitAll,
@DenyAll, and
@RolesAllowed annotations allow you to implement most of the authorization decision, you need the
@DeclareRoles annotation to help you to accomplish more complicated logic.
hello method is to be accessible by a user who is in role A and who is not in role B at the same time. The following code clip achieves this goal:
@Stateless
@DeclaresRoles({"A", "B"})
public class HelloEJB implements Hello {
@Resource private SessionContext sc;
public String hello(String msg) {
if (sc.isCallerInRole("A") && !sc.isCallerInRole("B")) {
...
} else {
...
}
}
}
|
@DeclareRoles in the servlet, filter, and tag libraries. Annotations are not supported in JSP pages.
| |
@DenyAll,
@PermitAll,
@RolesAllowed cannot apply to the same method.
@PermitAll
@DenyAll
public String hello()
|
@RolesAllowed annotations cannot apply to the same methods.
@RolesAllowed("javaee")
@RolesAllowed("j2ee")
public String hello()
|
@RolesAllowed annotation as a list, as shown in this example:
@RolesAllowed({"javaee", "j2ee"})
public String hello()
|
| |
This section discusses the inheritance of security annotations. Because the default behavior of methods in GlassFish is
@PermitAll, this annotation is omitted in the discussion below for simplicity.
The general rule of inheritance is as follows:
@RunAs annotation, only those in the leaves of the hierarchy will be considered.
@DeclareRoles annotation, inheritance is additive from the inheritance hierarchies.
The following examples illustrate these rules of inheritance of annotations.
Example: In the following hierarchies of EJBs:
The following method permissions apply:
Example: In the following hierarchies of Servlets:
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||