Skip to content

Commit

Permalink
test: Run the reverse proxy Spring Security test automatically (#14658)
Browse files Browse the repository at this point in the history
Uses a modified Jetty proxy servlet to deal with path rewriting
  • Loading branch information
Artur- authored Sep 28, 2022
1 parent 28c2a83 commit 5073899
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
3 changes: 1 addition & 2 deletions flow-tests/vaadin-spring-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@
<module>test-spring-security-flow</module>
<module>test-spring-security-flow-contextpath</module>
<module>test-spring-security-flow-urlmapping</module>
<!-- This can be included if it is set up to automatically start a reverse proxy as part of the build -->
<!-- <module>test-spring-security-flow-reverseproxy</module> -->
<module>test-spring-security-flow-reverseproxy</module>

<module>test-spring-boot-only-prepare</module>
<module>test-spring-white-list</module>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.vaadin.flow.spring.test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.proxy.ProxyServlet.Transparent;

public class PathRewritingProxyServlet extends Transparent {

private String prefix;
private String proxyTo;

@Override
public void init(ServletConfig config) throws ServletException {
proxyTo = config.getInitParameter("proxyTo");
prefix = config.getInitParameter("prefix");
super.init(config);
}

@Override
protected String filterServerResponseHeader(
HttpServletRequest clientRequest, Response serverResponse,
String headerName, String headerValue) {
if (headerName.toLowerCase(Locale.ENGLISH).equals("set-cookie")) {
// Set-Cookie: JSESSIONID=07E35F87D336463E597B5B0D32744660; Path=/;
// HttpOnly
return headerValue.replace("Path=/", "Path=" + prefix);
} else if (headerName.equals("Location")) {
// Location: http://localhost:8888/my/login/page
if ((headerValue.startsWith("http://")
|| headerValue.startsWith("https://"))
&& !headerValue.startsWith(proxyTo)) {
// External location
return headerValue;
}

try {
URL publicURL = new URL(
clientRequest.getRequestURL().toString());
String hostAndBasePath = publicURL.getProtocol() + "://"
+ publicURL.getHost() + ":" + publicURL.getPort()
+ prefix + "/";

if (headerValue.startsWith(proxyTo)) {
return headerValue.replace(proxyTo, hostAndBasePath);
} else {
// Location: /foo/bar
return prefix + headerValue;
}
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Unable to rewrite header "
+ headerName + ": " + headerValue);
}

}
return super.filterServerResponseHeader(clientRequest, serverResponse,
headerName, headerValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void start() throws Exception {
ServletContextHandler context = new ServletContextHandler(proxy, "/",
ServletContextHandler.SESSIONS);
ServletHolder proxyServlet = new ServletHolder(
ProxyServlet.Transparent.class);
PathRewritingProxyServlet.class);
proxyServlet.setInitParameter("proxyTo", "http://localhost:8888/");
proxyServlet.setInitParameter("prefix", proxyPath);
context.addServlet(proxyServlet, "/*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.vaadin.flow.spring.test.Proxy;

@SpringBootApplication
@ComponentScan(basePackageClasses = Proxy.class)
public class Application
extends com.vaadin.flow.spring.flowsecurity.Application {

Expand Down
1 change: 1 addition & 0 deletions scripts/computeMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const moduleWeights = {
'flow-tests/vaadin-spring-tests/test-spring-security-flow': { pos: 5, weight: 5 },
'flow-tests/vaadin-spring-tests/test-spring-security-flow-contextpath': { pos: 5, weight: 4 },
'flow-tests/vaadin-spring-tests/test-spring-security-flow-urlmapping': { pos: 5, weight: 4 },
'flow-tests/vaadin-spring-tests/test-spring-security-flow-reverseproxy': { pos: 5, weight: 4 },
'flow-tests/vaadin-spring-tests/test-spring': { pos: 5, weight: 3 },
'flow-tests/vaadin-spring-tests/test-mvc-without-endpoints': { pos: 5, weight: 2 },
'flow-tests/test-router-custom-context': { weight: 6 },
Expand Down

0 comments on commit 5073899

Please sign in to comment.