View Javadoc

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