/hudson-core/src/main/java/hudson/security/AuthenticationProcessingFilter2.java

http://github.com/hudson/hudson · Java · 88 lines · 35 code · 10 blank · 43 comment · 3 complexity · e28c44947de6124e832bec6fcb7fe01e MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Matthew R. Harrah
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.security;
  25. import java.util.Properties;
  26. import java.util.logging.Logger;
  27. import java.util.logging.Level;
  28. import java.io.IOException;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import org.acegisecurity.AuthenticationException;
  32. import org.acegisecurity.ui.webapp.AuthenticationProcessingFilter;
  33. /**
  34. * {@link AuthenticationProcessingFilter} with a change for Hudson so that
  35. * we can pick up the hidden "from" form field defined in <tt>login.jelly</tt>
  36. * to send the user back to where he came from, after a successful authentication.
  37. *
  38. * @author Kohsuke Kawaguchi
  39. */
  40. public class AuthenticationProcessingFilter2 extends AuthenticationProcessingFilter {
  41. @Override
  42. protected String determineTargetUrl(HttpServletRequest request) {
  43. String targetUrl = request.getParameter("from");
  44. request.getSession().setAttribute("from", targetUrl);
  45. if (targetUrl == null)
  46. return getDefaultTargetUrl();
  47. // URL returned from determineTargetUrl() is resolved against the context path,
  48. // whereas the "from" URL is resolved against the top of the website, so adjust this.
  49. if(targetUrl.startsWith(request.getContextPath()))
  50. return targetUrl.substring(request.getContextPath().length());
  51. // not sure when this happens, but apparently this happens in some case.
  52. // see #1274
  53. return targetUrl;
  54. }
  55. /**
  56. * @see org.acegisecurity.ui.AbstractProcessingFilter#determineFailureUrl(javax.servlet.http.HttpServletRequest, org.acegisecurity.AuthenticationException)
  57. */
  58. @Override
  59. protected String determineFailureUrl(HttpServletRequest request, AuthenticationException failed) {
  60. Properties excMap = getExceptionMappings();
  61. String failedClassName = failed.getClass().getName();
  62. String whereFrom = request.getParameter("from");
  63. request.getSession().setAttribute("from", whereFrom);
  64. return excMap.getProperty(failedClassName, getAuthenticationFailureUrl());
  65. }
  66. /**
  67. * Leave the information about login failure.
  68. *
  69. * <p>
  70. * Otherwise it seems like Acegi doesn't really leave the detail of the failure anywhere.
  71. */
  72. @Override
  73. protected void onUnsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException {
  74. super.onUnsuccessfulAuthentication(request, response, failed);
  75. LOGGER.log(Level.INFO, "Login attempt failed", failed);
  76. }
  77. private static final Logger LOGGER = Logger.getLogger(AuthenticationProcessingFilter2.class.getName());
  78. }