2011-11-18 29 views
2

ここに問題があります。SpringデータJPAリポジトリが見つからないApache Shiroアノテーションがスプリングで有効になっている場合

私は、新しいDATA JPAリポジトリモジュール( CrudRepository<T, ID extends Serializable>を拡張するインターフェイス)を使用して、3.0.5のスプリングを使用しています。

私はApache Shiro 1.1.0を私のアプリケーションのセキュリティソリューションとして使用しています。

<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史郎構成はそれを反映していることに注意してください:次のようにApacheの四郎は、春の豆defintion xmlファイルに を設定されています。

標準のスプリング構成(注釈スキャン)と同様に、スプリングjpaリポジトリの構成は、他のファイルで構成されているため、この問題には関係していないと思われます。

私のSecurityDAORealmクラスは、資格情報が保存されているデータベースにアクセスするためのjpaリポジトリコントローラインターフェイス(CredentialsRepository extends CrudRepository<T, ID extends Serializable>)として、CredentialsRepositoryをautowiringしています。

@Component 
public class SecurityDAORealm extends AuthorizingRealm { 

@Autowired 
CredentialRepository credentialRepository; 
... 
} 

問題が発生しました。

Apache Shiro注釈スキャンが設定されている場合、タイプCredentialsRepositoryの自動受注のBeanが見つからないため、配線されません。注釈スキャンがオフになっていると、CredentialsRepository変数は自動で実行され、すべて正常に動作します。 Apacheの史郎の注釈処理をanables

一部は

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

注釈が、それはそれらを再びオンにしますコメントを外し、オフにされ、その中心の平和をコメントアウトすることで、この

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

です。

私はCredentialsRepositoryの代わりにシンプルなpojoを自動配線しようとしましたが、これは両方のケース(アノテーションのオン/オフ)でうまくいきます。

私は春の内部をあまり見ません。ここで起こっていることは、Springがバックエンドに適切な実装(SimpleJpaRepository)を作成する機会を得られないため、CredentialsRepository変数が自動配線されていないことです。

バグ管理されたインターフェイスの実装ではなく、「フルクラス」のJPAコントローラを自動配線するだけで回避できます。

しかし、これは修正が必要なバグか、スプリングデータインターフェイスでも動作させることができる追加のスプリングマジックが存在するかどうか不思議です。

答えて

5

私も同じ問題を抱えていました。

私の回避策は、securityManagerを注入する必要のあるリポジトリに依存させることです。このように、あなたのケースで:

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

+1を取る前に、史郎がinstantiantedなっていることです。ありがとう – Justin

+0

+1助けて! – sunny

+0

私はshiroをアプリケーションに統合しているので、これはセキュリティモジュールのために働いていました。今度は ' sunny

3

私はこれと同様の状況がありましたが、私はセキュリティを行うために白を使用していませんが、私は春のセキュリティを使用しています。

私がautowiredのとき、spring-data @Repositoryのコンポーネントスキャンの前にスプリングセキュリティビーンが初期化されているため、正しく注入されませんでした。サーブレットフィルタなどを設定するには、コンテナの起動時に春のセキュリティを初期化する必要があるため、適切な注入が行われるようにセキュリティを確保する前にリポジトリを配線する必要がありました。

これはちょうどあなたの座談ではありませんが、おそらく助けになるかもしれません!

また、最も混乱している問題と私の問題解決のために私は@AutowiredというUserDetailsS​​erviceのユニットテストをセットアップしました。このテストでは、リポジトリに注入されたユニットテストが正常に動作しました。私はそれが豆がセットアップされている方法の順序問題であったと信じてくれました。

+2

私は、あなたが正しいセキュリティである「インスタンス化」のリポジトリ順序を考えて、それがリポジトリスキャンが、これは本当に助けた場所 –

関連する問題