Java
  Home arrow Java arrow Page 5 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 26
    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 - 2. Write the CallBackHandler


    (Page 5 of 7 )

    To gather users’ authentication information, LoginModule uses a   javax.security.auth.callback.CallbackHandler. An application implements this interface and passes it to the LoginContext. The LoginContext then forwards it to the underlying LoginModules. So data gathering can be decoupled from authentication implementations. One part of the CallbackHandler implementation is the type of data used for validating the user. Below is the code for the CallbackHandler used by our application.

    To implement CallbackHandler a single method has to be overridden- handle(). Let's see how its done.

    Create a class that implements the CallbackHandler

    class MyCallBackHandler implements CallbackHandler

    {

              …..

    }

    Then override the handle() method

    if(callback[i] instanceof NameCallback)

    {       

                            NameCallback nc = (PasswordCallback)callbacks[i];

    //do the needful to get the user name such as a login page or a terminal   //based userid prompter

                ………

                nc.setPassword(password.toCharArray());

           }

            if(callback[i] instanceof PasswordCallback)

            {      

                    PasswordCallback pc= (PasswordCallback)callbacks[i];

    //do the needful to get the password such as a login page or a terminal   //based password prompter. Just like the one done for user name.

                ………

                pc.setPassword(password.toCharArray());

            }

    That’s all for the CallbackHandler.

    3.  Providing custom implementations for Principal and Action (this is optional)

    This is an optional step. This step is useful for storing the user name and comparing it with a new login.

    Just as in the pvious step, start by creating a class.

    public class userPrincipal implements Principal, java.io.Serializable

            {

                   …….

            }

    Since I am using this class to compare between two userPrincipals, I have done it in equals method.

    public boolean equals(Object o)

    {

       if(o==null)

            return false;

      

      if(this==o)

            return true;

     

      if(!(o instanceof userPrincipal))

            return false;

           

      userPrincipal that=(userPrincipal)o;

     if (this.getName().equals(that.getName()))

            return true;

     return false;

     }

    The code first checks for null object. Then it checks for equality of the object (not value). Once that is done, it assigns the object to an object of type userPrincipal and checks the equality of the value contained in it. The only method to be overridden is getName(). In our case it just returns the name contained in the passed uesrPrincipal object.

    public String getName()

    {

     return userPrincipal.getName();

    }

    Next is the implementation for PrivilagedAction. By doing this we can control the access of the resource on the basis of permissions in real time. 

    Create a custom implementation by implementing the PrivilagedAction interface.

    public class userAction implements PrivilagedAction

    {

     …..

    }

    Then override the run() method as shown below. Implementations can differ.

    public Object run()

    {

            File f=new File(“tips.html”);

            if(!f.exists())

    <>               System.out.println(“File does not exists in user directory”);

    <>//do other needful. In real environment the out put wouldn’t be given like above

    <>    return null;

    <>}

    Here we are just checking for the existence of the file. The exists() method returns true only if the file object has the required permissions. In this case it has permissions only in the user’s home directory.

    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...
       · put the rigth code or dont put anything, that snippets of code was writed with your...
       · The content was easy to understand. I habe 1 question in this. I want my application...
     

    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-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT