spring-boot - Spring boot Authentication Failure Handler - 自定义,没有额外的 url 或查询参数
问题描述
我当前的 Spring Boot Login 实现如下:
登录.html:
<div class="error-message m-l-4" th:if="${errorMessage}" th:text="${errorMessage}"></div>
WebSecurityConfigurerAdapter - 配置方法:
formLogin().failureUrl("/login-error")
请求映射器:
@RequestMapping("/")
public class Root {
@GetMapping("login")
public ModelAndView getLoginView(ModelMap mav) {
return new ModelAndView("login", mav);
}
@GetMapping("login-error")
public RedirectView setErrorMessage(RedirectAttributes attributes) {
attributes.addFlashAttribute("errorMessage", "Username or Password is invalid");
return new RedirectView("login");
}
}
当用户输入无效凭据时,上述工作完美,错误显示在 HTML 上。但是,有没有办法实现相同的结果,但摆脱额外的目录 /login-error?尽管重定向到出现错误的登录页面,但我不希望它可以访问。我试图翻译自定义 AuthenticationFailureHandler (.failureHandler(new CustomAuthenticationFailureHandler())) 以产生相同的效果,但我只能返回 JSON 而不是 HTML。基本上,我想要与 Instagram 登录页面完全相同的行为。
解决方案
推荐阅读
- javascript - 如何知道一个对象是可见的还是隐藏的?
- excel - 连接两列的信息
- php - 使用 PDO 连接到远程 Ms-SQL 服务器
- vba - 循环遍历工作表 a 中的每个单元格,如果工作表 B 中的值为 true,则将单元格值替换为“保留”
- epplus - 打开受密码保护的 .xls 文件
- android - 如何从颤动中的复选框选择中生成项目列表?
- php - 与哨兵一起使用时 Laravel 队列停止
- mysql - Spring Cloud 数据流未连接到 MySQL DB
- sql - 如何优化这个嵌套的 SQL SELECT 查询
- c# - DBcontext 在注册为 Scoped 时被共享