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" />添加到控制加载controller的xml中
有点类似于shiro中的Controller的方法级AOP安全控制
1、<!-- 支持Shiro对Controller的方法级AOP安全控制 -->
<bean class="org.s

在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" />。
最低0.47元/天 解锁文章
2885

被折叠的 条评论
为什么被折叠?



