如何在不使用"authorities"的情况下使用Spring安全的JDBC认证?

How to use Spring Security's JDBC authentication without using "authorities"?

我对当局有意见。

Reason: PreparedStatementCallback; bad SQL grammar [select username,authority from authorities where username = ?]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "authorities" does not exist Position: 32

我不想实现权限,但不知道如何在 Spring 的 JavaConfig 中禁用它。

@Override
protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
    auth.jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery(
                    "select username,password,'true' as enabled from users where username=?")
            .passwordEncoder(new ShaPasswordEncoder());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();
}

据我所知,您无法在 Spring 安全性中使用 JavaConfig 停用权限机制,但您可以覆盖用于获取权限数据的查询以达到相同的效果。

当使用 Spring 安全性的 JDBC 身份验证时,它对检索身份验证数据的数据库方案做出某些假设。您已经通过调用 usersByUsernameQuery().

覆盖了示例中用于获取用户数据的查询

我设法有效地禁用了整个权限检查,方法是覆盖查询以获取像这样的权限数据(如 Craig Walls 在 "Spring in Action" 中所述):

@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder)
        throws Exception {

    authenticationManagerBuilder.jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery(
                    "SELECT username, password, enabled FROM users WHERE username=?")
            .authoritiesByUsernameQuery(
                    "SELECT username, 'ROLE_USER' FROM users WHERE username=?");
}