赞
踩
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.3.8.RELEASE</version>
</dependency>
1、初始用户密码
spring.security.user.name=user
spring.security.user.password=随机的
# Using generated security password: 8beaaa7b-2700-4fa6-b18b-c6f052229aec 当前随机密码
2、自定义用户和密码
spring.security.user.name=admin
spring.security.user.password=123456
3、查询数据库确定账户密码
SecurtyConfig 配置类
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapte{
@Autowired
private MyUserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
}
MyUserDetailsService 配置类
@Service @Slf4j class MyUserDetailsService implements UserDetailsService{ @Resource private UserMapper userMapper; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { log.info("输入账户为:" + username); /* UserDetails 的 子类 User public User( String username,// 用户名 String password,//密码 Collection<? extends GrantedAuthority> authorities //不晓得 可以获取到 ) */ //根据用户名称 查询密码 并返回 QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("username", username); User user = userMapper.selectOne(wrapper); if (user == null) { throw new UsernameNotFoundException("用户名不存在!"); } //角色暂定 List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("admin"); //进行 加密 String password = user.getPassword(); return new org.springframework.security.core.userdetails.User( username, password, authorities ); } }
运行错误 :
IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
没有PasswordEncoder
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
运行错误 :
BCryptPasswordEncoder : Encoded password does not look like BCrypt
密码没有使用 PasswordEncoder 进行加密:
//进行 加密
String password = passwordEncoder.encode(user.getPassword());
4、断点 观察如何认证
1、加密后 返回打断点
2、直接 step over 越过 ( 接下来 还要 step info 进入内部 )
3、一直跳 ,直到下一个类
4、在跳 ,直到 additionalAuthenticationChecks( , )
5、进行 step info
6、 这里停 passwordEncoder.matches( , )
7、step info 两密码 加密 进行 比对
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。