现在我正在使用Spring Boot做项目。我想在登录失败时显示错误消息。当我做Maven项目时,我用BindingResult很容易做到。我按照几个指示完成了我想做的任务。但我无法得到任何适当的解决方案。我需要以最简单的方式找到合适的解决方案。
这是我Controller
课
@RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
public String checkLogin(@RequestParam String roll, @RequestParam String pass) {
if (studentService.existsByRollAndPass(roll, pass)) {
return "Welcome";
} else {
return "Login";
}
我的html
文件是
<form class="form-horizontal" method="post" action="/loginCheck">
<fieldset>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Roll</label>
<div class="col-md-4">
<input id="textInput" name="roll" type="text"
placeholder="Roll" class="form-control input-md">
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password">Password</label>
<div class="col-md-4">
<input id="password" name="pass" type="password"
placeholder="password" class="form-control input-md">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="login"></label>
<div class="col-md-4">
<button id="login" name="login" class="btn btn-primary">Login</button>
</div>
</div>
</fieldset>
</form>
您可以在 URL 中放置一个错误参数,例如 localhost:8080/login?error。然后,在Thymeleaf的帮助下,您可以将此div添加到html中。
<div th:if="${param.error}" class="alert alert-danger">
Invalid username and password.
</div>
如果 URL 处存在错误参数,将显示此div。
但是,您可能需要更改
return "Login"
自
return "redirect:Login?error";
浪费了3个小时后,我解决了这个问题。我更改了html
标题
<html xmlns:th="http://www.w3.org/1999/xhtml" lang="en">
自
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sf="http://www.w3.org/1999/xhtml" lang="en">
并在Controller
班上增加了Model
。现在我的控制器类是
@RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
public String checkLogin(@RequestParam String roll, @RequestParam String pass, Model model) {
if (studentService.existsByRollAndPass(roll, pass))
{
return "Welcome";
}
else
{
model.addAttribute("logError","logError");
return "Login";
}
}
为了捕获错误,我将一行代码放在html
文件中。
<span th:if="${logError != null}" class="error">Wrong user or password</span>
现在一切正常!!
您还可以通过实现 AuthenticationFailureHandler 并使用参数重定向来指示存在登录错误来执行此操作。
在扩展 WebSecurityConfigurerAdapter 的类中,在 configure 方法的重写中,确保添加 failureHandler
@Override
protected void configure(HttpSecurity http) throws Exception {
...
.failureHandler(getAuthenticationFailureHandler())
...
}
@Bean
public AuthenticationFailureHandler getAuthenticationFailureHandler() {
return new MyAuthenticationFailureHandler();
}
在实现 AuthenticationFailureHandler(例如:MyAuthenticationFailureHandler(的类中,实现 onAuthenticationFailure 方法并重定向到登录页面并向 URL 添加一个参数,例如 loginError=true。
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
AuthenticationException e) throws IOException, ServletException {
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
httpServletResponse.sendRedirect("/login?loginError=true");
}
然后在您的登录.html页面中,使用 ${param.loginError} 检查此 URL 参数。
<p class="text-danger" th:if="${param.loginError}" th:text="'Wrong username or password'"></p>