View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.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   * JDBCLoginModule
34   * <p>
35   * JAAS LoginModule to retrieve user information from
36   * a database and authenticate the user.
37   * <h1>Notes</h1>
38   * <p>This version uses plain old JDBC connections NOT
39   * Datasources.
40   */
41  public class JDBCLoginModule extends AbstractDatabaseLoginModule
42  {
43      private static final Logger LOG = Log.getLogger(JDBCLoginModule.class);
44  
45      private String dbDriver;
46      private String dbUrl;
47      private String dbUserName;
48      private String dbPassword;
49  
50      /**
51       * Get a connection from the DriverManager
52       * @see AbstractDatabaseLoginModule#getConnection()
53       * @return the connection for this datasource
54       * @throws Exception if unable to get the connection
55       */
56      public Connection getConnection ()
57      throws Exception
58      {
59          if (!((dbDriver != null)
60                  &&
61                  (dbUrl != null)))
62              throw new IllegalStateException ("Database connection information not configured");
63  
64          if(LOG.isDebugEnabled())LOG.debug("Connecting using dbDriver="+dbDriver+"+ dbUserName="+dbUserName+", dbPassword="+dbUrl);
65  
66          return DriverManager.getConnection (dbUrl,
67                  dbUserName,
68                  dbPassword);
69      }
70  
71  
72  
73      /* ------------------------------------------------ */
74      /** 
75       * Init LoginModule.
76       * <p>
77       * Called once by JAAS after new instance created.
78       * 
79       * @param subject the subject
80       * @param callbackHandler the callback handler
81       * @param sharedState the shared state map
82       * @param options the options map
83       */
84      public void initialize(Subject subject,
85                             CallbackHandler callbackHandler,
86                             Map<String,?> sharedState,
87                             Map<String,?> options)
88      {
89          try
90          {
91              super.initialize(subject, callbackHandler, sharedState, options);
92  
93              //get the jdbc  username/password, jdbc url out of the options
94              dbDriver = (String)options.get("dbDriver");
95              dbUrl = (String)options.get("dbUrl");
96              dbUserName = (String)options.get("dbUserName");
97              dbPassword = (String)options.get("dbPassword");
98  
99              if (dbUserName == null)
100                 dbUserName = "";
101 
102             if (dbPassword == null)
103                 dbPassword = "";
104 
105             if (dbDriver != null)
106                 Loader.loadClass(this.getClass(), dbDriver).newInstance();
107         }
108         catch (ClassNotFoundException e)
109         {
110             throw new IllegalStateException (e.toString());
111         }
112         catch (InstantiationException e)
113         {
114             throw new IllegalStateException (e.toString());
115         }
116         catch (IllegalAccessException e)
117         {
118             throw new IllegalStateException (e.toString());
119         }
120     }
121 }