如何在Spring Security 3中为匿名用户从数据库加载角色?

发布于 2024-08-23 09:35:26 字数 1600 浏览 13 评论 0原文

我正在使用 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>

我的匿名身份验证提供程序从未被调用,因此我无法从数据库加载自定义权限。 当我登录时,我的服务 accountDetails 被调用,我可以从数据库中为用户加载角色,我希望匿名用户也能做同样的事情。

我该怎么办?谢谢

I'm using Spring Security 3.0.2 and I can't find a way to load roles of anonymous user from database (I've got dynamic roles where roles can be given to everyone).

I've tried to use a custom anonymousAuthenticationProvider but this provider is never called. Here is my config:

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

My anonymousAuthenticationProvider is never called so I can't load custom authorities from database.
When I log in, my service accountDetails is called and I can load roles from database for the user, I want the same thing for anonymous user.

How can I do it ? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-08-30 09:35:26

实现它的最简单方法似乎是使用自定义的 UserAttribute 声明 AnonymousAuthenticationFilter ,这将生成所需的权限:

<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 = "..." />

It seems to be that the easiest way to achieve it is to declare a AnonymousAuthenticationFilter with a custom UserAttribute, which will produce the required authorities:

<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 = "..." />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文