Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ResetPasswordFilter.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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 program 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 Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.authentication;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.io.IOException;
  23. import java.util.Set;
  24. import javax.servlet.FilterChain;
  25. import javax.servlet.FilterConfig;
  26. import javax.servlet.ServletException;
  27. import javax.servlet.ServletRequest;
  28. import javax.servlet.ServletResponse;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import org.sonar.api.web.ServletFilter;
  32. import org.sonar.server.user.ThreadLocalUserSession;
  33. import static org.sonar.api.web.ServletFilter.UrlPattern.Builder.staticResourcePatterns;
  34. import static org.sonar.server.authentication.AuthenticationRedirection.redirectTo;
  35. public class ResetPasswordFilter extends ServletFilter {
  36. private static final String RESET_PASSWORD_PATH = "/account/reset_password";
  37. private static final Set<String> SKIPPED_URLS = ImmutableSet.of(
  38. RESET_PASSWORD_PATH,
  39. "/batch/*", "/api/*");
  40. private final ThreadLocalUserSession userSession;
  41. public ResetPasswordFilter(ThreadLocalUserSession userSession) {
  42. this.userSession = userSession;
  43. }
  44. @Override
  45. public UrlPattern doGetPattern() {
  46. return UrlPattern.builder()
  47. .includes("/*")
  48. .excludes(staticResourcePatterns())
  49. .excludes(SKIPPED_URLS)
  50. .build();
  51. }
  52. @Override
  53. public void init(FilterConfig filterConfig) {
  54. // nothing to do
  55. }
  56. @Override
  57. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
  58. HttpServletRequest request = (HttpServletRequest) servletRequest;
  59. HttpServletResponse response = (HttpServletResponse) servletResponse;
  60. if (userSession.hasSession() && userSession.isLoggedIn() && userSession.shouldResetPassword()) {
  61. redirectTo(response, request.getContextPath() + RESET_PASSWORD_PATH);
  62. }
  63. chain.doFilter(request, response);
  64. }
  65. @Override
  66. public void destroy() {
  67. // nothing to do
  68. }
  69. }