在Spring Security中启用Group Authorities

我的一个项目需要使用Spring Security的Group Authorities,但是按照默认的配置方式

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"/>
        <password-encoder hash="bcrypt" />
    </authentication-provider>
</authentication-manager>

Group Authorities并没有生效,所以我看了看配置项

<jdbc-user-service>

对应的类 org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl 的源码:

在Spring Security中启用Group Authorities_第1张图片

其中有个“enableGroups”属性,默认值是“false”。好了,这样就明白了,Group Authorities默认是不生效的,看了看在

<jdbc-user-service>

这个配置项中并没enableGroups这样的属性,看来只能自己配置jdbc-user-service的bean了,于是我配置好了jdbc-user-service的bean,试了试,果然生效了。

<authentication-manager>
    <authentication-provider user-service-ref="jdbcUserDetailsService">
        <jdbc-user-service data-source-ref="dataSource"/>
        <password-encoder hash="bcrypt" />
    </authentication-provider>
</authentication-manager>
<beans:bean id="jdbcUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
    <beans:property name="enableGroups" value="true" />
    <beans:property name="jdbcTemplate" ref="jdbcTemplate" />
</beans:bean>


log:

2014/05/23 09:52:23:626 CST [DEBUG] DataSourceUtils - Returning JDBC Connection to DataSource
2014/05/23 09:52:23:627 CST [DEBUG] JdbcTemplate - Executing prepared SQL query
2014/05/23 09:52:23:627 CST [DEBUG] JdbcTemplate - Executing prepared SQL statement [select g.id, g.group_name, ga.authority from groups g, group_members gm, group_authorities ga where gm.username = ? and g.id = ga.group_id and g.id = gm.group_id]
2014/05/23 09:52:23:627 CST [DEBUG] DataSourceUtils - Fetching JDBC Connection from DataSource
2014/05/23 09:52:23:627 CST [TRACE] StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [admin], value class [java.lang.String], SQL type unknown
2014/05/23 09:52:23:628 CST [DEBUG] DataSourceUtils - Returning JDBC Connection to DataSource


你可能感兴趣的:(spring,spring-security)