Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(spring-boot-webapp): introduce filter to make URLs without trailing slashes work #3947

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
import static java.util.Collections.singletonMap;
import static org.glassfish.jersey.servlet.ServletProperties.JAXRS_APPLICATION_CLASS;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterRegistration;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;
import jakarta.servlet.SessionTrackingMode;

import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;
import org.camunda.bpm.admin.impl.web.AdminApplication;
import org.camunda.bpm.admin.impl.web.bootstrap.AdminContainerBootstrap;
import org.camunda.bpm.cockpit.impl.web.CockpitApplication;
Expand All @@ -38,6 +36,7 @@
import org.camunda.bpm.engine.rest.filter.EmptyBodyFilter;
import org.camunda.bpm.spring.boot.starter.property.CamundaBpmProperties;
import org.camunda.bpm.spring.boot.starter.property.WebappProperty;
import org.camunda.bpm.spring.boot.starter.webapp.filter.AppendTrailingSlashFilter;
import org.camunda.bpm.spring.boot.starter.webapp.filter.LazyProcessEnginesFilter;
import org.camunda.bpm.spring.boot.starter.webapp.filter.LazySecurityFilter;
import org.camunda.bpm.tasklist.impl.web.TasklistApplication;
Expand Down Expand Up @@ -92,6 +91,14 @@ public void onStartup(ServletContext servletContext) {

ServletContextUtil.setAppPath(applicationPath, servletContext);

// make sure that trailing slashes are added for the registered patterns
// see AppendTrailingSlashFilter for details
registerFilter("AppendTrailingSlashFilter", AppendTrailingSlashFilter.class,
applicationPath + "/app",
applicationPath + "/app/cockpit",
applicationPath + "/app/admin",
applicationPath + "/app/tasklist",
applicationPath + "/app/welcome");
registerFilter("Authentication Filter", AuthenticationFilter.class,
Collections.singletonMap("cacheTimeToLive", getAuthCacheTTL(webapp)),
applicationPath + "/api/*", applicationPath + "/app/*");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.spring.boot.starter.webapp.filter;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* Servlet filter to ensure request paths always have a trailing slash. Before Spring Boot 3, missing trailing slashes were
* handled automatically. This filter implements the Spring Boot 2 behavior for the registered request patterns.
*
* @see https://github.com/spring-projects/spring-framework/issues/28552
*/
public class AppendTrailingSlashFilter implements Filter {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String requestURI = ((HttpServletRequest) request).getRequestURI();
((HttpServletResponse) response).sendRedirect(requestURI + "/");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.spring.boot.starter.webapp.apppath;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.camunda.bpm.spring.boot.starter.webapp.TestApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(
classes = { TestApplication.class },
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RequestTrailingSlashIT {

public static final List<String> REDIRECT_PATHS = List.of("/app", "/app/cockpit", "/app/admin", "/app/tasklist", "/app/welcome");

TestRestTemplate client = new TestRestTemplate();

@LocalServerPort
public int port;

@Test
public void shouldRedirectPathWithMissingTrailingSlash() throws IOException {
// given
List<ResponseEntity<String>> responses = new ArrayList<>();

// when calling different paths with and without trailing slashes
for (String path : REDIRECT_PATHS) {
String url = "http://localhost:" + port + "/camunda" + path;
responses.add(client.getForEntity(url, String.class));
responses.add(client.getForEntity(url + "/", String.class));
}

// then all paths should be found
assertThat(responses).extracting("statusCode").containsOnly(HttpStatus.OK);
}

}