Home

This Chapter
-Chapter 13: Securing Struts Applications
-Principals and Roles
-Writing Security Policy
-Authentication Methods
-Hiding Resources
-Struts Security Configuration
-Programmatic Security
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

Programmatic Security

Even though configuring the deployment descriptor and specifying roles in the tomcat-users.xml file means that you do not need to write Java code, sometimes coding is inevitable. For example, you might want to record all the users that log in. The javax.servlet.http.HttpServletRequest interface provides several methods that enable you to have access to portions of the user’s login information. These methods are getAuthType, isUserInRole, getPrincipal, and getRemoteUser. The methods are explained in the following subsections.

The getAuthType Method

The getAuthType method has the following signature.

public String getAuthType()

This method returns the name of the authentication scheme used to protect the servlet. The return value is one of the following values: BASIC_AUTH, FORM_AUTH, CLIENT_CERT_AUTH, and DIGEST_AUTH. It returns null if the request was not authenticated.

The isUserInRole Method

Here is the signature of the isUserInRole method.

public boolean isUserInRole(String role)

This method indicates whether the authenticated user is included in the specified role. If the user has not been authenticated, the method returns false.

The getUserPrincipal Method

The signature of getUserPrincipal is as follows.

public java.security.Principal getUserPrincipal()

This method returns a java.security.Principal object containing the name of the current authenticated user. If the user has not been authenticated, the method returns null.

The getRemoteUser Method

The getRemoteUser method has the following signature.

public String getRemoteUser()

This method returns the name of the user making this request, if the user has been authenticated. Otherwise, it returns null. Whether the user name is sent with each subsequent request depends on the browser and type of authentication.

Note

You can extend the org.apache.struts.action.RequestProcessor class if you have special needs for security. Check this out in Chapter 22, “How Struts Works”.

Previous
Next