|
1 | 1 | package io.kafbat.ui.controller; |
2 | 2 |
|
3 | | -import java.nio.charset.Charset; |
4 | 3 | import lombok.RequiredArgsConstructor; |
5 | 4 | import lombok.extern.slf4j.Slf4j; |
6 | 5 | import org.springframework.core.io.ClassPathResource; |
7 | | -import org.springframework.security.web.server.csrf.CsrfToken; |
8 | | -import org.springframework.util.MultiValueMap; |
9 | 6 | import org.springframework.web.bind.annotation.GetMapping; |
10 | 7 | import org.springframework.web.bind.annotation.RestController; |
11 | | -import org.springframework.web.server.ServerWebExchange; |
12 | 8 | import reactor.core.publisher.Mono; |
13 | 9 |
|
14 | 10 | @RestController |
15 | 11 | @RequiredArgsConstructor |
16 | 12 | @Slf4j |
17 | 13 | public class AuthenticationController { |
18 | 14 |
|
19 | | - @GetMapping(value = "/login", produces = {"text/html"}) |
20 | | - public Mono<ClassPathResource> getLoginPage(ServerWebExchange exchange) { |
21 | | - return Mono.just(new ClassPathResource("static/index.html")); |
22 | | - } |
23 | | - |
24 | | - @GetMapping(value = "/auth", produces = {"text/html"}) |
25 | | - public Mono<byte[]> getAuth(ServerWebExchange exchange) { |
26 | | - Mono<CsrfToken> token = exchange.getAttributeOrDefault(CsrfToken.class.getName(), Mono.empty()); |
27 | | - return token |
28 | | - .map(AuthenticationController::csrfToken) |
29 | | - .defaultIfEmpty("") |
30 | | - .map(csrfTokenHtmlInput -> createPage(exchange, csrfTokenHtmlInput)); |
31 | | - } |
32 | | - |
33 | | - private byte[] createPage(ServerWebExchange exchange, String csrfTokenHtmlInput) { |
34 | | - MultiValueMap<String, String> queryParams = exchange.getRequest() |
35 | | - .getQueryParams(); |
36 | | - String contextPath = exchange.getRequest().getPath().contextPath().value(); |
37 | | - String page = |
38 | | - "<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + " <head>\n" |
39 | | - + " <meta charset=\"utf-8\">\n" |
40 | | - + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, " |
41 | | - + "shrink-to-fit=no\">\n" |
42 | | - + " <meta name=\"description\" content=\"\">\n" |
43 | | - + " <meta name=\"author\" content=\"\">\n" |
44 | | - + " <title>Please sign in</title>\n" |
45 | | - + " <link href=\"" + contextPath + "/static/css/bootstrap.min.css\" rel=\"stylesheet\" " |
46 | | - + "integrity=\"sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M\" " |
47 | | - + "crossorigin=\"anonymous\">\n" |
48 | | - + " <link href=\"" + contextPath + "/static/css/signin.css\" " |
49 | | - + "rel=\"stylesheet\" crossorigin=\"anonymous\"/>\n" |
50 | | - + " </head>\n" |
51 | | - + " <body>\n" |
52 | | - + " <div class=\"container\">\n" |
53 | | - + formLogin(queryParams, contextPath, csrfTokenHtmlInput) |
54 | | - + " </div>\n" |
55 | | - + " </body>\n" |
56 | | - + "</html>"; |
57 | | - |
58 | | - return page.getBytes(Charset.defaultCharset()); |
59 | | - } |
| 15 | + private static final String INDEX_HTML = "/static/index.html"; |
60 | 16 |
|
61 | | - private String formLogin( |
62 | | - MultiValueMap<String, String> queryParams, |
63 | | - String contextPath, String csrfTokenHtmlInput) { |
64 | | - |
65 | | - boolean isError = queryParams.containsKey("error"); |
66 | | - boolean isLogoutSuccess = queryParams.containsKey("logout"); |
67 | | - return |
68 | | - " <form class=\"form-signin\" method=\"post\" action=\"" + contextPath + "/auth\">\n" |
69 | | - + " <h2 class=\"form-signin-heading\">Please sign in</h2>\n" |
70 | | - + createError(isError) |
71 | | - + createLogoutSuccess(isLogoutSuccess) |
72 | | - + " <p>\n" |
73 | | - + " <label for=\"username\" class=\"sr-only\">Username</label>\n" |
74 | | - + " <input type=\"text\" id=\"username\" name=\"username\" class=\"form-control\" " |
75 | | - + "placeholder=\"Username\" required autofocus>\n" |
76 | | - + " </p>\n" + " <p>\n" |
77 | | - + " <label for=\"password\" class=\"sr-only\">Password</label>\n" |
78 | | - + " <input type=\"password\" id=\"password\" name=\"password\" " |
79 | | - + "class=\"form-control\" placeholder=\"Password\" required>\n" |
80 | | - + " </p>\n" + csrfTokenHtmlInput |
81 | | - + " <button class=\"btn btn-lg btn-primary btn-block\" " |
82 | | - + "type=\"submit\">Sign in</button>\n" |
83 | | - + " </form>\n"; |
84 | | - } |
85 | | - |
86 | | - private static String csrfToken(CsrfToken token) { |
87 | | - return " <input type=\"hidden\" name=\"" |
88 | | - + token.getParameterName() |
89 | | - + "\" value=\"" |
90 | | - + token.getToken() |
91 | | - + "\">\n"; |
92 | | - } |
93 | | - |
94 | | - private static String createError(boolean isError) { |
95 | | - return isError |
96 | | - ? "<div class=\"alert alert-danger\" role=\"alert\">Invalid credentials</div>" |
97 | | - : ""; |
| 17 | + @GetMapping(value = "/login", produces = {"text/html"}) |
| 18 | + public Mono<ClassPathResource> getLoginPage() { |
| 19 | + return Mono.just(new ClassPathResource(INDEX_HTML)); |
98 | 20 | } |
99 | 21 |
|
100 | | - private static String createLogoutSuccess(boolean isLogoutSuccess) { |
101 | | - return isLogoutSuccess |
102 | | - ? "<div class=\"alert alert-success\" role=\"alert\">You have been signed out</div>" |
103 | | - : ""; |
104 | | - } |
105 | 22 | } |
0 commit comments