@@ -59,6 +59,9 @@ public class DefaultLoginPageGeneratingFilter extends GenericFilterBean {
59
59
60
60
public static final String ERROR_PARAMETER_NAME = "error" ;
61
61
62
+ private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
63
+ .getContextHolderStrategy ();
64
+
62
65
private @ Nullable String loginPageUrl ;
63
66
64
67
private @ Nullable String logoutSuccessUrl ;
@@ -118,6 +121,10 @@ private void initAuthFilter(UsernamePasswordAuthenticationFilter authFilter) {
118
121
}
119
122
}
120
123
124
+ public void setSecurityContextHolderStrategy (SecurityContextHolderStrategy securityContextHolderStrategy ) {
125
+ this .securityContextHolderStrategy = securityContextHolderStrategy ;
126
+ }
127
+
121
128
/**
122
129
* Sets a Function used to resolve a Map of the hidden inputs where the key is the
123
130
* name of the input and the value is the value of the input. Typically this is used
@@ -307,6 +314,13 @@ private String renderFormLogin(HttpServletRequest request, boolean loginError, b
307
314
return "" ;
308
315
}
309
316
317
+ String username = getUsername ();
318
+ String usernameInput = (username == null ) ? FORM_USERNAME_INPUT
319
+ : HtmlTemplates .fromTemplate (FORM_READONLY_USERNAME_INPUT )
320
+ .withValue ("usernameParameter" , this .usernameParameter )
321
+ .withValue ("username" , username )
322
+ .render ();
323
+
310
324
String hiddenInputs = this .resolveHiddenInputs .apply (request )
311
325
.entrySet ()
312
326
.stream ()
@@ -317,7 +331,7 @@ private String renderFormLogin(HttpServletRequest request, boolean loginError, b
317
331
.withValue ("loginUrl" , contextPath + this .authenticationUrl )
318
332
.withRawHtml ("errorMessage" , renderError (loginError , errorMsg ))
319
333
.withRawHtml ("logoutMessage" , renderSuccess (logoutSuccess ))
320
- .withValue ( "usernameParameter " , this . usernameParameter )
334
+ .withRawHtml ( "usernameInput " , usernameInput )
321
335
.withValue ("passwordParameter" , this .passwordParameter )
322
336
.withRawHtml ("rememberMeInput" , renderRememberMe (this .rememberMeParameter ))
323
337
.withRawHtml ("hiddenInputs" , hiddenInputs )
@@ -337,11 +351,16 @@ private String renderOneTimeTokenLogin(HttpServletRequest request, boolean login
337
351
.map ((inputKeyValue ) -> renderHiddenInput (inputKeyValue .getKey (), inputKeyValue .getValue ()))
338
352
.collect (Collectors .joining ("\n " ));
339
353
354
+ String username = getUsername ();
355
+ String usernameInput = (username == null ) ? ONE_TIME_USERNAME_INPUT
356
+ : HtmlTemplates .fromTemplate (ONE_TIME_READONLY_USERNAME_INPUT ).withValue ("username" , username ).render ();
357
+
340
358
return HtmlTemplates .fromTemplate (ONE_TIME_TEMPLATE )
341
359
.withValue ("generateOneTimeTokenUrl" , contextPath + this .generateOneTimeTokenUrl )
342
360
.withRawHtml ("errorMessage" , renderError (loginError , errorMsg ))
343
361
.withRawHtml ("logoutMessage" , renderSuccess (logoutSuccess ))
344
362
.withRawHtml ("hiddenInputs" , hiddenInputs )
363
+ .withRawHtml ("usernameInput" , usernameInput )
345
364
.render ();
346
365
}
347
366
@@ -410,6 +429,14 @@ private String renderRememberMe(@Nullable String paramName) {
410
429
.render ();
411
430
}
412
431
432
+ private @ Nullable String getUsername () {
433
+ Authentication authentication = this .securityContextHolderStrategy .getContext ().getAuthentication ();
434
+ if (authentication != null && authentication .isAuthenticated ()) {
435
+ return authentication .getName ();
436
+ }
437
+ return null ;
438
+ }
439
+
413
440
private boolean isLogoutSuccess (HttpServletRequest request ) {
414
441
return this .logoutSuccessUrl != null && matches (request , this .logoutSuccessUrl );
415
442
}
@@ -511,7 +538,7 @@ private boolean matches(HttpServletRequest request, @Nullable String url) {
511
538
{{errorMessage}}{{logoutMessage}}
512
539
<p>
513
540
<label for="username" class="screenreader">Username</label>
514
- <input type="text" id="username" name="{{usernameParameter}}" placeholder="Username" required autofocus>
541
+ {{usernameInput}}
515
542
</p>
516
543
<p>
517
544
<label for="password" class="screenreader">Password</label>
@@ -522,6 +549,14 @@ private boolean matches(HttpServletRequest request, @Nullable String url) {
522
549
<button type="submit" class="primary">Sign in</button>
523
550
</form>""" ;
524
551
552
+ private static final String FORM_READONLY_USERNAME_INPUT = """
553
+ <input type="text" id="ott-username" name="{{usernameParameter}}" value="{{username}}" placeholder="Username" required readonly>
554
+ """ ;
555
+
556
+ private static final String FORM_USERNAME_INPUT = """
557
+ <input type="text" id="ott-username" name="{{usernameParameter}}" placeholder="Username" required>
558
+ """ ;
559
+
525
560
private static final String HIDDEN_HTML_INPUT_TEMPLATE = """
526
561
<input name="{{name}}" type="hidden" value="{{value}}" />
527
562
""" ;
@@ -554,11 +589,19 @@ private boolean matches(HttpServletRequest request, @Nullable String url) {
554
589
{{errorMessage}}{{logoutMessage}}
555
590
<p>
556
591
<label for="ott-username" class="screenreader">Username</label>
557
- <input type="text" id="ott-username" name="username" placeholder="Username" required>
592
+ {{usernameInput}}
558
593
</p>
559
594
{{hiddenInputs}}
560
595
<button class="primary" type="submit" form="ott-form">Send Token</button>
561
596
</form>
562
597
""" ;
563
598
599
+ private static final String ONE_TIME_READONLY_USERNAME_INPUT = """
600
+ <input type="text" id="ott-username" name="username" value="{{username}}" placeholder="Username" required readonly>
601
+ """ ;
602
+
603
+ private static final String ONE_TIME_USERNAME_INPUT = """
604
+ <input type="text" id="ott-username" name="username" placeholder="Username" required>
605
+ """ ;
606
+
564
607
}
0 commit comments