10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Checking Caller Identity Programmatically<br />

In general, security management should be enforced by the container in a manner that is<br />

transparent to the web component. <strong>The</strong> security API described in this section should be used<br />

only in the less frequent situations in which the web component methods need to access the<br />

security context information.<br />

■ <strong>The</strong> HttpServletRequest interface provides the following methods that enable you to<br />

access security information about the component’s caller:getRemoteUser: Determines the<br />

user name with which the client authenticated. If no user has been authenticated, this<br />

method returns null.<br />

■ isUserInRole: Determines whether a remote user is in a specific security role. If no user has<br />

been authenticated, this method returns false. This method expects a String user<br />

role-name parameter.<br />

You can use either the @DeclareRoles annotation or the element<br />

with a sub-element in the deployment descriptor to pass the role name to this<br />

method. Using security role references is discussed in “Declaring and Linking Role<br />

References” on page 842.<br />

■ getUserPrincipal: Determines the principal name of the current user and returns a<br />

java.security.Principal object. If no user has been authenticated, this method returns<br />

null.<br />

Your application can make business logic decisions based on the information obtained using<br />

these APIs.<br />

<strong>The</strong> following is a code snippet from an index.jsp file that uses these methods to access<br />

security information about the component’s caller.<br />

<br />

<br />

<br />

<br />

/title><br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Checking Caller Identity Programmatically<br />

Chapter 30 • SecuringWeb Applications 841

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!