Spring Security 中基于角色的访问拒绝处理 - 如何?

发布于 2025-01-03 02:17:57 字数 414 浏览 3 评论 0原文

我正在使用 Spring Security 开发基于 Spring 的应用程序。我有具有不同角色的用户,并且希望根据角色实施访问拒绝处理。更具体地说,期望的效果如下:当用户尝试访问不允许他访问的资源时,我想区分由于用户未经身份验证而拒绝的情况和由于用户未经身份验证而拒绝的情况权限不足(角色错误)。未经身份验证的用户可以被定向到常规 403 页面,但那些角色错误的用户我想重定向到一个表单,他们可以在其中请求授予适当的角色。

我正在考虑添加“”并实现 xyz 控制器或以某种方式(尚未确定如何)将其实现为自定义过滤器。

你会如何解决这个问题?有没有最佳实践?试图用谷歌搜索一些例子,因为我认为这是一种非常常见的模式,但没有成功。

多谢。

I'm working on Spring based application with Spring Security. I have users with different roles and would like to implement access denial handling depending on the role. More specifically the desired effect would be the following: when user tries to access a resource that he is not allowed to, I would like to distinguish between the case when the denial is because the user is not authenticated and the case when it is due to insufficient privileges (wrong role). The users who are not authenticated can be directed to the regular 403 page, but the ones with just a wrong role I would like to redirect to a form in which they could request granting appropriate role.

I'm thinking about either adding the "<access-denied-handler error-page="/xyz">" and implementing the xyz controller or somehow (not yet sure how) implement this as a custom filter.

How would you tackle the problem? Is there any best practice for that? Was trying to google for some examples as I would consider this as a quite common pattern, but no success.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2025-01-10 02:17:57

你会如何解决这个问题?

我会尝试实现我的自定义 AccessDeniedHandler 并尝试在 org.springframework.security.web.access.ExceptionTranslationFilter(阅读其类 JavaDoc!)

How would you tackle the problem?

I would try to implement my custom AccessDeniedHandler and try to "register" it in the org.springframework.security.web.access.ExceptionTranslationFilter (read its class JavaDoc!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文