问题背景:
- 学习spring security时,正在做数据库的登录验证,碰到了一个稀奇的问题;明明数据库中有用户数据,但是jdbcTemplate无法将关键信息带出来,记录下思考过程。
先贴代码
package com.atguigu.security.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import com.atguigu.security.entity.Admin;
@Component
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private JdbcTemplate jdbcTemplate;
// 总目标:根据表单提奥的用户名查询User对象,并装配角色,权限等信息
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 1.从数据库查询Admin对象
String sql = "SELECT id,login_acct,user_pswd,user_name,email FROM t_admin WHERE login_acct=?";
List<Admin> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Admin.class), username);
Admin admin = list.get(0);
//System.out.println(jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Admin.class), username));
// 2.给Admin设置角色权限信息
List<GrantedAuthority> authorities = new Arra