How can I get user authentication error (bad username/password, disabled/locked/expired account) in @Controller and add this error to model attributes to show in Thymeleaf template? I handle it in WebSecurityConfigurerAdapter
http
.formLogin()
.loginPage("/login")
.failureUrl("/login/error")
and process in @Controller method
@RequestMapping("/login/error")
public String loginError(Model model) {
model.addAttribute("loginError", true);
return "login";
}
but what method parameter I should use to do this? It's possible with HttpSession (it contains authentication exception object), but I don't think it's a good idea.
You can print the authentication error message with the following Thymeleaf code:
<p data-th-if="${loginError == true && session.SPRING_SECURITY_LAST_EXCEPTION != null}"
data-th-text="${session.SPRING_SECURITY_LAST_EXCEPTION.message}">
Bad credentials.
</p>
No additional code in controller needed.