gpt4 book ai didi

java - @Secured 和 @PreAuthorize 在 Controller 中工作正常,但在服务级别中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:01 25 4
gpt4 key购买 nike

可能是什么问题?当我在 Controller 中使用此注释时,它工作正常,该页面只能由 ROLE_USER 访问

@Secured("ROLE_USER") 

此注释也有效:

@PreAuthorize("hasRole('ROLE_USER')")

当我将相同的注释移动到我的服务时,它不起作用,该方法无需 ROLE_USER 角色即可访问!我正在测试Spring Security,My Service只是一个测试(TestService接口(interface)及其实现),无论我把注解放在接口(interface)还是实现层面,就好像注解不存在一样。

这是我的dispatcher-servlet.xml 文件:

<security:global-method-security secured-annotations="enabled" pre-post-annotations="enabled"/>
<bean id="TestService" class="myPackage.TestServiceImpl"/>

感谢您的帮助

最佳答案

@M. Deinum said ,问题是您的 enable-global-method-security 仅在 DispatchereServlet 的上下文中,而不在 ContextLoaderListener 的上下文中(它是另一个的父级)。您的服务 bean 可能属于 ContextLoaderListener 上下文,您的 Controller 可能属于 DispatchereServlet。如@M. Deinum mentioned@Secured@PreAuthorize 基于 AOP,并且仅在与 enable-global-method-security 相同的上下文中工作。

TL;DRsec:enable-global-method-security 添加到 ContextLoaderListener 的上下文(可能是 web.xml 中名为 contextConfigLocation 的参数)。

关于java - @Secured 和 @PreAuthorize 在 Controller 中工作正常,但在服务级别中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29048147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com