/** * Send a redirect back to the HTTP client. * @param targetUrl the target URL to redirect to * @param exchange current exchange */ protected Mono<Void> sendRedirect(String targetUrl, ServerWebExchange exchange) { String transformedUrl = (isRemoteHost(targetUrl) ? targetUrl : exchange.transformUrl(targetUrl)); ServerHttpResponse response = exchange.getResponse(); response.getHeaders().setLocation(URI.create(transformedUrl)); response.setStatusCode(getStatusCode()); return Mono.empty(); }
@Test public void remoteHost() { RedirectView view = new RedirectView(""); assertFalse(view.isRemoteHost("http://url.somewhere.com")); assertFalse(view.isRemoteHost("/path")); assertFalse(view.isRemoteHost("http://url.somewhereelse.com")); view.setHosts("url.somewhere.com"); assertFalse(view.isRemoteHost("http://url.somewhere.com")); assertFalse(view.isRemoteHost("/path")); assertTrue(view.isRemoteHost("http://url.somewhereelse.com")); }
/** * Send a redirect back to the HTTP client * @param targetUrl the target URL to redirect to * @param exchange current exchange */ protected Mono<Void> sendRedirect(String targetUrl, ServerWebExchange exchange) { ServerHttpResponse response = exchange.getResponse(); String encodedURL = (isRemoteHost(targetUrl) ? targetUrl : response.encodeUrl(targetUrl)); response.getHeaders().setLocation(URI.create(encodedURL)); response.setStatusCode(getStatusCode()); return Mono.empty(); }