03.05.2015 Views

IBM WebSphere V5.0 Security - CGISecurity

IBM WebSphere V5.0 Security - CGISecurity

IBM WebSphere V5.0 Security - CGISecurity

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

8.2.2 Servlet security methods<br />

The Servlet 2.3 specification defines three methods that allow programmatic<br />

access to the caller’s security information of HttpServletRequest interface.<br />

Important: The methods getRemoteUser() and getUserPrincipal() return<br />

null as a result even if the user is logged in, unless the servlet or the JSP<br />

itself is secured.<br />

►<br />

►<br />

►<br />

String getRemoteUser()<br />

The getRemoteUser method returns the user name that the client used to log<br />

in.<br />

String user = request.getRemoteUser()<br />

Boolean isUserInRole(String roleName)<br />

The isUserInRole method allows the developer to perform additional checks<br />

on the authorization rights of a user which are not possible, or more difficult,<br />

to perform through the deployment descriptor of the servlet.<br />

if (request.isUserInRole("Manager")) {<br />

// the user is in the manager role<br />

// ...<br />

}<br />

java.security.Principal getUserPrincipal()<br />

The getUserPrincipal method allows the developer to get the name of the<br />

current caller. To do this, you need to call getName() on the<br />

java.security.Principal object returned.<br />

Principal principal=request.getUserPrincipal();<br />

String username=principal.getName();<br />

Sample usage of security methods<br />

The following example is a modified code snippet from the ITSOBank sample<br />

application. You can find similar code in the TransferServlet.java in the doPost()<br />

method. For more details, check the comments in the source below or in the<br />

sample application.<br />

Example 8-2 Sample code using the servlet security methods<br />

// getting the environment variables for restricted role<br />

// and for maximum transferable amount<br />

restrictedRole=(String)environment.lookup("RestrictedRole");<br />

maxWebTransferAmount=(Integer)environment.lookup("MaximumWebTransferAmount");<br />

// checking if the user is restricted to a certain amount of transfer<br />

182 <strong>IBM</strong> <strong>WebSphere</strong> <strong>V5.0</strong> <strong>Security</strong> Handbook

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

Saved successfully!

Ooh no, something went wrong!