【问题标题】:Spring Security - DB Authorization with a default authority?Spring Security - 具有默认权限的数据库授权?
【发布时间】:2011-02-03 21:42:13
【问题描述】:

在 Spring security 3 中 - 我可以声明性地定义一个默认权限,以分配给使用 jdbc 身份验证提供程序进行身份验证的所有经过身份验证的用户吗?目前我的 webapp 没有角色的概念(也不会 - 您要么是允许访问您的帐户的用户,要么不是),我不想在我的数据库中实现权限表,因为它是多余的。目前通过以下声明,我收到以下错误消息。

消息 [表“权限”不 存在];

<authentication-manager>
        <authentication-provider>
            <password-encoder hash="plaintext"/>
            <jdbc-user-service data-source-ref="dataSource"
                               users-by-username-query="select account_uid,hashedpassword,true from account where account_uid=?" />

        </authentication-provider>
    </authentication-manager>

如果这不能以声明方式完成 - 最好的方法是什么?

提前致谢,

伊恩。

【问题讨论】:

    标签: authorization spring-security


    【解决方案1】:

    &lt;jdbc-user-service&gt; 元素由JdbcUserServiceBeanDefinitionParser 类处理,该类又实例化和配置JdbcUserDetailsManager bean。此 bean 包含与托管用户、组和权限相关的 SQL。

    如果您查看JdbcUserDetailsManager 的源代码,您会发现它的灵活性非常有限。如果您不想要用户权限表,则需要您使用组权限表。一个或另一个。

    如果您想要更简单的东西,看起来您必须编写自己的 UserDetailsService 自定义实现。如果您这样做并在上下文中将其声明为普通 Spring bean,则可以使用以下方法将其连接到 Spring Security:

    <authentication-manager>
        <authentication-provider user-service-ref="myUserService">
            <password-encoder hash="plaintext"/>
        </authentication-provider>
    </authentication-manager>
    

    也许复制JdbcUserDetailsManager 的源代码并删除权限/组内容是最简单的方法。

    【讨论】:

      【解决方案2】:

      这可以通过简单的方式完成:

      authorities-by-username-query="select account_uid, 'default' from account where account_uid=?"

      此查询重复使用您的帐户表并为每个用户提供默认权限。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-04
        • 2011-09-02
        • 2011-11-20
        • 2014-02-18
        • 1970-01-01
        • 2019-03-30
        相关资源
        最近更新 更多