spring security:两个访问被拒绝的页面/处理程序取决于访问的资源
例如,我有:
<intercept-url pattern="/aaa/**" access="ROLE_AAA" />
<intercept-url pattern="/bbb/**" access="ROLE_BBB" />
并且,相应地,如果用户想要页面 /aaa 并且同时没有角色 ROLE_AAA - 我想让他重定向到页面 /access-denied-aaa/
并且,如果他试图获取 /bbb并且没有 ROLE_BBB ->到页面/access-denied-bbb/。
目前我只能描述一个AccessDeniedHandler,一个通用的accessDenied页面......
通常如何实现?最好使用 Spring Security
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以实现自己的
AccessDeniedHandler
。这是我通过扩展默认的AccessDeniedHandlerImpl
(省略包和导入):以下是在应用程序上下文配置文件中配置它的方法:
不要忘记告诉 Spring Security在您的
http
部分中使用它:首先,请记住:这只是一个想法。它有效,但可以改进。
基本上它有一个映射,其中键是受保护的资源,值是它们的错误页面。它有一个默认的。
它使用
AntPathMatcher
确定当前请求的错误页面,以便您可以正常使用 ant 路径。在确定错误页面是什么后,该类只需调用handle
其超类 (AccessDeniedHandlerImpl
) 的方法。更好的配置方法是这样的:
但我找不到一种方法来知道用户没有的角色是什么导致了错误。如果您查看
RoleVoter
代码,可以看到这条信息丢失了。You can always implement your own
AccessDeniedHandler
. Here's an example I made by extending the defaultAccessDeniedHandlerImpl
(package and imports omitted):Here's how you configure it in your application context configuration file:
Don't forget to tell Spring Security to use it, in your
http
section:First of all, remember: this is just an idea. It works, but it can be improved.
Basically it has a Map in which the keys are the protected resources, and the values are their error pages. And it has a default one.
It uses an
AntPathMatcher
to determine the error page for the current request so you can normally use ant paths. After it decides what is the error page, the class just call thehandle
method of it's superclass (AccessDeniedHandlerImpl
).A better way to configure it would be something like:
But I couldn't find a way to know what was the role the user didn't had that caused the error. If you look at the
RoleVoter
code, you can see that this information is lost.