繁体   English   中英

在扩展另一个类的控制器的方法上使用Spring Security注释

[英]Using Spring Security annotation on a method of a controller that extends another class

对此示例的 spring-security bean配置非常相似。 控制器方法上的@Secured注释只有在不是另一个类的子类的方法上才能正常工作。 换句话说,以下代码不起作用(在bean初始化期间引发的异常):

@Controller
@RequestMapping("/systeminfo")
public class SystemInfoController extends AbstractViewableController {

    @RequestMapping(method = RequestMethod.GET, value = "/")
    @Secured("ROLE_USER") // an exception below was raised
    public void view(HttpServletRequest request) {
    }
}

这是一个例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'systemInfoController' defined in file [C:\workspace\my\my-webapp
\target\classes\my\webapp\controller\SystemInfoController.class]: Initializa
tion of bean failed; nested exception is org.springframework.aop.framework.AopCo
nfigException: Could not generate CGLIB subclass of class [class my.webapp.c
ontroller.SystemInfoController]: Common causes of this problem include using a f
inal class or a non-visible class; nested exception is net.sf.cglib.core.CodeGen
erationException: java.lang.RuntimeException-->RequestMapping annotation cannot
be found on my.webapp.controller.SystemInfoController$$EnhancerByCGLIB$$e99f
e366

所以我按照这里的说明将proxy-target-class="true"<global-method-security ...> (不确定它是否相关)但安全方面仍然丢失。 但是,如果删除了超类,则正确应用安全性,即转发到登录页面。

当控制器需要扩展另一个类时,有谁知道发生了什么以及如何解决问题?

您是否在security.xml文件中启用了安全注释

<global-method-security secured-annotations="enabled" />

而且,这些注释仅适用于Spring bean。 您需要注入这些bean而不仅仅是创建普通实例。 尝试使用Spring Dependency Injection。

这很可能是spring bean工厂,spring注释扫描程序或使用CGLIB生成子类的错误。 Spring不应该直接扫描CGLIB生成的子类上的注释,因为它们会丢失。 它应该扫描其目标类。 在这种情况下,它试图扫描@ResquestMappingmy.webapp.controller.SystemInfoController$$EnhancerByCGLIB$$e99fe366

但是,如果SystemInfoController没有扩展一个abstrat类,那么整个过程就可以了。 它表示只有当控制器是子类时才会丢失注释,这表明这可能是生成代理子类或CGLIB限制的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM