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

标签 spring repository shiro

这是问题所在。

我有 spring 3.0.5,使用它的新 DATA JPA 存储库模块(接口(interface)扩展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 配置反射(reflect)了它。

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

我的 SecurityDAORealm类是 Autowiring CredentialsRepository作为其 jpa 存储库 Controller 接口(interface) ( CredentialsRepository extends CrudRepository<T, ID extends Serializable> ),用于访问存储凭据的数据库。
@Component
public class SecurityDAORealm extends AuthorizingRealm {

@Autowired
CredentialRepository credentialRepository;
...
}

现在解决问题。

配置 Apache Shiro 注释扫描时,类型为 CredentialsRepository 的 Autowiring 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 而不是我的 CredentialsRepository ,这在两种情况下都很好用(注释开/关)。

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

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

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

最佳答案

我遇到了同样的问题。

我的解决方法是让 securityManager 依赖于需要注入(inject)的存储库。因此,在您的情况下:

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"
  depends-on="userRepository">
    <property name="realm" ref="credentialRepository" />
</bean>

关于Spring 中启用 Apache Shiro 注释时未找到 Spring 数据 JPA 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8177548/

相关文章:

spring - 向多个 RabbitMQ 消费者提供相同的消息

spring - POST 方法读取查询参数而不是表单字段

git - 如何在本地计算机上进行 git rebase 后删除存储库中的 "branch"

node.js - TypeORM 和 MongoDB 和存储库 : Cannot read property 'prototype' of undefined

c# - 如何使用 Simple injector、Repository 和 Context - code first

java - 从 Shiro 主题获取 grails 域对象

grails - 用密码登录Apache Shiro Grails中的哈希

java - 希罗+吉斯 : Why IniRealm is instantiated four times?

java - 是否可以让 ObjectProvider 提供来自其他包的对象?

java - 使用 JPA 通过一列连接并返回另一列