0

春のセキュリティ構成に複数の http ブロックがあります。これは、Spring Security バージョン 3.1 以降で許可されています。この問題は、SpringSocial 構成が RequestCache オブジェクトを自動注入しようとしたときに発生します。次のエラーが表示されます: No qualing Bean of type [org.springframework.security.web.savedrequest.RequestCache] is defined: expected single matching Bean but found 3. 自動配線された RequestCache オブジェクトへの Java コード内の参照を削除すると、 xml 構成に 3 つの http ブロックが含まれていても問題ありません。このエラーを修正する最善の方法は何ですか? 正しい RequestCache を自動配線する方法はありますか?

xml http ブロック:

<http pattern="/oauth/token"...>
</http>

<http pattern="/something/**...>
</http>

<http use-expressions="true"...>
</http>

Java Config には、自動配線された RequestCache への参照があります。

@Bean
public ProviderSignInController providerSignInController(RequestCache requestCache) {
    return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(), new SimpleSignInAdapter(requestCache, userService));
} 

これらのいずれも、それ自体で問題を引き起こすことはありません。ただし、複数の http ブロックと自動配線された RequestCache があると、次のようになります。

これを正しく設定する方法を教えてくれる人はいますか?

4

1 に答える 1

6

問題は、各ブロックが RequestCache の独自のインスタンスを作成することです。RequestCache のデフォルトの実装は同じデータ ストア (つまり HttpSession) を使用するため、新しいインスタンスを使用してもブロック間で機能します。ただし、自動配線を使用しようとすると、使用するインスタンスがわかりません。代わりに、HttpSessionRequestCache の独自のインスタンスを作成して使用します (http ブロックと同じように)。例えば:

@Bean
public ProviderSignInController providerSignInController() {
    RequestCache requestCache = new HttpSessionRequestCache();  
    return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(), new SimpleSignInAdapter(requestCache, userService));
}

動作するはずの代替手段は、構成で RequestCache を指定し、request-cache 要素を使用して同じインスタンスを再利用することです。例えば:

<bean:bean class="org.springframework.security.web.savedrequest.HttpSessionRequestCache"
        id="requestCache" />

<http pattern="/oauth/token"...>
    <request-cache ref="requestCache"/>
</http>

<http pattern="/something/**...>
    <request-cache ref="requestCache"/>
</http>

<http use-expressions="true"...>
    <request-cache ref="requestCache"/>
</http>

RequestCache インスタンスは 1 つしかないため、既存の Java Config を使用できます (つまり、RequestCache を自動配線します)。必要に応じて、XML ではなく Java Config で RequestCache を簡単に作成できることに注意してください。

于 2013-01-25T20:58:31.490 に答える