KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > cmp > transaction > CMPAuthenticator


1 package example.cmp.transaction;
2
3 import java.util.*;
4 import java.security.*;
5 import com.caucho.util.*;
6 import com.caucho.server.security.AbstractAuthenticator;
7
8 import javax.ejb.*;
9 import javax.naming.*;
10
11 /**
12  * A custom Authenticator that looks up users in the <code>Student</code>
13  * CMP bean.
14  */

15 public class CMPAuthenticator extends AbstractAuthenticator {
16   /**
17    * Home Interface for the Student bean.
18    */

19   StudentHome studentHome = null;
20
21   /**
22    * For more sophisticated sites, this would allow cookie-based
23    * authentication so the user doesn't always have to login.
24    *
25    * <p>Since Hogwarts student will tend to access the web from
26    * public labs, we won't use cookies.
27    */

28   public Principal authenticateCookie(String cookieValue)
29   {
30     return null;
31   }
32
33   /**
34    * Authenticate for the user and password.
35    */

36   public Principal authenticate(String user, String password)
37   {
38     Student student = null;
39     try {
40       student = getStudentHome().findByName(user);
41       
42       if (student.getPassword().equals(password))
43         return student;
44     } catch (NamingException e) {
45       e.printStackTrace();
46     } catch (FinderException e) {
47       e.printStackTrace();
48     }
49
50     return null;
51   }
52
53   /**
54    * Returns the student home, looking it up in the JNDI context
55    * if necessary.
56    */

57   private StudentHome getStudentHome()
58     throws NamingException
59   {
60     if (studentHome == null) {
61       // The JNDI context containing EJBs
62
Context cmp = (Context) new InitialContext().lookup("java:comp/env/cmp");
63
64       // get the student bean stub
65
studentHome = (StudentHome) cmp.lookup("transaction_student");
66     }
67     
68     return studentHome;
69   }
70     
71
72   /**
73    * If we were storing cookies, this would store the <cookie, user> pair
74    * in some sort of persistent storage.
75    */

76   public boolean updateCookie(Principal user, String cookieValue)
77   {
78     return true;
79   }
80
81  /**
82    * Returns <code>true</code> if the user is in the specified role.
83    * For this example, simlpy returns true if the user exists.
84    */

85   public boolean isUserInRole(Principal user, String role)
86   {
87     return user != null;
88   }
89 }
90
91
Popular Tags