@@ -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,18 @@ private void initAuthFilter(UsernamePasswordAuthenticationFilter authFilter) {
118
121
}
119
122
}
120
123
124
+ /**
125
+ * Use this {@link SecurityContextHolderStrategy} to retrieve authenticated users.
126
+ * <p>
127
+ * Uses {@link SecurityContextHolder#getContextHolderStrategy()} by default.
128
+ * @param securityContextHolderStrategy the strategy to use
129
+ * @since 7.0
130
+ */
131
+ public void setSecurityContextHolderStrategy (SecurityContextHolderStrategy securityContextHolderStrategy ) {
132
+ Assert .notNull (securityContextHolderStrategy , "securityContextHolderStrategy cannot be null" );
133
+ this .securityContextHolderStrategy = securityContextHolderStrategy ;
134
+ }
135
+
121
136
/**
122
137
* Sets a Function used to resolve a Map of the hidden inputs where the key is the
123
138
* name of the input and the value is the value of the input. Typically this is used
@@ -307,6 +322,13 @@ private String renderFormLogin(HttpServletRequest request, boolean loginError, b
307
322
return "" ;
308
323
}
309
324
325
+ String username = getUsername ();
326
+ String usernameInput = ((username != null )
327
+ ? HtmlTemplates .fromTemplate (FORM_READONLY_USERNAME_INPUT ).withValue ("username" , username )
328
+ : HtmlTemplates .fromTemplate (FORM_USERNAME_INPUT ))
329
+ .withValue ("usernameParameter" , this .usernameParameter )
330
+ .render ();
331
+
310
332
String hiddenInputs = this .resolveHiddenInputs .apply (request )
311
333
.entrySet ()
312
334
.stream ()
@@ -317,7 +339,7 @@ private String renderFormLogin(HttpServletRequest request, boolean loginError, b
317
339
.withValue ("loginUrl" , contextPath + this .authenticationUrl )
318
340
.withRawHtml ("errorMessage" , renderError (loginError , errorMsg ))
319
341
.withRawHtml ("logoutMessage" , renderSuccess (logoutSuccess ))
320
- .withValue ( "usernameParameter " , this . usernameParameter )
342
+ .withRawHtml ( "usernameInput " , usernameInput )
321
343
.withValue ("passwordParameter" , this .passwordParameter )
322
344
.withRawHtml ("rememberMeInput" , renderRememberMe (this .rememberMeParameter ))
323
345
.withRawHtml ("hiddenInputs" , hiddenInputs )
@@ -337,11 +359,17 @@ private String renderOneTimeTokenLogin(HttpServletRequest request, boolean login
337
359
.map ((inputKeyValue ) -> renderHiddenInput (inputKeyValue .getKey (), inputKeyValue .getValue ()))
338
360
.collect (Collectors .joining ("\n " ));
339
361
362
+ String username = getUsername ();
363
+ String usernameInput = (username != null )
364
+ ? HtmlTemplates .fromTemplate (ONE_TIME_READONLY_USERNAME_INPUT ).withValue ("username" , username ).render ()
365
+ : ONE_TIME_USERNAME_INPUT ;
366
+
340
367
return HtmlTemplates .fromTemplate (ONE_TIME_TEMPLATE )
341
368
.withValue ("generateOneTimeTokenUrl" , contextPath + this .generateOneTimeTokenUrl )
342
369
.withRawHtml ("errorMessage" , renderError (loginError , errorMsg ))
343
370
.withRawHtml ("logoutMessage" , renderSuccess (logoutSuccess ))
344
371
.withRawHtml ("hiddenInputs" , hiddenInputs )
372
+ .withRawHtml ("usernameInput" , usernameInput )
345
373
.render ();
346
374
}
347
375
@@ -410,6 +438,14 @@ private String renderRememberMe(@Nullable String paramName) {
410
438
.render ();
411
439
}
412
440
441
+ private @ Nullable String getUsername () {
442
+ Authentication authentication = this .securityContextHolderStrategy .getContext ().getAuthentication ();
443
+ if (authentication != null && authentication .isAuthenticated ()) {
444
+ return authentication .getName ();
445
+ }
446
+ return null ;
447
+ }
448
+
413
449
private boolean isLogoutSuccess (HttpServletRequest request ) {
414
450
return this .logoutSuccessUrl != null && matches (request , this .logoutSuccessUrl );
415
451
}
@@ -511,7 +547,7 @@ private boolean matches(HttpServletRequest request, @Nullable String url) {
511
547
{{errorMessage}}{{logoutMessage}}
512
548
<p>
513
549
<label for="username" class="screenreader">Username</label>
514
- <input type="text" id="username" name="{{usernameParameter}}" placeholder="Username" required autofocus>
550
+ {{usernameInput}}
515
551
</p>
516
552
<p>
517
553
<label for="password" class="screenreader">Password</label>
@@ -522,6 +558,14 @@ private boolean matches(HttpServletRequest request, @Nullable String url) {
522
558
<button type="submit" class="primary">Sign in</button>
523
559
</form>""" ;
524
560
561
+ private static final String FORM_READONLY_USERNAME_INPUT = """
562
+ <input type="text" id="username" name="{{usernameParameter}}" value="{{username}}" placeholder="Username" required readonly>
563
+ """ ;
564
+
565
+ private static final String FORM_USERNAME_INPUT = """
566
+ <input type="text" id="username" name="{{usernameParameter}}" placeholder="Username" required autofocus>
567
+ """ ;
568
+
525
569
private static final String HIDDEN_HTML_INPUT_TEMPLATE = """
526
570
<input name="{{name}}" type="hidden" value="{{value}}" />
527
571
""" ;
@@ -554,11 +598,19 @@ private boolean matches(HttpServletRequest request, @Nullable String url) {
554
598
{{errorMessage}}{{logoutMessage}}
555
599
<p>
556
600
<label for="ott-username" class="screenreader">Username</label>
557
- <input type="text" id="ott-username" name="username" placeholder="Username" required>
601
+ {{usernameInput}}
558
602
</p>
559
603
{{hiddenInputs}}
560
604
<button class="primary" type="submit" form="ott-form">Send Token</button>
561
605
</form>
562
606
""" ;
563
607
608
+ private static final String ONE_TIME_READONLY_USERNAME_INPUT = """
609
+ <input type="text" id="ott-username" name="username" value="{{username}}" placeholder="Username" required readonly>
610
+ """ ;
611
+
612
+ private static final String ONE_TIME_USERNAME_INPUT = """
613
+ <input type="text" id="ott-username" name="username" placeholder="Username" required>
614
+ """ ;
615
+
564
616
}
0 commit comments