| Sun GlassFish Enterprise Server |
Programmatic security is used by security aware applications when
declarative security alone is not sufficient to express the security
model of the application. Programmatic security consists of the
following methods of the HttpServletRequest interface:
login logout getRemoteUser isUserInRole getUserPrincipal The login methods allow an application to perform username and password collection (as an alternative to Form-Based Login), and to instigate authentication of the request caller by the container from within an unconstrained request context. The logout method is provided to allow an application to reset the caller identity of a request.
The getRemoteUser method returns the name of the
remote user (that
is, the caller) associated, by the container, with the request.
The isUserInRole method determines if the remote user
(that is, the
caller) associated with the request is in a specified security role.
The getUserPrincipal method determines the principal
name of the
remote user (that is, the caller) and returns a java.security.Principal
object corresponding to the remote user. Calling the getName method on
the Principal returned by getUserPrincipal returns the
name of the
remote user. These APIs allow servlets to make business logic decisions
based on the information obtained.
The servlet performs a programmatic login, providing the username and
password obtained from the user. This is an alternative to
form-based-login in previous versions of servlets. Calls are made to
getRemoteUser(), getUserPrincipal() and isUserInRole(),
before login,
after login, and after logout. Only when logged in (as javaee6user -
the
configured user), the calls return non-null values.
Following are the instructions for building, deploying, and running
this sample application.
app_dir is the sample application base
directory: samples_install_dir/javaee6/security/programmatic-login.Change directory to app_dir.
app_dir> ant
The launch target opens the browser. Enter the username/password
(javaee6user/abc123 - the configured file user/password) and check the
displayed values. It can be observed that before calling login()
and
after calling logout(), the values for isUserInRole
is
false,getRemoteUser returns null, while after calling login(),
the
methods return proper values.
clean to undeploy the sample
application and to
remove the temporary directories like build and dist.
app_dir> ant
clean
Perform the following steps to build, deploy, and run the application using NetBeans IDE:
samples_install_dir/javaee6/security/ directory, select programmatic-login, and click Open Project.programmtic-login and select Run to build, deploy, and run the project.If you have problems when running the application, refer the troubleshooting document.
Copyright 2009 Sun Microsystems, Inc. All rights reserved.