【问题标题】:NoSuchBeanDefinitionException when add @PreAuthorize annotation添加@PreAuthorize注解时出现NoSuchBeanDefinitionException
【发布时间】:2012-04-10 03:38:34
【问题描述】:

我正在尝试执行我的 GWT 应用程序和 Spring Security 的集成。当我在我的 DAO 类的方法中添加 @PreAuthorize("hasRole('ROLE_USER')") 注释时,会出现以下异常:

没有定义类型 [server.dao.ElementServiceDAO] 的唯一 bean: 预期单个 bean,但找到 0

DaoServiceLocator 找不到 DAO bean,但在调试模式下,我在 ApplicationContext 实例中看到 elementServiceDAO bean。

我的 DAO 类如下所示:

@Service
public class ElementServiceDAO extends EntityDAO {

@PreAuthorize("hasRole('ROLE_USER')")
@SuppressWarnings("unchecked")
public Layer getFullRenderingTopology() {
...
}

}

DAO 服务定位器代码:

public class DaoServiceLocator implements ServiceLocator {

@Override
public Object getInstance(final Class<?> clazz) {
    try {
        final HttpServletRequest request = RequestFactoryServlet
                .getThreadLocalRequest();

        final ServletContext servletContext = request.getSession()
                .getServletContext();

        final ApplicationContext context = WebApplicationContextUtils
                .getWebApplicationContext(servletContext);

        return context.getBean(clazz);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
}

applicationContext-security.xml:

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="operator" password="operator" authorities="ROLE_USER, ROLE_ADMIN" />
            <user name="guest" password="guest" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

<global-method-security pre-post-annotations="enabled" /> 

请给我任何建议!

【问题讨论】:

    标签: spring gwt spring-security requestfactory


    【解决方案1】:

    ElementServiceDAO 实现一个接口时(在你的情况下——通过EntityDAO 传递),Spring 默认创建一个基于接口的代理来应用安全方面。因此,您的应用程序上下文中的elementServiceDAO 是不是ElementServiceDAO 实例的代理,因此无法按类型检索。

    你要么需要

    • 强制创建基于目标类的代理,如下所示

      <global-method-security 
          pre-post-annotations="enabled" proxy-target-class = "true" /> 
      
    • 或为ElementServiceDAO 创建一个业务接口并使用该接口而不是实现类。

    另请参阅:

    【讨论】:

    • 非常感谢!你的第一个假设对我不起作用,但第二个是我正在寻找的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多