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