Skip to content
This repository was archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
Generate relative URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanik committed Mar 18, 2019
1 parent 663089c commit 5a02bb6
Showing 1 changed file with 5 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.springframework.security.saml2.serviceprovider.web.filters;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.FilterChain;
Expand All @@ -29,11 +28,9 @@
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.HtmlUtils;
import org.springframework.web.util.UriComponentsBuilder;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.http.MediaType.TEXT_HTML_VALUE;
import static org.springframework.security.saml2.util.StringUtils.stripSlashes;

/**
* Filter that generates a static SAML SP login page.
Expand All @@ -54,28 +51,16 @@ public SamlLoginPageGeneratingFilter(RequestMatcher matcher,
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (matcher.matches(request)) {
generateLoginPage(request, response);
response.setContentType(TEXT_HTML_VALUE);
response.setCharacterEncoding(UTF_8.name());
response.getWriter().write(getSamlLoginPageHtml(providerUrls, request.getContextPath()));
}
else {
filterChain.doFilter(request, response);
}
}

private void generateLoginPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String, String> urls = new HashMap<>();
providerUrls.entrySet().stream().forEach(
e -> {
String linkText = e.getKey();
String url = getAuthenticationRequestRedirectUrl(e.getValue(), request);
urls.put(linkText, url);
}
);
response.setContentType(TEXT_HTML_VALUE);
response.setCharacterEncoding(UTF_8.name());
response.getWriter().write(getSamlLoginPageHtml(urls));
}

private String getSamlLoginPageHtml(Map<String, String> providers) {
private String getSamlLoginPageHtml(Map<String, String> providers, String contextPath) {
return
"<html>\n" +
"<head>\n" +
Expand All @@ -89,7 +74,7 @@ private String getSamlLoginPageHtml(Map<String, String> providers) {
.map(
entry ->
" <li>\n" +
" <a href=\"" + entry.getValue() + "\"><span style=\"font-weight:bold\">" +
" <a href=\"" + contextPath + "/" + entry.getValue() + "\"><span style=\"font-weight:bold\">" +
HtmlUtils.htmlEscape(entry.getKey()) + "</span></a>\n" +
" </li>\n"
)
Expand All @@ -100,28 +85,4 @@ private String getSamlLoginPageHtml(Map<String, String> providers) {
"</html>"
;
}

private String getAuthenticationRequestRedirectUrl(String url,
HttpServletRequest request) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(
getBasePath(request, false)
);
builder.pathSegment(stripSlashes(url));
return builder.build().toUriString();
}

private String getBasePath(HttpServletRequest request, boolean includeStandardPorts) {
boolean includePort = true;
if (443 == request.getServerPort() && "https".equals(request.getScheme())) {
includePort = includeStandardPorts;
}
else if (80 == request.getServerPort() && "http".equals(request.getScheme())) {
includePort = includeStandardPorts;
}
return request.getScheme() +
"://" +
request.getServerName() +
(includePort ? (":" + request.getServerPort()) : "") +
request.getContextPath();
}
}

0 comments on commit 5a02bb6

Please sign in to comment.