|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features Valve, JAAS and Filter in Tomcat
Their usages and relationships
By: Zhiyong Li
Aug. 14, 2011 06:15 AM
Tomcat is a widely popular lightweight application server. When securing Tomcat web applications, Valve, JAAS and Filter are used in various scenarios. The challenges for developers are when to use each of these methods and how to integrate them together if more than one method is chosen. For example, the WebSeal agent discussed in the article [1] uses Valve. If a customer needs to integrate WebSeal and its own JAAS-based authentication module, they will need to know how to configure Tomcat to use both the WebSeal Agent Valve and the JAAS module and how to pass information between them. In this article, we will explain the concepts of Valve, JAAS and Filter, and their relationships such as the order that they get called. Through an example application, we will explain how you can use them together and pass information among them for an authentication process. How to configure and run the example application using Tomcat 7 will also be discussed. The Concepts
For example, Listing 1 is a simple Valve. (Listings 1 - 5 can be downloaded here.) That Valve also has a method that may call an external policy server to verify the user. It may retrieve the user information passed in as a token in the HTTP header field such as the one used in [1]. You can ignore the ThreadLocale part in the Listing 1, which will be discussed later.
JAAS When JAASRealm is used, a user will need to provide a login module and the appropriate configurations: a configuration file and a security configuration in the web.xml. Once it's configured properly, the login module will be called and managed by the Tomcat. Listing 2 has a sample JAAS login module. This login module will use a callback to get the userid and password. It will check whether the password is the reverse of the userid. If the check succeeds, it will create a principal based on the username and assign the role "jvalve" to the principal. Filter
Listing 3 shows an example filter. This filter simply calculates the time used to complete the request. The Relationships
Even though JAAS is the official method for providing security implementations, Valve and filter have been used to implement securities frequently. Especially in the Tomcat case, it is relatively hard to get a Subject in the application code (JBoss has a SharedState, WebLogic and WebSphere all provide a static method to make the "Subject" available for the application code). Implementations may use Valve for the authentication and then set the authenticated principal on the request for the filter and application code to use. However, it's also important to understand their differences especially if you want to use them together.
Because of the above differences, it becomes a challenge if you want to use all three of them together and pass information among them. Consider the following example. Tivilo WebSeal provides a Valve engine that will set an authenticated user on the request. Your company has been using the JAAS module for the additional authentication purpose. The JAAS module will want to retrieve that authenticated user from the Valve. Tomcat will not help in this case. In the next session, we'll discuss a ThreadLocale [6] solution to address this issue. The Sample Application In Listing 1, we have set the "key" to the ThreadLocale. In Listing 2 of the JAAS code, we have code to retrieve the "key" from the ThreadLocale (it is commented out). We define the value of the password as the reverse of the username plus the key (it's commented out in Listing 2 as well). The web.xml, which protects the secured resource and defines the filter, is listed in Listing 5. The JAAS login module can be defined in the following login.config file: MyAccess { This same ThreadLocale concept can be used if a user wants to pass information from the JAAS login module to the filter. Issues with Tomcat 6 For example, you have the following code in your Valve's "invoke()" method: GenericPrincipal genericprincipal = new This problem does not occur at Tomcat 7. It should note that the construct for GenericPrincipal has been changed from 6 to 7. In Tomcat 7, you will do: GenericPrincipal genericprincipal = new Configure and Run the Sample Application To run the application, complete the following process:
<Context path="/loginmodule">
-Djava.security.auth.login.config=%CATALINA_BASE%\conf\login.config
Security Valve: invoke SampleLoginModule - initialize - sharedState: {} In the above output, you may notice that the Valve works like filters but is called before and after JAAS and the filters. We use the password "velavjsas", which is the reverse of username "jvalve" plus the key that is passed from the Valve to the JAAS module using the ThreadLocale. Conclusion The concept discussed here can also be used in other Tomcat based application servers such as the VMware vFabric tc Server [7]. Resources Reader Feedback: Page 1 of 1
SOA World Latest Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||