3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.authentication;
22 import java.io.IOException;
24 import org.sonar.api.config.Configuration;
25 import org.sonar.api.server.http.HttpRequest;
26 import org.sonar.api.server.http.HttpResponse;
27 import org.sonar.api.web.FilterChain;
28 import org.sonar.api.web.HttpFilter;
29 import org.sonar.api.web.UrlPattern;
30 import org.sonar.server.user.ThreadLocalUserSession;
32 import static org.sonar.api.web.UrlPattern.Builder.staticResourcePatterns;
33 import static org.sonar.server.authentication.AuthenticationRedirection.redirectTo;
35 public class DefaultAdminCredentialsVerifierFilter extends HttpFilter {
36 private static final String RESET_PASSWORD_PATH = "/account/reset_password";
37 private static final String CHANGE_ADMIN_PASSWORD_PATH = "/admin/change_admin_password";
38 // This property is used by Orchestrator to disable this force redirect. It should never be used in production, which
39 // is why this is not defined in org.sonar.process.ProcessProperties.
40 private static final String SONAR_FORCE_REDIRECT_DEFAULT_ADMIN_CREDENTIALS = "sonar.forceRedirectOnDefaultAdminCredentials";
42 private static final Set<String> SKIPPED_URLS = Set.of(
44 CHANGE_ADMIN_PASSWORD_PATH,
45 "/batch/*", "/api/*", "/api/v2/*");
47 private final Configuration config;
48 private final DefaultAdminCredentialsVerifier defaultAdminCredentialsVerifier;
49 private final ThreadLocalUserSession userSession;
51 public DefaultAdminCredentialsVerifierFilter(Configuration config, DefaultAdminCredentialsVerifier defaultAdminCredentialsVerifier, ThreadLocalUserSession userSession) {
53 this.defaultAdminCredentialsVerifier = defaultAdminCredentialsVerifier;
54 this.userSession = userSession;
58 public UrlPattern doGetPattern() {
59 return UrlPattern.builder()
61 .excludes(staticResourcePatterns())
62 .excludes(SKIPPED_URLS)
72 public void doFilter(HttpRequest request, HttpResponse response, FilterChain chain) throws IOException {
73 boolean forceRedirect = config
74 .getBoolean(SONAR_FORCE_REDIRECT_DEFAULT_ADMIN_CREDENTIALS)
77 if (forceRedirect && userSession.hasSession() && userSession.isLoggedIn()
78 && userSession.isSystemAdministrator() && !"admin".equals(userSession.getLogin())
79 && defaultAdminCredentialsVerifier.hasDefaultCredentialUser()) {
80 redirectTo(response, request.getContextPath() + CHANGE_ADMIN_PASSWORD_PATH);
83 chain.doFilter(request, response);
87 public void destroy() {