|
@@ -19,6 +19,7 @@ package org.springframework.security.web.server.ui;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
@@ -37,7 +38,6 @@ import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
import org.springframework.web.server.WebFilter;
|
|
|
import org.springframework.web.server.WebFilterChain;
|
|
|
-import org.springframework.web.util.HtmlUtils;
|
|
|
|
|
|
/**
|
|
|
* Generates a default log in page used for authenticating users.
|
|
@@ -89,80 +89,61 @@ public class LoginPageGeneratingWebFilter implements WebFilter {
|
|
|
private byte[] createPage(ServerWebExchange exchange, String csrfTokenHtmlInput) {
|
|
|
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
|
|
String contextPath = exchange.getRequest().getPath().contextPath().value();
|
|
|
- StringBuilder page = new StringBuilder();
|
|
|
- page.append("<!DOCTYPE html>\n");
|
|
|
- page.append("<html lang=\"en\">\n");
|
|
|
- page.append(" <head>\n");
|
|
|
- page.append(" <meta charset=\"utf-8\">\n");
|
|
|
- page.append(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">\n");
|
|
|
- page.append(" <meta name=\"description\" content=\"\">\n");
|
|
|
- page.append(" <meta name=\"author\" content=\"\">\n");
|
|
|
- page.append(" <title>Please sign in</title>\n");
|
|
|
- page.append(CssUtils.getCssStyleBlock().indent(4));
|
|
|
- page.append(" </head>\n");
|
|
|
- page.append(" <body>\n");
|
|
|
- page.append(" <div class=\"content\">\n");
|
|
|
- page.append(formLogin(queryParams, contextPath, csrfTokenHtmlInput));
|
|
|
- page.append(oauth2LoginLinks(queryParams, contextPath, this.oauth2AuthenticationUrlToClientName));
|
|
|
- page.append(" </div>\n");
|
|
|
- page.append(" </body>\n");
|
|
|
- page.append("</html>");
|
|
|
- return page.toString().getBytes(Charset.defaultCharset());
|
|
|
+
|
|
|
+ return HtmlTemplates.fromTemplate(LOGIN_PAGE_TEMPLATE)
|
|
|
+ .withRawHtml("cssStyle", CssUtils.getCssStyleBlock().indent(4))
|
|
|
+ .withRawHtml("formLogin", formLogin(queryParams, contextPath, csrfTokenHtmlInput))
|
|
|
+ .withRawHtml("oauth2Login", oauth2Login(queryParams, contextPath, this.oauth2AuthenticationUrlToClientName))
|
|
|
+ .render()
|
|
|
+ .getBytes(Charset.defaultCharset());
|
|
|
}
|
|
|
|
|
|
private String formLogin(MultiValueMap<String, String> queryParams, String contextPath, String csrfTokenHtmlInput) {
|
|
|
if (!this.formLoginEnabled) {
|
|
|
return "";
|
|
|
}
|
|
|
+
|
|
|
boolean isError = queryParams.containsKey("error");
|
|
|
boolean isLogoutSuccess = queryParams.containsKey("logout");
|
|
|
- StringBuilder page = new StringBuilder();
|
|
|
- page.append(" <form class=\"login-form\" method=\"post\" action=\"" + contextPath + "/login\">\n");
|
|
|
- page.append(" <h2>Please sign in</h2>\n");
|
|
|
- page.append(createError(isError));
|
|
|
- page.append(createLogoutSuccess(isLogoutSuccess));
|
|
|
- page.append(" <p>\n");
|
|
|
- page.append(" <label for=\"username\" class=\"screenreader\">Username</label>\n");
|
|
|
- page.append(" <input type=\"text\" id=\"username\" name=\"username\" "
|
|
|
- + "placeholder=\"Username\" required autofocus>\n");
|
|
|
- page.append(" </p>\n" + " <p>\n");
|
|
|
- page.append(" <label for=\"password\" class=\"screenreader\">Password</label>\n");
|
|
|
- page.append(" <input type=\"password\" id=\"password\" name=\"password\" "
|
|
|
- + "placeholder=\"Password\" required>\n");
|
|
|
- page.append(" </p>\n");
|
|
|
- page.append(csrfTokenHtmlInput);
|
|
|
- page.append(" <button class=\"primary\" type=\"submit\">Sign in</button>\n");
|
|
|
- page.append(" </form>\n");
|
|
|
- return page.toString();
|
|
|
+
|
|
|
+ return HtmlTemplates.fromTemplate(LOGIN_FORM_TEMPLATE)
|
|
|
+ .withValue("loginUrl", contextPath + "/login")
|
|
|
+ .withRawHtml("errorMessage", createError(isError))
|
|
|
+ .withRawHtml("logoutMessage", createLogoutSuccess(isLogoutSuccess))
|
|
|
+ .withRawHtml("csrf", csrfTokenHtmlInput)
|
|
|
+ .render();
|
|
|
}
|
|
|
|
|
|
- private static String oauth2LoginLinks(MultiValueMap<String, String> queryParams, String contextPath,
|
|
|
+ private static String oauth2Login(MultiValueMap<String, String> queryParams, String contextPath,
|
|
|
Map<String, String> oauth2AuthenticationUrlToClientName) {
|
|
|
if (oauth2AuthenticationUrlToClientName.isEmpty()) {
|
|
|
return "";
|
|
|
}
|
|
|
boolean isError = queryParams.containsKey("error");
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- sb.append("<div class=\"content\"><h2>Login with OAuth 2.0</h2>");
|
|
|
- sb.append(createError(isError));
|
|
|
- sb.append("<table class=\"table table-striped\">\n");
|
|
|
- for (Map.Entry<String, String> clientAuthenticationUrlToClientName : oauth2AuthenticationUrlToClientName
|
|
|
- .entrySet()) {
|
|
|
- sb.append(" <tr><td>");
|
|
|
- String url = clientAuthenticationUrlToClientName.getKey();
|
|
|
- sb.append("<a href=\"").append(contextPath).append(url).append("\">");
|
|
|
- String clientName = HtmlUtils.htmlEscape(clientAuthenticationUrlToClientName.getValue());
|
|
|
- sb.append(clientName);
|
|
|
- sb.append("</a>");
|
|
|
- sb.append("</td></tr>\n");
|
|
|
- }
|
|
|
- sb.append("</table></div>\n");
|
|
|
- return sb.toString();
|
|
|
+
|
|
|
+ String oauth2Rows = oauth2AuthenticationUrlToClientName.entrySet()
|
|
|
+ .stream()
|
|
|
+ .map((urlToName) -> oauth2LoginLink(contextPath, urlToName.getKey(), urlToName.getValue()))
|
|
|
+ .collect(Collectors.joining("\n"))
|
|
|
+ .indent(2);
|
|
|
+ return HtmlTemplates.fromTemplate(OAUTH2_LOGIN_TEMPLATE)
|
|
|
+ .withRawHtml("errorMessage", createError(isError))
|
|
|
+ .withRawHtml("oauth2Rows", oauth2Rows)
|
|
|
+ .render();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String oauth2LoginLink(String contextPath, String url, String clientName) {
|
|
|
+ return HtmlTemplates.fromTemplate(OAUTH2_ROW_TEMPLATE)
|
|
|
+ .withValue("url", contextPath + url)
|
|
|
+ .withValue("clientName", clientName)
|
|
|
+ .render();
|
|
|
}
|
|
|
|
|
|
private static String csrfToken(CsrfToken token) {
|
|
|
- return " <input type=\"hidden\" name=\"" + token.getParameterName() + "\" value=\"" + token.getToken()
|
|
|
- + "\">\n";
|
|
|
+ return HtmlTemplates.fromTemplate(CSRF_INPUT_TEMPLATE)
|
|
|
+ .withValue("name", token.getParameterName())
|
|
|
+ .withValue("value", token.getToken())
|
|
|
+ .render();
|
|
|
}
|
|
|
|
|
|
private static String createError(boolean isError) {
|
|
@@ -174,4 +155,53 @@ public class LoginPageGeneratingWebFilter implements WebFilter {
|
|
|
: "";
|
|
|
}
|
|
|
|
|
|
+ private static final String LOGIN_PAGE_TEMPLATE = """
|
|
|
+ <!DOCTYPE html>
|
|
|
+ <html lang="en">
|
|
|
+ <head>
|
|
|
+ <meta charset="utf-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
|
|
+ <meta name="description" content="">
|
|
|
+ <meta name="author" content="">
|
|
|
+ <title>Please sign in</title>
|
|
|
+ {{cssStyle}}
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+ <div class="content">
|
|
|
+ {{formLogin}}
|
|
|
+ {{oauth2Login}}
|
|
|
+ </div>
|
|
|
+ </body>
|
|
|
+ </html>""";
|
|
|
+
|
|
|
+ private static final String LOGIN_FORM_TEMPLATE = """
|
|
|
+ <form class="login-form" method="post" action="{{loginUrl}}">
|
|
|
+ <h2>Please sign in</h2>
|
|
|
+ {{errorMessage}}{{logoutMessage}}
|
|
|
+ <p>
|
|
|
+ <label for="username" class="screenreader">Username</label>
|
|
|
+ <input type="text" id="username" name="username" placeholder="Username" required autofocus>
|
|
|
+ </p>
|
|
|
+ <p>
|
|
|
+ <label for="password" class="screenreader">Password</label>
|
|
|
+ <input type="password" id="password" name="password" placeholder="Password" required>
|
|
|
+ </p>
|
|
|
+ {{csrf}}
|
|
|
+ <button type="submit" class="primary">Sign in</button>
|
|
|
+ </form>""";
|
|
|
+
|
|
|
+ private static final String CSRF_INPUT_TEMPLATE = """
|
|
|
+ <input name="{{name}}" type="hidden" value="{{value}}" />
|
|
|
+ """;
|
|
|
+
|
|
|
+ private static final String OAUTH2_LOGIN_TEMPLATE = """
|
|
|
+ <h2>Login with OAuth 2.0</h2>
|
|
|
+ {{errorMessage}}
|
|
|
+ <table class="table table-striped">
|
|
|
+ {{oauth2Rows}}
|
|
|
+ </table>""";
|
|
|
+
|
|
|
+ private static final String OAUTH2_ROW_TEMPLATE = """
|
|
|
+ <tr><td><a href="{{url}}">{{clientName}}</a></td></tr>""";
|
|
|
+
|
|
|
}
|