且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Spring Security和自定义数据库的Spring Boot

更新时间:2022-05-27 09:17:33

请更改SQL以返回列名按顺序而不是 * ;

Please change the SQL to return the column names in order instead of *;

<!-- change to your own column name, return the columns in order-->
select <username,password,enabled> from use_user where use_user.use_username=?

AUTHORITIES SQL

<!-- change to your own column name, the authority must be second column  -->
select <username,authority> from usr_user_role inner join use_user on use_user.use_id = usr_user_role.usr_use_id where use_user.use_username=?

更多详细信息,请参见此处,确定,这是代码,我认为它更容易

More detail see here, OK, here is the code, I think it's more easy to understand why you should return the columns in order.

protected List<UserDetails> loadUsersByUsername(String username) {
        return getJdbcTemplate().query(this.usersByUsernameQuery,
                new String[] { username }, new RowMapper<UserDetails>() {
                    @Override
                    public UserDetails mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        // get user info
                        String username = rs.getString(1);
                        String password = rs.getString(2);
                        boolean enabled = rs.getBoolean(3);
                        return new User(username, password, enabled, true, true, true,
                                AuthorityUtils.NO_AUTHORITIES);
                    }

                });
    }

protected List<GrantedAuthority> loadUserAuthorities(String username) {
        return getJdbcTemplate().query(this.authoritiesByUsernameQuery,
                new String[] { username }, new RowMapper<GrantedAuthority>() {
                    @Override
                    public GrantedAuthority mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        //get GrantedAuthority
                        String roleName = JdbcDaoImpl.this.rolePrefix + rs.getString(2);

                        return new SimpleGrantedAuthority(roleName);
                    }
                });
    }