Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.config.ObjectPostProcessor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.test.SpringTestContext;
import org.springframework.security.config.test.SpringTestContextExtension;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.userdetails.PasswordEncodedUser;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
Expand Down Expand Up @@ -75,6 +77,8 @@ public class DefaultLoginPageConfigurerTests {
@Autowired
MockMvc mvc;

MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();

@Test
public void getWhenFormLoginEnabledThenRedirectsToLoginPage() throws Exception {
this.spring.register(DefaultLoginPageConfig.class).autowire();
Expand Down Expand Up @@ -144,6 +148,8 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {
this.mvc.perform(get("/login?error").session((MockHttpSession) mvcResult.getRequest().getSession())
.sessionAttr(csrfAttributeName, csrfToken))
.andExpect((result) -> {
String badCredentialsLocalizedMessage = this.messages
.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials");
CsrfToken token = (CsrfToken) result.getRequest().getAttribute(CsrfToken.class.getName());
assertThat(result.getResponse().getContentAsString()).isEqualTo("""
<!DOCTYPE html>
Expand All @@ -160,7 +166,7 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {
<div class="content">
<form class="login-form" method="post" action="/login">
<h2>Please sign in</h2>
<div class="alert alert-danger" role="alert">Bad credentials</div>
<div class="alert alert-danger" role="alert">%s</div>
<p>
<label for="username" class="screenreader">Username</label>
<input type="text" id="username" name="username" placeholder="Username" required autofocus>
Expand All @@ -178,7 +184,7 @@ public void loginPageWhenErrorThenDefaultLoginPageWithError() throws Exception {

</div>
</body>
</html>""".formatted(token.getToken()));
</html>""".formatted(badCredentialsLocalizedMessage, token.getToken()));
});
// @formatter:on
}
Expand Down