2

ここに問題があります。

新しいDATAJPAリポジトリモジュール(インターフェース拡張 CrudRepository<T, ID extends Serializable>)を使用して、Spring3.0.5を使用しています。

アプリのセキュリティソリューションとしてApacheShiro1.1.0を使用しています。Apache shiroは、Spring BeanDefintionxmlファイルで次のように構成されています。

<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> 

私のアプリはスタンドアロンアプリケーションであり、このApachoeShiro構成はそれを反映していることに注意してください。

Spring JPAリポジトリの構成と標準のSpring構成(アノテーションスキャン)は他のファイルで構成されていますが、これらはこの問題に関連していないようですので、印刷をスキップします。

SecurityDAORealmのクラスは、クレデンシャルが格納されているデータベースにアクセスするためのCredentialsRepositoryjpaリポジトリコントローラーインターフェイス( )として自動配線されています。CredentialsRepository extends CrudRepository<T, ID extends Serializable>

@Component
public class SecurityDAORealm extends AuthorizingRealm {

@Autowired
CredentialRepository credentialRepository;
...
}

今問題のために。

Apache Shiroアノテーションスキャンが設定されている場合、タイプの自動配線されたBeanがCredentialsRepository見つからないため、配線されていません。注釈スキャンがオフになっている場合、CredentialsRepository変数は自動配線され、すべてが正しく機能します。

ApacheShiroアノテーション処理が可能な部分はこちら

    <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適切な実装()を作成する機会がないため、変数が自動配線されていないことです。SimpleJpaRepository

回避策は、スプリングマネージドインターフェイスの実装ではなく、「フルクラス」のJPAコントローラーを自動配線するだけです。

ただし、これが修正が必要なバグなのか、それともスプリングデータインターフェイスでも機能する可能性のある追加のスプリングマジックがここに存在するのか、興味があります。

4

2 に答える 2

5

私は同じ問題に遭遇しました。

私の回避策は、securityManagerを注入する必要のあるリポジトリに依存させることです。したがって、あなたの場合:

<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セキュリティを使用しています。

自動配線したとき、コンポーネントがspring-data @Repositoryをスキャンする前に、SpringセキュリティBeanが初期化されていたため、正しく注入されませんでした。サーブレットフィルタなどを設定するには、コンテナの起動時にSpring Securityを初期化する必要があるため、適切なインジェクションを実行するには、SpringSecurityの前にリポジトリを配線する必要がありました。

これがあなたの状況ではないことを知ってください、しかし多分それは助けになるでしょう!

また、最も紛らわしい部分であり、問​​題の解決策につながったのは、リポジトリが注入されたUserDetailsS​​erviceを@Autowiredする単体テストを設定し、単体テストが正常に機能したことです。Beanのセットアップ方法に問題があると私は信じました。

于 2012-01-11T19:32:24.840 に答える