· Use jaasrealm in Tomcat

Source: Internet
Author: User
Document directory
  • Ntroduction
  • Quick Start
  • Realm element attributes
  • Example
  • Additional Notes
 

Ntroduction

JaasrealmIs an implementation of the Tomcat 4RealmInterface that authenticates users through the Java Authentication & Authorization Service (JAAS) framework, a Java package that is available as an optional package in Java 2 SDK 1.3 and is fully integrated as of SDK 1.4.

Using jaasrealm gives the developer the ability to combine practically any conceivable security realm with Tomcat's CMA.

Jaasrealm is prototype for Tomcat of the proposed JAAS-based J2EE Authentication Framework for J2EE v1.4, based on the JCP specification request 196 to enhance container-managed security and promote 'able gable' authentication mechanism whoisms whose implementations wocould be container-independent.

Based on the JAAS login module and principal (seejavax.security.auth.spi.LoginModuleAndjavax.security.Principal), You can develop your own security mechanisms or wrap another third-party mechanisms for integration with the CMA as implemented by Tomcat.

Quick Start

To set up tomcat to use jaasrealm with your own JAAS login module, you will need to follow these steps:

  1. Write your own loginmodule, user and role classes based on JAAS (see the JAAS authentication tutorial and the JAAS login module developer's guide) to be managed by the JAAS login context (javax.security.auth.login.LoginContext). When developing your loginmodule, note that jaasrealm's built-inCallbackHandlerOnly recognizesNameCallbackAndPasswordCallbackAt present.
  2. Although not specified in JAAS, you shoshould create seperate classes to distinguish between users and roles, extendingjavax.security.Principal, So that Tomcat can tell which principals returned from your login module are users and which are roles (seeorg.apache.catalina.realm.JAASRealm). Regardless, the first principal returned isAlwaysTreated as the user principal.
  3. Place the compiled classes on Tomcat's classpath
  4. Set up a login. config file for Java (see JAAS loginconfig file) and tell Tomcat where to find it by specifying its location to the JVM, for instance by setting the environment variable:JAVA_OPTS=-DJAVA_OPTS=-Djava.security.auth.login.config==$CATALINA_HOME/conf/jaas.config
  5. Configure your security-constraints in your web. XML for the resources you want to protect
  6. Configure the jaasrealm module in your server. xml
  7. Restart Tomcat 4 if it is already running.
Realm element attributes

To configure jaasrealm as for Step 6 abve, you create<Realm>Element and nest it in your$CATALINA_HOME/conf/server.xmlFile within your<Engine>Node. the following attributes are supported by this implementation:

Attribute Description
className

The fully qualified Java class name of this realm implementation. YouMustSpecify the value"org.apache.catalina.realm.MemoryRealm"Here.

debug

The level of debugging detail logged by this realm to the associated logger. Higher numbers generate more detailed output. If not specified, the default debugging detail level is zero (0 ).

appName

The name of the application as configured in your login configuration file (JAAS loginconfig ).

userClassNames

A comma-seperated List of the names of the classes that you have made for your userPrincipals.

roleClassNames

A comma-seperated List of the names of the classes that you have made for your rolePrincipals.

useContextClassLoader

Instructs jaasrealm to use the context class loader for loading the user-specifiedLoginModuleClass and associatedPrincipalClasses. The default value istrue, Which is backwards-compatible with the way Tomcat 4 works. To load classes using the container's classloader, specifytrue.

Example

Here is an example of how your server. xml snippet shocould look.

<Realm className="org.apache.catalina.realm.JAASRealm"                                 appName="MyFooRealm"           userClassNames="org.foobar.realm.FooUser"            roleClassNames="org.foobar.realm.FooRole"                       debug="99"/>

It is the responsibility of your login module to create and save user and role Objects representing principals for the user (javax.security.auth.Subject). If your login module doesn't create a user object but also doesn't throw a login exception, then the Tomcat CMA will break and you will be left at the http: // localhost: 8080/MyApp/j_security_check URI or at some other unspecified location.

The flexibility of the JAAS approach is two-fold:

  • You can carry out whatever processing you require behind the scenes in your own login module.
  • You can plug in a completely different loginmodule by changing the configuration and restarting the server, without any code changes to your application.
Additional Notes
  • When a user attempts to access a protected resource for the first time, Tomcat 4 will callauthenticate()Method of thisRealm. Thus, any changes you have made in the security mechanisms directly (new users, changed passwords or roles, etc.) will be immediately reflected.
  • Once a user has been authenticated, the user (and his or her associated roles) are cached within Tomcat for the duration of the user's login. (For form-based authentication, that means until the session times out or is invalidated; for basic authentication, that means until the user closes their browser ). any changes to the security information for an already authenticated user willNotBe reflected until the next time that user logs on again.
  • Debugging and exception messages logged by thisRealmWill be recorded byLoggerThat is associated with our surroundingContext,Host, OrEngine. By default, the corresponding logger will create a log file in$CATALINA_HOME/logsDirectory.
  • As with otherRealmImplementations, digested passwords are supported if<Realm>Element inserver.xmlContainsdigestAttribute; jaasrealm'sCallbackHandlerWill digest the password prior to passing it back toLoginModule

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.