如何在不使用“权限”的情况下使用Spring Security的JDBC身份验证?

Mar*_*rko 4 postgresql spring spring-security

我对当局有意见。

原因:PreparedStatementCallback;错误的 SQL 语法 [select username,authority fromauthorities where username = ?]; 嵌套异常是 org.postgresql.util.PSQLException:错误:关系“权限”不存在 位置: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();
}
Run Code Online (Sandbox Code Playgroud)

ano*_*ode 5

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

当使用 Spring Security 的 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=?");
}
Run Code Online (Sandbox Code Playgroud)