SpringSecurity使用提供的UserDetailService实现连接数据库认证
配置MybatisPlus基础环境
- 配置pom文件
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>
<!--MybatisPlus映射框架-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
- 配置yml文件
server:
port: 8090
servlet:
encoding:
charset: UTF-8
spring:
application:
name: auth
datasource:
url: jdbc:mysql://localhost:3306/authdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&AllowPublicKeyRetrieval=True
username: root
password: spy167935842687
mybatis-plus:
mapper-locations: mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
type-aliases-package: com.spyu.auth.entity
- 用户表
CREATE TABLE `per_user` (
`user_id` bigint unsigned NOT NULL COMMENT '用户id',
`user_code` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '用户编码',
`login_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '登录名称',
`username` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '用户名称',
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '密码',
`phone` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '电话',
`salt` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '盐值',
`pic_url` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '头像路径',
`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '备注',
`user_create` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户创建时间',
`user_update` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户更新时间',
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
- 生成对应的Mapper文件
# PerUserInfoMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.spyu.auth.dao.PerUserInfoMapper">
</mapper>
# PerUserInfoMapper.java
/**
* 用户dao
* @author sunpeiyu
* @since 2023-06-19
*/
@Mapper
public interface PerUserInfoMapper extends BaseMapper<PerUserInfoEntity> {
}
编写实现了UserDetailService接口的实现类
/**
* spring security获取数据库返回的结果UserDetail
* @author sunpeiyu
* @since 2023-06-19
*/
@Component
public class LoginUserDetailImpl implements UserDetailsService {
@Autowired
private PerUserInfoMapper perUserInfoMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
String tip = "用户" + username + "不存在";
if (StringUtils.isEmpty(username)) {
throw new CommonException(tip);
}
LambdaQueryWrapper<PerUserInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(PerUserInfoEntity::getUsername, username);
PerUserInfoEntity userInfoEntity = perUserInfoMapper.selectOne(queryWrapper);
if (Objects.isNull(userInfoEntity)) {
throw new CommonException("用户不存在!");
}
PerUserInfoBO perUserInfoBO = new PerUserInfoBO();
BeanUtils.copyProperties(userInfoEntity, perUserInfoBO);
return new LoginUserBO(perUserInfoBO);
}
}
配置一个无加密的编码器
注意:如果不配置编码器,那么就会抛出异常,There is no PasswordEncoder mapped for id “null”
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 注册解码器
* @return
*/
@Bean
public PasswordEncoder passwordEncoder(){
// return new BCryptPasswordEncoder();
return NoOpPasswordEncoder.getInstance();
}
}
测试
数据库:
访问http://localhost:8090/test/boot
登录失败:
此处在登录成功后,就返回了业务测试接口的数据,如果失败返回错误提示,要求重新登录