22
22
import org .springframework .beans .factory .annotation .Autowired ;
23
23
import org .springframework .context .annotation .Bean ;
24
24
import org .springframework .context .annotation .Configuration ;
25
+ import org .springframework .context .support .MessageSourceAccessor ;
25
26
import org .springframework .mock .web .MockHttpSession ;
26
27
import org .springframework .security .config .ObjectPostProcessor ;
27
28
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
28
29
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
29
30
import org .springframework .security .config .test .SpringTestContext ;
30
31
import org .springframework .security .config .test .SpringTestContextExtension ;
32
+ import org .springframework .security .core .SpringSecurityMessageSource ;
31
33
import org .springframework .security .core .userdetails .PasswordEncodedUser ;
32
34
import org .springframework .security .core .userdetails .UserDetailsService ;
33
35
import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
@@ -75,6 +77,8 @@ public class DefaultLoginPageConfigurerTests {
75
77
@ Autowired
76
78
MockMvc mvc ;
77
79
80
+ MessageSourceAccessor messages = SpringSecurityMessageSource .getAccessor ();
81
+
78
82
@ Test
79
83
public void getWhenFormLoginEnabledThenRedirectsToLoginPage () throws Exception {
80
84
this .spring .register (DefaultLoginPageConfig .class ).autowire ();
@@ -144,6 +148,8 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {
144
148
this .mvc .perform (get ("/login?error" ).session ((MockHttpSession ) mvcResult .getRequest ().getSession ())
145
149
.sessionAttr (csrfAttributeName , csrfToken ))
146
150
.andExpect ((result ) -> {
151
+ String badCredentialsLocalizedMessage = this .messages
152
+ .getMessage ("AbstractUserDetailsAuthenticationProvider.badCredentials" , "Bad credentials" );
147
153
CsrfToken token = (CsrfToken ) result .getRequest ().getAttribute (CsrfToken .class .getName ());
148
154
assertThat (result .getResponse ().getContentAsString ()).isEqualTo ("""
149
155
<!DOCTYPE html>
@@ -160,7 +166,7 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {
160
166
<div class="content">
161
167
<form class="login-form" method="post" action="/login">
162
168
<h2>Please sign in</h2>
163
- <div class="alert alert-danger" role="alert">Bad credentials </div>
169
+ <div class="alert alert-danger" role="alert">%s </div>
164
170
<p>
165
171
<label for="username" class="screenreader">Username</label>
166
172
<input type="text" id="username" name="username" placeholder="Username" required autofocus>
@@ -178,7 +184,7 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {
178
184
179
185
</div>
180
186
</body>
181
- </html>""" .formatted (token .getToken ()));
187
+ </html>""" .formatted (badCredentialsLocalizedMessage , token .getToken ()));
182
188
});
183
189
// @formatter:on
184
190
}
0 commit comments