Java
  Home arrow Java arrow Page 4 - JAAS, Securing J2EE Applications: Securing...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

JAAS, Securing J2EE Applications: Securing Web Components
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2005-08-17

    Table of Contents:
  • JAAS, Securing J2EE Applications: Securing Web Components
  • JAAS: What is it?
  • Subject, Principal and Credentials
  • Implementing the JAAS Security Module
  • 2. Write the CallBackHandler
  • 4. Configure the JAAS policy file
  • Using the JAAS Module to Secure the Web Component

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    JAAS, Securing J2EE Applications: Securing Web Components - Implementing the JAAS Security Module


    (Page 4 of 7 )

    So much for theories. Now, it's time to have a look at the steps involved in implementing JAAS as a module. The implementation I am discussing below has been developed to provide security for a Java Mail application (experimental) that I am currently trying out for my personal use. To write a custom implementation, use the following steps:

    1. Implement the LoginModule.
    2. Write the CallBackHandler.
    3. Providing custom implementations for Principal and Action (this is optional).
    4. Configure the JAAS policy file.
    5. Configure the J2EE Application server.

    Before going into the details of the implementation, the package you need to import  for all the steps is:

     javax.security.

    1.  Implement the LoginModule

    LoginModule is one of the core modules of JAAS. It encapsulates the authentication logic intended for the JAAS framework. To implement LoginModule, one must override the following four methods:

    a.  login():

    This method performs the tasks of fetching the login information and authenticating the user. In short the logic to authenticate a user comes here. Fetching the login information is done as shone below:

                  Callback[] callbacks=new Callbacks[2];

                  Callbacks[0]=new NameCallback(“userModule username:”);

                  Callbacks[0]=new NameCallback(“userModule password:”,false);

                  callbackHandler.handle(calls);

    It is obvious from the above code that login information is fetched using the CallBackHandler. Once the login information is gathered, the login method attempts to connect to the server. If connected, the method returns true. The following snippet shows the procedure.

                      boolean succeeded=false;

                      try{

                        user=((NameCallback)callbacks[0]).getName();

                        passwd=((NameCallback)callbacks[0]).getPassword();

                          props.put(Context.SECURITY_PRINCIPAL,user);

                    props.put(Context.SECURITY_CREDENTIALS,passwd);

                          ctx = new InitialDirContext(props);

                        succeeded =true;

                        }…

                     return succeeded;

    In the try-catch block, it tries to connect to the LDAP server with the gathered information. If the connection succeeds (that is, InitialDirContext() doesn’t throw exceptions), the verification variable is set to true. Here instead of the LDAP server, the database server could also be used as the authenticating server.

      1. commit():

    This method sets the validated username in the session context. The code to populate the subject with the roles and credentials (such as private keys) if any, also goes here. The code is as follows:    

                    if(!succeeded)

                      return false;

                    else

                    {

                      userPrincipal=new userPrincipal(user);

                      if(!subject.getPrincipals().contains(userPrincipal)

                         subject.getPrincipals().add(userPrincipal);

                         …...……

                      commitSucceeded=true;

                      return true;

                      }

    c.  abort():

    This method is called if the LoginContext’s overall authentication fails. This method is also triggered by the runtime if a runtime exception is generated. To do custom processing in case of authentication failure, the code would be as follows: 

        if(!succeed)//this is for authentication failure

              {

                 return false;

              }

    else if(succeeded&&commitSucceeded==false)//this is for //runtime //error

                  {

                        user=null;

                         passwd=null;

                         throw new FailedLoginException(“Exception in Processin”);

    }

    d.  logout():

    Whenever user logs out, this method is called. So if you want to unset some credentials or release the resources held by the user, you can do it here. The code that I used is as follows:           

                           subject.getPrincipals.remove(userPrincipal);

                  succeeded=false;

                  succeeded=commitSucceeded;

                  user=null;

                  if(password!=null)

                    password=null;

                 

                  userPrincipal=null;

                  return true;

    The class embedding the above code is:

                 public class MyLoginModule implements LoginModule

           {

           }

    More Java Articles
    More By A.P.Rajshekhar


       · HiThank you for reading. Hope it was helpful. Please comment.A.P.Rajshekhar
       · Very well structured and helpful for a newbie like me. The "almost" is because I...
       · found it very helpful, it only lacks references to specific server aplication...
       · Thhanks for your comments. Since here the context is Web Applicatin, so there is no...
       · Hi thanks for that article, it helped clear a lot of confusion i was having about...
       · Hi liked the explanation of JAASLoginFilter. But what i am not able to understand...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway