在 Spring Security 中处理 DataAccessException
现在,我已经使用 Spring Security 使用基本身份验证来保护应用程序。用户详细信息来自 JDBC 源。如果数据库出现故障,用户加载机制的内部将抛出DataAccessException
。默认身份验证提供程序类 DaoAuthenticationProvider
捕获异常并将其映射回 AuthenticationServiceException
。这种映射的最终结果是浏览器/客户端收到 HTTP 代码 401。
我想做的是以不同的方式处理数据库不可用。至少,我希望通过 HTTP 503 响应来处理此问题,但我更希望将其重定向到错误页面。我怎样才能实现这个目标?
编辑: Ritesh 的解决方案部分正确。除了实现您自己的基本入口点之外,缺少的步骤还在于使用安全架构的 v3.0.3,以便
元素具有 entry-point- ref
属性。如果您不使用此特殊属性,则默认的 Basic 过滤器将始终使用其自己的 Basic 入口点实现。
Right now I've got Spring Security protecting an application using basic authentication. The user details are coming from a JDBC source. If the database goes down, the internals of the user loading mechanism will throw a DataAccessException
. The default authentication provider class, DaoAuthenticationProvider
, catches the exception and maps it back to an AuthenticationServiceException
. The end result of such a mapping is that the browser/client receives HTTP code 401.
What I want to do is to handle database unavailability in a different way. At the very least, I want this to be handled by responding with HTTP 503 but I would prefer if it redirected to an error page. How can I achieve this?
EDIT: Ritesh's solution was partially correct. The missing steps apart from implementing your own Basic entry point is to also use v3.0.3 of the security schema so that the <http-basic/>
element has the entry-point-ref
attribute. If you don't use this special attribute, the default Basic filter will always use its own Basic entry point implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BasicAuthenticationEntryPoint
发送 401 表示AuthenticationException
。您可以创建自己的自定义入口点来处理 AuthenticationServiceException 并发送 503。其他选项是不在入口点中执行任何操作并使用 SimpleMappingExceptionResolver 和/或实现您自己的 <代码>HandlerExceptionResolver。
The
BasicAuthenticationEntryPoint
sends 401 forAuthenticationException
. You can create your own custom entry point to handleAuthenticationServiceException
and send out 503.Other option is not to do anything in entry point and use
SimpleMappingExceptionResolver
and/or implement your ownHandlerExceptionResolver
.