6

Spring Security 3.0.2を使用していますが、データベースから匿名ユーザーのロールをロードする方法が見つかりません(ロールを全員に付与できる動的なロールがあります)。

カスタムanonymousAuthenticationProviderを使用しようとしましたが、このプロバイダーが呼び出されることはありません。これが私の設定です:

<http auto-config="false">
    <logout invalidate-session="true" logout-success-url="/page/welcome" />
    <remember-me />
    <anonymous username="_GUEST_" granted-authority="ROLE_GUEST" key="anonymousKey" />
    <form-login login-page="/page/login" authentication-failure-url="/page/login?fail=1" default-target-url="/page/welcome" />
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="anonymousAuthenticationProvider"></authentication-provider>
    <authentication-provider user-service-ref="accountDetails">
        <password-encoder ref="passwordEncoder">
            <salt-source user-property="xxxx" />
        </password-encoder>
    </authentication-provider>
</authentication-manager>

<beans:bean id="accountDetails" class="com.mysite.AccountDetailsImpl" />

<beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
    <beans:constructor-arg value="512" />
</beans:bean>

<beans:bean id="anonymousAuthenticationProvider" class="com.my.site.security.CustomAnonymousAuthenticationProvider">
    <beans:property name="key" value="anonymousKey" />
</beans:bean>

私のanonymousAuthenticationProviderが呼び出されることはないため、データベースからカスタム権限をロードできません。ログインすると、サービスaccountDetailsが呼び出され、ユーザーのデータベースからロールをロードできます。匿名ユーザーにも同じことが必要です。

どうすればいいですか?ありがとう

4

1 に答える 1

4

それを達成する最も簡単な方法はAnonymousAuthenticationFilter、カスタムUserAttributeでを宣言することであるように思われます。これにより、必要な権限が生成されます。

<http auto-config = "false">
    <anonymous enabled = "false" />
    <custom-filter ref = "myFilter" position = "ANONYMOUS_FILTER" />
    ...
</http>

<beans:bean id = "myFilter" class = "org.springframework.security.web.authentication.AnonymousAuthenticationFilter">
    <beans:property name = "key" value = "anonymousKey" />
    <beans:property name = "userAttribute" ref = "myAttributes" />
</beans:bean>

<beans:bean id = "myAttributes" class = "..." />
于 2010-03-03T16:00:24.870 に答える