spring - Spring Security,自定义登录表单: display custom message matching SPRING_SECURITY_LAST_EXCEPTION

标签 spring error-handling spring-security type-mismatch

我正在实现Spring Security&我有一个自定义登录表单,例如:

<c:if test="${not empty param.error}">
  <div class="errorblock">
    LOGIN ERROR<br/>
    Reason: ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}<br/>
  </div>
</c:if>

<form method="POST" action="<c:url value="/j_spring_security_check"/>">
    ...
</form>

到目前为止,到目前为止,显示的原因与预期的一样(即,如果我的数据库已关闭,则为Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed)

我希望能够显示更加用户友好的消息,例如“数据库问题,请联系管理员”

我尝试将typeMismatch.org.hibernate.TransactionException=Unable to connect to databse, please contact an admin添加到我已用于覆盖Spring安全身份验证异常的messages.properties中,请参阅how to display custom error message in jsp for spring security auth exception

我不太确定如何“调用”此自定义错误消息。

谢谢你的帮助。

最佳答案

您引用的线程中的方法仅在您想要更改现有Spring消息的消息时才起作用,您可以在安全核心jar内的message.properties文件中看到该消息。因此,这对您要尝试执行的操作无效。您可以将发生故障的操作定向到另一个 Controller (请参阅authentication-failure-url),并在该处执行任何所需的自定义错误处理,然后显示登录表单 View ,该 View 显示您生成的消息。

关于spring - Spring Security,自定义登录表单: display custom message matching SPRING_SECURITY_LAST_EXCEPTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12973984/

相关文章:

java - Spring boot 项目生成问题

java - 在带有嵌入式 Tomcat 的 Spring Boot 中使用 ThreadLocal 来保存每个请求的数据是否安全

java - 使用 java.time 进行 hibernate

objective-c - 如何将可抛出的 Swift 初始化程序与 Objective-C 代码桥接?

bash - 如果语法错误,如何使 bash 函数返回错误

Java错误处理,正确的处理方法是什么?

java - Spring access-denied-handler 可以引用弹出窗口吗?

Spring:注解等价于 security:authentication-manager 和 security:global-method-security

java - bean 的 Autowiring 问题

Spring Security 删除 RoleVoter 前缀