【问题标题】:Spring Security: configure(AuthenticationManagerBuilder auth)Spring Security:配置(AuthenticationManagerBuilder auth)
【发布时间】:2017-08-13 04:29:35
【问题描述】:

我有一个带有属性的实体User:

  • id
  • username
  • password
  • firstname
  • lastname
  • userrole

属性userrole 是enum 的类型,而不是Set,所以1 个用户角色/用户。

现在我想做一个 JDBC 身份验证。

现在我有:

auth
    .jdbcAuthentication()
        .dataSource(dataSource)
        .usersByUsernameQuery("select username, password from user where username=?")
        .authoritiesByUsernameQuery("select username, userrole from user where username=?");

但这不起作用。

.usersByUsernameQuery("...")和.authoritiesByUsernameQuery("...");中的查询应该如何表述?

错误信息:

原因:org.h2.jdbc.JdbcSQLException:参数“columnIndex”不允许值“3” 参数“columnIndex”的值“3”无效 [90008-192] 在 org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.192.jar:1.4.192] 在 org.h2.message.DbException.get(DbException.java:179) ~[h2-1.4.192.jar:1.4.192] 在 org.h2.message.DbException.getInvalidValueException(DbException.java:228) ~[h2-1.4.192.jar:1.4.192] 在 org.h2.jdbc.JdbcResultSet.checkColumnIndex(JdbcResultSet.java:3172) ~[h2-1.4.192.jar:1.4.192] 在 org.h2.jdbc.JdbcResultSet.get(JdbcResultSet.java:3200) ~[h2-1.4.192.jar:1.4.192] 在 org.h2.jdbc.JdbcResultSet.getBoolean(JdbcResultSet.java:541) ~[h2-1.4.192.jar:1.4.192] 在 org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl$1.mapRow(JdbcDaoImpl.java:223) ~[spring-security-core-4.1.3.RELEASE.jar:4.1.3.RELEASE] 在 org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl$1.mapRow(JdbcDaoImpl.java:218) ~[spring-security-core-4.1.3.RELEASE.jar:4.1.3.RELEASE] 在 org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93) ~[spring-jdbc-4.3.3.RELEASE.jar:4.3.3.RELEASE] 在 org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60) ~[spring-jdbc-4.3.3.RELEASE.jar:4.3.3.RELEASE] 在 org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:697) ~[spring-jdbc-4.3.3.RELEASE.jar:4.3.3.RELEASE] 在 org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633) ~[spring-jdbc-4.3.3.RELEASE.jar:4.3.3.RELEASE] ...省略了65个常用框架

【问题讨论】:

    标签: spring spring-security h2


    【解决方案1】:

    查询用户需要3个参数,见Spring Security Reference:

    • users-by-username-query 用于查询用户名、密码和给定用户名的启用状态的 SQL 语句。默认是

    select username, password, enabled from users where username = ?
    

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2014-11-14
      • 2020-03-25
      • 2020-06-21
      • 2021-02-08
      • 2012-01-23
      • 2011-01-08
      • 1970-01-01
      相关资源
      最近更新 更多