【问题标题】:How to use Spring Security's JDBC authentication without using "authorities"?如何在不使用“权限”的情况下使用 Spring Security 的 JDBC 身份验证?
【发布时间】:2015-04-07 19:15:54
【问题描述】:

我对当局有意见。

原因:PreparedStatementCallback;糟糕的 SQL 语法 [选择 用户名,权威机构,其中用户名 = ?];嵌套的 例外是 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();
}

【问题讨论】:

    标签: spring postgresql spring-security


    【解决方案1】:

    据我所知,您无法在 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=?");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-15
      • 2011-11-14
      • 2016-08-12
      • 2020-03-15
      • 2015-05-22
      • 2011-06-04
      • 2016-09-09
      相关资源
      最近更新 更多