View Javadoc

1   // ========================================================================
2   // Copyright (c) 2002-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.jaas.spi;
15  
16  import java.sql.Connection;
17  import java.sql.DriverManager;
18  import java.util.Map;
19  
20  import javax.security.auth.Subject;
21  import javax.security.auth.callback.CallbackHandler;
22  
23  import org.eclipse.jetty.util.Loader;
24  import org.eclipse.jetty.util.log.Log;
25  
26  
27  
28  /* ---------------------------------------------------- */
29  /** JDBCLoginModule
30   * <p>JAAS LoginModule to retrieve user information from
31   *  a database and authenticate the user.
32   *
33   * <p><h4>Notes</h4>
34   * <p>This version uses plain old JDBC connections NOT
35   * Datasources.
36   *
37   * <p><h4>Usage</h4>
38   * <pre>
39   * </pre>
40   *
41   * @version 1.0 Tue Apr 15 2003
42   */
43  public class JDBCLoginModule extends AbstractDatabaseLoginModule
44  {
45      private String dbDriver;
46      private String dbUrl;
47      private String dbUserName;
48      private String dbPassword;
49  
50      
51      /** 
52       * Get a connection from the DriverManager
53       * @see AbstractDatabaseLoginModule#getConnection()
54       * @return the connection for this datasource
55       * @throws Exception
56       */
57      public Connection getConnection ()
58      throws Exception
59      {
60          if (!((dbDriver != null)
61                  &&
62                  (dbUrl != null)))
63              throw new IllegalStateException ("Database connection information not configured");
64          
65          if(Log.isDebugEnabled())Log.debug("Connecting using dbDriver="+dbDriver+"+ dbUserName="+dbUserName+", dbPassword="+dbUrl);
66          
67          return DriverManager.getConnection (dbUrl,
68                  dbUserName,
69                  dbPassword);
70      }
71     
72     
73      
74      /* ------------------------------------------------ */
75      /** Init LoginModule.
76       * Called once by JAAS after new instance created.
77       * @param subject 
78       * @param callbackHandler 
79       * @param sharedState 
80       * @param options 
81       */
82      public void initialize(Subject subject,
83                             CallbackHandler callbackHandler,
84                             Map sharedState,
85                             Map options)
86      {
87          try
88          {
89              super.initialize(subject, callbackHandler, sharedState, options);
90              
91              //get the jdbc  username/password, jdbc url out of the options
92              dbDriver = (String)options.get("dbDriver");
93              dbUrl = (String)options.get("dbUrl");
94              dbUserName = (String)options.get("dbUserName");
95              dbPassword = (String)options.get("dbPassword");
96  
97              if (dbUserName == null)
98                  dbUserName = "";
99  
100             if (dbPassword == null)
101                 dbPassword = "";
102             
103             if (dbDriver != null)
104                 Loader.loadClass(this.getClass(), dbDriver).newInstance();
105         }
106         catch (ClassNotFoundException e)
107         {
108             throw new IllegalStateException (e.toString());
109         }
110         catch (InstantiationException e)
111         {
112             throw new IllegalStateException (e.toString());
113         }
114         catch (IllegalAccessException e)
115         {
116             throw new IllegalStateException (e.toString());
117         }
118     }
119 }