-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Run the reverse proxy Spring Security test automatically (#14658)
Uses a modified Jetty proxy servlet to deal with path rewriting
- Loading branch information
Showing
5 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...t-spring-helpers/src/main/java/com/vaadin/flow/spring/test/PathRewritingProxyServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters