/vt-ldap/tags/vt-ldap-3.2/src/main/java/edu/vt/middleware/ldap/servlets/LoginServlet.java

http://vt-middleware.googlecode.com/ · Java · 215 lines · 140 code · 18 blank · 57 comment · 45 complexity · bc14dd5fe63fdd2352456ceea43dbed6 MD5 · raw file

  1. /*
  2. $Id: LoginServlet.java 930 2009-10-26 20:44:26Z dfisher $
  3. Copyright (C) 2003-2009 Virginia Tech.
  4. All rights reserved.
  5. SEE LICENSE FOR MORE INFORMATION
  6. Author: Middleware Services
  7. Email: middleware@vt.edu
  8. Version: $Revision: 930 $
  9. Updated: $Date: 2009-10-26 21:44:26 +0100 (Mon, 26 Oct 2009) $
  10. */
  11. package edu.vt.middleware.ldap.servlets;
  12. import java.io.IOException;
  13. import java.net.URLEncoder;
  14. import javax.servlet.ServletConfig;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import javax.servlet.http.HttpSession;
  19. import edu.vt.middleware.ldap.Authenticator;
  20. import edu.vt.middleware.ldap.props.LdapProperties;
  21. /**
  22. * <code>LoginServet</code> attempts to authenticate a user against an LDAP. The
  23. * following init params can be set for this servlet:
  24. * edu.vt.middleware.ldap.servlets.propertiesFile - to load authenticator
  25. * properties from edu.vt.middleware.ldap.servlets.sessionId - to set the user
  26. * identifier in the session edu.vt.middleware.ldap.servlets.loginUrl - to set
  27. * the URL of your login page edu.vt.middleware.ldap.servlets.errorMsg - to
  28. * display if authentication fails
  29. * edu.vt.middleware.ldap.servlets.sessionManager - optional class to perform
  30. * session management after login and logout (must extend
  31. * edu.vt.middleware.ldap.servlets.session.SessionManager)
  32. *
  33. * <p>The following http params can be sent to this servlet: user - user
  34. * identifier to authenticate credential - user credential to authenticate with
  35. * url - to redirect client to after successful authentication</p>
  36. *
  37. * @author Middleware Services
  38. * @version $Revision: 930 $ $Date: 2009-10-26 21:44:26 +0100 (Mon, 26 Oct 2009) $
  39. */
  40. public final class LoginServlet extends CommonServlet
  41. {
  42. /** serial version uid. */
  43. private static final long serialVersionUID = -3482852409544351134L;
  44. /** URL of the page that does collects user credentials. */
  45. private String loginUrl;
  46. /** Message to display if authentication fails. */
  47. private String errorMsg;
  48. /** Used to authenticate against an LDAP. */
  49. private Authenticator auth;
  50. /**
  51. * Initialize this servlet.
  52. *
  53. * @param config <code>ServletConfig</code>
  54. *
  55. * @throws ServletException if an error occurs
  56. */
  57. public void init(final ServletConfig config)
  58. throws ServletException
  59. {
  60. super.init(config);
  61. this.loginUrl = getInitParameter(ServletConstants.LOGIN_URL);
  62. if (this.loginUrl == null) {
  63. this.loginUrl = ServletConstants.DEFAULT_LOGIN_URL;
  64. }
  65. if (this.logger.isDebugEnabled()) {
  66. this.logger.debug(ServletConstants.LOGIN_URL + " = " + this.loginUrl);
  67. }
  68. this.errorMsg = getInitParameter(ServletConstants.ERROR_MSG);
  69. if (this.errorMsg == null) {
  70. this.errorMsg = ServletConstants.DEFAULT_ERROR_MSG;
  71. }
  72. if (this.logger.isDebugEnabled()) {
  73. this.logger.debug(ServletConstants.ERROR_MSG + " = " + this.errorMsg);
  74. }
  75. String propertiesFile = getInitParameter(ServletConstants.PROPERTIES_FILE);
  76. if (propertiesFile == null) {
  77. propertiesFile = LdapProperties.PROPERTIES_FILE;
  78. }
  79. if (this.logger.isDebugEnabled()) {
  80. this.logger.debug(
  81. ServletConstants.PROPERTIES_FILE + " = " + propertiesFile);
  82. }
  83. this.auth = new Authenticator();
  84. this.auth.loadFromProperties(
  85. LoginServlet.class.getResourceAsStream(propertiesFile));
  86. }
  87. /**
  88. * Handle all requests sent to this servlet.
  89. *
  90. * @param request <code>HttpServletRequest</code>
  91. * @param response <code>HttpServletResponse</code>
  92. *
  93. * @throws ServletException if this request cannot be serviced
  94. * @throws IOException if a response cannot be sent
  95. */
  96. public void service(
  97. final HttpServletRequest request,
  98. final HttpServletResponse response)
  99. throws ServletException, IOException
  100. {
  101. boolean validCredentials = false;
  102. String user = request.getParameter(ServletConstants.USER_PARAM);
  103. if (user != null) {
  104. user = user.trim().toLowerCase();
  105. }
  106. if (this.logger.isDebugEnabled()) {
  107. this.logger.debug("Received user param = " + user);
  108. }
  109. final String credential = request.getParameter(
  110. ServletConstants.CREDENTIAL_PARAM);
  111. String url = request.getParameter(ServletConstants.URL_PARAM);
  112. if (url == null) {
  113. url = "";
  114. }
  115. if (this.logger.isDebugEnabled()) {
  116. this.logger.debug("Received url param = " + url);
  117. }
  118. final StringBuffer error = new StringBuffer(this.errorMsg);
  119. try {
  120. if (this.auth.authenticate(user, credential)) {
  121. validCredentials = true;
  122. }
  123. } catch (Exception e) {
  124. if (this.logger.isErrorEnabled()) {
  125. this.logger.error("Error authenticating user " + user, e);
  126. }
  127. if (
  128. e.getCause() != null &&
  129. e.getCause().getMessage() != null &&
  130. !e.getCause().getMessage().equals("null")) {
  131. error.append(": ").append(e.getCause().getMessage());
  132. } else if (e.getMessage() != null && !e.getMessage().equals("null")) {
  133. error.append(": ").append(e.getMessage());
  134. }
  135. }
  136. if (validCredentials) {
  137. if (this.logger.isDebugEnabled()) {
  138. this.logger.debug("Authentication succeeded for user " + user);
  139. }
  140. try {
  141. // invalidate existing session
  142. HttpSession session = request.getSession(false);
  143. if (session != null) {
  144. session.invalidate();
  145. }
  146. session = request.getSession(true);
  147. this.sessionManager.login(session, user);
  148. if (this.logger.isDebugEnabled()) {
  149. this.logger.debug("Initialized session for user " + user);
  150. }
  151. response.sendRedirect(url);
  152. if (this.logger.isDebugEnabled()) {
  153. this.logger.debug("Redirected user to " + url);
  154. }
  155. return;
  156. } catch (Exception e) {
  157. if (this.logger.isErrorEnabled()) {
  158. this.logger.error("Error authorizing user " + user, e);
  159. }
  160. if (
  161. e.getCause() != null &&
  162. e.getCause().getMessage() != null &&
  163. !e.getCause().getMessage().equals("null")) {
  164. error.append(": ").append(e.getCause().getMessage());
  165. } else if (e.getMessage() != null && !e.getMessage().equals("null")) {
  166. error.append(": ").append(e.getMessage());
  167. }
  168. }
  169. }
  170. final StringBuffer errorUrl = new StringBuffer(this.loginUrl);
  171. if (error != null) {
  172. errorUrl.append("?error=").append(
  173. URLEncoder.encode(error.toString(), "UTF-8"));
  174. }
  175. if (user != null) {
  176. errorUrl.append("&user=").append(URLEncoder.encode(user, "UTF-8"));
  177. }
  178. if (url != null) {
  179. errorUrl.append("&url=").append(URLEncoder.encode(url, "UTF-8"));
  180. }
  181. response.sendRedirect(errorUrl.toString());
  182. if (this.logger.isDebugEnabled()) {
  183. this.logger.debug("Redirected user to " + errorUrl.toString());
  184. }
  185. }
  186. /**
  187. * Called by the servlet container to indicate to a servlet that the servlet
  188. * is being taken out of service.
  189. */
  190. public void destroy()
  191. {
  192. super.destroy();
  193. }
  194. }