/AuthenticRoast/AuthenticRoast-Extras/src/main/java/name/aikesommer/authenticator/LogoutManager.java

http://authenticroast.googlecode.com/ · Java · 106 lines · 57 code · 11 blank · 38 comment · 10 complexity · 87cd6526db419c7cb89480784e79ece4 MD5 · raw file

  1. /**
  2. * Copyright (C) 2007-2010 Aike J Sommer (http://aikesommer.name/)
  3. *
  4. * This file is part of AuthenticRoast.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General
  17. * Public License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * You can reach the author and get more information about this
  22. * project at: http://aikesommer.name/
  23. */
  24. package name.aikesommer.authenticator;
  25. import java.io.UnsupportedEncodingException;
  26. import java.net.URLEncoder;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import name.aikesommer.authenticator.AuthenticationRequest.ManageAction;
  30. import name.aikesommer.authenticator.AuthenticationRequest.Status;
  31. /**
  32. * A very simple authenticator to allow for logouts.
  33. * Just have a link to "j_security_exit" as logout-link and this authenticator
  34. * should pick it up when clicked.
  35. *
  36. * @author Aike J Sommer
  37. */
  38. public class LogoutManager extends PluggableAuthenticator {
  39. public static final String LOGOUT_ACTION = "/j_security_exit";
  40. /**
  41. * Overwrite this to specify a different path to direct to after logging
  42. * the user out.
  43. */
  44. protected String getNextPath(AuthenticationManager manager, AuthenticationRequest request) {
  45. if (request.getHttpServletRequest().getParameter("_to") != null) {
  46. String to = request.getHttpServletRequest().getParameter("_to");
  47. String url = null;
  48. for (Map.Entry<String, String[]> entry : (Set<Map.Entry<String, String[]>>) request.
  49. getHttpServletRequest().getParameterMap().entrySet()) {
  50. if (entry.getKey().startsWith("_p_") && entry.getKey().length() > 3 && entry.getValue().length > 0) {
  51. String name;
  52. String value;
  53. try {
  54. name = URLEncoder.encode(entry.getKey().substring(3), "UTF-8");
  55. value = URLEncoder.encode(entry.getValue()[0], "UTF-8");
  56. } catch (UnsupportedEncodingException ex) {
  57. throw new RuntimeException(ex);
  58. }
  59. String param = name + "=" + value;
  60. url = url == null ? (to + "?" + param) : (url + "&" + param);
  61. }
  62. }
  63. if (url == null) {
  64. url = to;
  65. }
  66. return url;
  67. }
  68. return "/";
  69. }
  70. /**
  71. * Overwrite this to perform anything necessary with your session-data. This
  72. * is called before the session is destroyed.
  73. */
  74. protected void onLogout(AuthenticationManager manager, AuthenticationRequest request) {
  75. }
  76. @Override
  77. public Status tryAuthenticate(AuthenticationManager manager, AuthenticationRequest request) {
  78. return Status.None;
  79. }
  80. @Override
  81. public Status authenticate(AuthenticationManager manager, AuthenticationRequest request) {
  82. return Status.None;
  83. }
  84. @Override
  85. public ManageAction manage(AuthenticationManager manager, AuthenticationRequest request) {
  86. String path = request.getRequestPath();
  87. boolean logoutAction = path.endsWith(LOGOUT_ACTION);
  88. if (logoutAction) {
  89. onLogout(manager, request);
  90. manager.forward(request, getNextPath(manager, request));
  91. return ManageAction.Clear;
  92. }
  93. return ManageAction.None;
  94. }
  95. }