在Spring中启用Apache Shiro注释时找不到Spring数据JPA存储库

Tom*_*lka 2 spring repository shiro

这是问题所在.

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

我有Apache Shiro 1.1.0作为我的应用程序的安全解决方案.Apache shiro在spring bean defintion 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> 
Run Code Online (Sandbox Code Playgroud)

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

spring jpa存储库的配置以及标准弹簧配置(注释扫描)在其他文件中配置,它们似乎与此问题无关,因此我将跳过它们.

我的SecurityDAORealm类是自动装配CredentialsRepository的jpa存储库控制器接口(CredentialsRepository extends CrudRepository<T, ID extends Serializable>),用于访问存储凭据的数据库.

@Component
public class SecurityDAORealm extends AuthorizingRealm {

@Autowired
CredentialRepository credentialRepository;
...
}
Run Code Online (Sandbox Code Playgroud)

现在为了这个问题.

配置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> 
Run Code Online (Sandbox Code Playgroud)

通过评论其中心和平

<!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/> -->
Run Code Online (Sandbox Code Playgroud)

注释被关闭,取消注释它将再次打开它们.

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

春天的内部我看不太多.这里可能发生的是CredentialsRepository变量没有自动连接,因为Spring没有机会SimpleJpaRepository在其后端创建适当的实现().

这里的解决方法只是通过自动连接一些"全类"JPA控制器而不是弹簧管理接口实现.

但是我很好奇这是否是一个需要修复的bug,或者是否存在一些额外的弹簧魔法可以使它与spring数据接口一起工作.

小智 5

我遇到了同样的问题.

我的解决方法是使securityManager依赖于需要注入的Repository.因此,在您的情况下:

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"
  depends-on="userRepository">
    <property name="realm" ref="credentialRepository" />
</bean>
Run Code Online (Sandbox Code Playgroud)