不使用自带密码校验,创建 BCryptPasswordEncoder 注入容器,密码加密
1、创建loginUser 类,实行 UserDetails 接口
package com.exam.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import java.util.Collection; @Data @NoArgsConstructor @AllArgsConstructor public class LoginUser implements UserDetails { private SysUserinfo sysUserinfo; @Override public Collection<? extends GrantedAuthority> getAuthorities() { return null; } @Override public String getPassword() { return sysUserinfo.getPassword(); } @Override public String getUsername() { return sysUserinfo.getUsername(); } @Override public boolean isAccountNonExpired() { return UserDetails.super.isAccountNonExpired(); } @Override public boolean isAccountNonLocked() { return UserDetails.super.isAccountNonLocked(); } @Override public boolean isCredentialsNonExpired() { return UserDetails.super.isCredentialsNonExpired(); } @Override public boolean isEnabled() { return UserDetails.super.isEnabled(); } }
2、创建 UserDetailsServiceImpl 类,实行 UserDetailsService 接口
package com.exam.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.exam.entity.LoginUser; import com.exam.entity.SysUserinfo; import com.exam.mapper.SysUserinfoMapper; import org.springframework.beans.factory.annotation.Autowired; 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.Service; import java.util.Objects; /** * 查询用户信息 */ @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired SysUserinfoMapper sysUserinfoMapper; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 查询用户信息 LambdaQueryWrapper<SysUserinfo> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(SysUserinfo::getUsername, username); SysUserinfo sysUserinfo = sysUserinfoMapper.selectOne(queryWrapper); // 判断不为空 if (Objects.isNull(sysUserinfo)) { throw new RuntimeException("用户名或密码错误"); } //todo 查询用户对应权限信息 return new LoginUser(sysUserinfo); } }
3、创建 BCryptPasswordEncoder 注入容器,密码加密
package com.exam.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class SecurityConfig { // 创建 BCryptPasswordEncoder 注入容器,密码加密 @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
4、测试
@Autowired private PasswordEncoder passwordEncoder; @Test public void findname(){ // 将密码 123 进行加密 String admin = passwordEncoder.encode("123"); // 加密后的密码 System.out.println(admin); // 定义前台输入的密码 String password = "123"; // 将输入的密码,与加密后的记性比较 boolean bo = passwordEncoder.matches(password, admin); System.out.println(bo); }
标签:集成,return,springframework,SpringSecurity,---,org,import,UserDetails,public From: https://www.cnblogs.com/wangdch/p/18369873