spring security @PreAuthorize在controller中配置失效解决方法

在Spring MVC与Spring Security整合时,发现在@Controller层使用@PreAuthorize注解没有生效。问题可能源于@Controller和@Service重复扫描导致bean重复创建。由于Spring MVC的Servlet容器是Spring容器的子容器,其内的Controller bean在父容器中不可见,因此Spring Security的注解无法在控制层起作用。解决方法是将在控制层加载controller的XML配置中添加<security:global-method-security pre-post-annotations="enabled" />。
摘要由CSDN通过智能技术生成

springmvc和spring security整合。在@Controller层 加@PreAuthorize注释没效果,囧(´Д`)
分析:一般配置文件是不会报错,但就是不起作用 初步分析如下:
@Controller, @Service不要重复扫描. 会重复创建bean.
spring mvc 的servlet容器是spring 容器的子容器, 里面创建的bean(一般是Controller)父容器无法访问, 如果要在控制层实现aop, spring security的注解要加载控制层才行,也就是spring mvc的配置文件里.
解决方法:
将<security:global-method-security pre-post-annotations="enabled" />添加到控制加载controller的xml中。

// @PreAuthorize("@securityService.hasProtectedAccess()")
// @PreAuthorize("hasRole('ADMIN')")
// 以上需要所作用必须将spring_security在applicationContext和mvc中配置一份,
// 具体原因,可能是他们分别处在不同的容器中缘故。另外使用@securityService.hasProtectedAccess()是不能使用注解方式来生成bean,必须在相应的mvc xml文件下增加bean
 ------》将<security:global-method-security pre-post-annotations="enabled" />添加到控制加载contro
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值