WAS v8.5 > Reference > Administrator best practicesJava Authentication and Authorization Service authorization
Java 2 security architecture uses security policies to specify which access rights are granted to running code. Permissions are granted based on code characteristics including...
- where the code is coming from
- whether it is digitally signed, and by whom.
Authorization of the Java Authentication and Authorization Service (JAAS) augments the existing code-centric access controls with new user-centric access controls. Permissions are granted based on what code is running and who is running it.
A subject is created to represent an authenticated user. A subject is comprised of a set of principals, where each principal represents an identity for that user. We can grant permissions in the policy to specific principals. After the user is authenticated, the application can associate the subject with the current access control context. For each subsequent security-checked operation, the Java runtime automatically determines whether the policy grants the required permission to a specific principal only. If so, the operation is supported if the subject associated with the access control context contains the designated principal only.
Associate a subject with the current access control context by calling the static doAs method from the subject class, passing it an authenticated subject and either method...
- java.security.PrivilegedAction
- java.security.PrivilegedExceptionAction
The doAs method associates the provided subject with the current access control context and then invokes the run method from the action. The run method implementation contains all the code that ran as the specified subject. The action runs as the specified subject.
In the Java 2 Platform, Enterprise Edition (J2EE) programming model, when invoking the EJB method from an enterprise bean or servlet, the method runs under the user identity that is determined by the run-as setting. The J2EE v1.4 Specification does not indicate which user identity to use when invoking an enterprise bean from a Subject.doAs action block within either the EJB code or the servlet code. A logical extension is to use the proper identity specified in the subject when invoking the EJB method within the Subject doAs action block.
Letting the Subject.doAs action overwrite the run-as identity setting is a way to integrate the JAAS programming model with the J2EE run-time environment. However, JAAS introduced an issue into the SDK, JTE V1.3 or later when integrating the JAAS v1.0 or later implementation with the Java 2 security architecture. A subject, which is associated with the access control context is cut off by a doPrivileged call when a doPrivileged call occurs within the Subject.doAs action block. Until this problem is corrected, no reliable and run-time efficient way is available to guarantee the correct behavior of Subject.doAs action in a J2EE run-time environment.
The problem can be explained better with the following example:
Subject.doAs(subject, new java.security.PrivilegedAction() {
Public Object run() {
Subject is associated with the current thread context
java.security.AccessController.doPrivileged( new
java.security.PrivilegedAction() {
Object run() {
Subject was cut off from the current
// thread context
return null;
}
});
// Subject is associated with the current thread context
return null;
}
});In the previous code example, the Subject object is associated with the context of the current thread. Within the run method of a doPrivileged action block, the Subject object is removed from the thread context. After leaving the doPrivileged block, the Subject object is restored to the current thread context. Because doPrivileged blocks can be placed anywhere along the running path and instrumented quite often in a server environment, the run-time behavior of a doAs action block becomes difficult to manage.
To resolve this difficulty, WebSphere Application Server provides a WSSubject helper class to extend the JAAS authorization to a J2EE EJB method invocation, as described previously. The WSSubject class provides static doAs and doAsPrivileged methods that have identical signatures to the subject class. The WSSubject.doAs method associates the Subject to the currently running thread. The WSSubject.doAs and WSSubject.doAsPrivileged methods then invoke the corresponding Subject.doAs and Subject.doAsPrivileged methods. The original credential is restored and associated with the running thread upon leaving the WSSubject.doAs and WSSubject.doAsPrivileged methods.
The WSSubject class is not a replacement of the subject object, but rather a helper class to ensure consistent run-time behavior as long as an EJB method invocation is a concern.
The following example illustrates the run-time behavior of the WSSubject.doAs method:
WSSubject.doAs(subject, new java.security.PrivilegedAction() {
Public Object run() {
// Subject is associated with the current thread context
java.security.AccessController.doPrivileged( new
java.security.PrivilegedAction() {
public Object run() {
// Subject was cut off from the current thread
// context.
return null;
}
});
// Subject is associated with the current thread context
return null;
}
});The Subject.doAs and Subject.doAsPrivileged methods are not integrated with the J2EE run-time environment. EJB methods that are invoked within the Subject.doAs and Subject.doAsPrivileged action blocks run under the identity specified by the run-as setting and not by the subject identity.
- The Subject object generated by the WSLoginModuleImpl instance and the WSClientLoginModuleImpl instance contains a principal that implements the WSPrincipal interface. Using the getCredential method for a WSPrincipal object returns an object that implements the WSCredential interface. We can also find the WSCredential object instance in the PublicCredentials list of the subject instance. Retrieve the WSCredential object from the PublicCredentials list instead of using the getCredential method.
- The getCallerPrincipal method for the WSSubject class returns a string that represents the caller security identity. The return type differs from the getCallerPrincipal method of the java.security.Principal EJBContext interface.
- The Subject object generated by the Java 2 Connector (J2C) DefaultPrincipalMapping module contains a resource principal and a PasswordCredentials list. The resource principal represents the RunAs identity.
For more information, see J2EE connector security.
Related concepts:
Java Authentication and Authorization Service
Java EE connector security
Related
Authenticate users