2

这是问题所在。

我有 spring 3.0.5,使用它的新 DATA JPA 存储库模块(接口扩展 CrudRepository<T, ID extends Serializable>)。

我有 Apache Shiro 1.1.0 作为我的应用程序的安全解决方案。Apache shiro在spring bean定义xml文件中配置如下:

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- Enable Shiro Annotations for Spring-configured beans.  Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean> 

<!-- Define the realm you want to use to connect to your back-end security datasource: -->
<bean id="securityDAORealm" class="com.bilto.archiweb.security.SecurityDAORealm" />

<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
    <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
    <property name="realm" ref="securityDAORealm"/>
</bean>

<!-- For simplest integration, so that all SecurityUtils.* methods work in all cases, -->
<!-- make the securityManager bean a static singleton.  DO NOT do this in web         -->
<!-- applications - see the 'Web Applications' section below instead.                 -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
    <property name="arguments" ref="securityManager"/>
</bean> 

请注意,我的应用程序是一个独立的应用程序,这个 Apachoe Shiro 配置反映了它。

spring jpa 存储库的配置以及标准 spring 配置(注释扫描)在其他文件中配置,它们似乎与这个问题无关,所以我将跳过打印它们。

我的SecurityDAORealmCredentialsRepository作为其 jpa 存储库控制器接口 ( CredentialsRepository extends CrudRepository<T, ID extends Serializable>) 自动装配,用于访问存储凭据的数据库。

@Component
public class SecurityDAORealm extends AuthorizingRealm {

@Autowired
CredentialRepository credentialRepository;
...
}

现在解决问题。

配置 Apache Shiro 注释扫描时,CredentialsRepository找不到类型的自动装配 bean,因此未装配。关闭注释扫描后,CredentialsRepository 变量会自动关联并且一切正常。

启用 Apache Shiro 注释处理的部分是这个

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- Enable Shiro Annotations for Spring-configured beans.  Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean> 

通过注释掉它的中心和平

<!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/> -->

注释已关闭,取消注释将再次打开它们。

作为测试,我尝试自动连接一个简单的 pojo 而不是 my CredentialsRepository,这在两种情况下都很好用(注释开/关)。

我对弹簧内部的了解不多。这里可能发生的情况是CredentialsRepository变量没有自动连接,因为 Spring 没有机会SimpleJpaRepository在其后端创建其适当的实现 ()。

解决方法只是通过自动连接一些“全类”JPA 控制器而不是 spring 管理接口实现来实现。

但是我很好奇这是否是一个需要修复的错误,或者这里是否存在一些额外的弹簧魔法可以使其也适用于弹簧数据接口。

4

2 回答 2

5

I came accross the same issue.

My workaround is to make the securityManager depend on the Repository which needs to be injected. Thus, in your case:

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"
  depends-on="userRepository">
    <property name="realm" ref="credentialRepository" />
</bean>
于 2012-02-10T13:37:09.943 回答
3

我有类似的情况,我没有使用 shiro 来做安全,但我使用的是 spring security。

当我自动装配时,由于在组件扫描 spring-data @Repository 之前初始化了 spring 安全 bean,它没有正确注入。由于 spring security 需要在容器启动时初始化以设置 servlet 过滤器等,因此我必须在 spring security 之前连接我的存储库,以便进行正确的注入。

知道这不完全是您的情况,但也许它会有所帮助!

此外,最令人困惑的部分,也是导致我解决问题的原因,我设置了一个单元测试,@Autowired UserDetailsS​​ervice 将存储库注入其中,并且单元测试运行良好。让我相信这是设置 bean 的方式的订购问题。

于 2012-01-11T19:32:24.840 回答