配置Mysql与注册登录模块 1
This commit is contained in:
parent
26ecd689c4
commit
78db9a28c8
|
@ -16,7 +16,59 @@
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<!-- dependencies 里面存放依赖-->
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- JDBC 依赖,用于操作连接数据库-->
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||||
|
<version>3.0.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 简化代码,可以帮助写一些构造函数, set() , get() 函数等-->
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.26</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- mysql 连接驱动-->
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<version>8.0.32</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- mybatis-plus 用于低代码开发-->
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
|
<version>3.5.3.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- mybatis-plus-generator 用于自动生成一些函数-->
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-generator</artifactId>
|
||||||
|
<version>3.5.3.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 用于安全验证-->
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
<version>3.0.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.kob.backend.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
//明文加密
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
|
public class SecurityConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
package com.kob.backend.controller.user;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.kob.backend.mapper.UserMapper;
|
||||||
|
import com.kob.backend.pojo.User;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//实现对用户类 User 的增删查操作
|
||||||
|
@RestController
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
// 用到数据库里的 Mapper 时,需要加 @Autowired 注解
|
||||||
|
@Autowired
|
||||||
|
UserMapper userMapper;
|
||||||
|
|
||||||
|
// 返回所有用户
|
||||||
|
@GetMapping("/user/all")
|
||||||
|
public List<User> getAll() {
|
||||||
|
// null 表示查询所有的
|
||||||
|
return userMapper.selectList(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
@GetMapping("/user/{userId}/")
|
||||||
|
public User getUser(@PathVariable int userId) {
|
||||||
|
// 查询某个用户
|
||||||
|
// return userMapper.selectById(userId);
|
||||||
|
|
||||||
|
// 封装查询语句:查询某个 id 的用户
|
||||||
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("id", userId);
|
||||||
|
return userMapper.selectOne(queryWrapper);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@GetMapping("/user/{userId}")
|
||||||
|
public List<User> getUser(@PathVariable int userId) {
|
||||||
|
// 封装查询语句:查询某个范围 id 的用户: ge 是大于等于, gt 是大于, le 是小于等于, lt 是小于
|
||||||
|
QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
|
||||||
|
queryWrapper1.ge("id", 2).le("id", 4);
|
||||||
|
return userMapper.selectList(queryWrapper1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/user/add/{userId}/{username}/{password}")
|
||||||
|
public String addUser(@PathVariable int userId,
|
||||||
|
@PathVariable String username,
|
||||||
|
@PathVariable String password) {
|
||||||
|
if(password.length() < 6){
|
||||||
|
return "密码少于6位,请重设密码";
|
||||||
|
}
|
||||||
|
// 加密明文密码并存入密文到数据库
|
||||||
|
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||||
|
String encodedPassword = passwordEncoder.encode(password);
|
||||||
|
User user = new User(userId, username, encodedPassword);
|
||||||
|
userMapper.insert(user);
|
||||||
|
return "Add User Successfully";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/user/delete/{userId}")
|
||||||
|
public String deleteUser(@PathVariable int userId){
|
||||||
|
userMapper.deleteById(userId);
|
||||||
|
return "Delete User Successfully";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
//用于将 class 的操作转化成 sql 语句
|
||||||
|
package com.kob.backend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.kob.backend.pojo.User;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface UserMapper extends BaseMapper<User> {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
//用于将 mysql 表 User 转换为 class
|
||||||
|
package com.kob.backend.pojo;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Data 用于编译时自动生成 getter setter 方法;
|
||||||
|
NoArgsConstructor 用于生成无参构造函数;
|
||||||
|
AllArgsConstructor 用于生成所有参数构造函数.
|
||||||
|
*/
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
// 使用对象类型定义而不是 Int 防止 Mybatis 报错
|
||||||
|
private Integer id;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.kob.backend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.kob.backend.mapper.UserMapper;
|
||||||
|
import com.kob.backend.pojo.User;
|
||||||
|
import com.kob.backend.service.impl.utils.UserDetailsImpl;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
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;
|
||||||
|
|
||||||
|
//自定义 Spring-Starter-Security 登录信息; @Autowired 需要在外层加入 @Service 注解
|
||||||
|
@Service
|
||||||
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("username",username);
|
||||||
|
User user = userMapper.selectOne(queryWrapper);
|
||||||
|
if(user == null){
|
||||||
|
throw new RuntimeException("用户不存在");
|
||||||
|
}
|
||||||
|
return new UserDetailsImpl(user);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
package com.kob.backend.service.impl.utils;
|
||||||
|
|
||||||
|
import com.kob.backend.pojo.User;
|
||||||
|
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;
|
||||||
|
|
||||||
|
//实现 UserDetails 接口:需要加上 lombok 的注解,来自动生成构造函数
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class UserDetailsImpl implements UserDetails {
|
||||||
|
|
||||||
|
// 定义一个 pojo 的 User 类对象
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取 pojo -> User 类对象 user 的用户密码
|
||||||
|
@Override
|
||||||
|
public String getPassword() {
|
||||||
|
return user.getPassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取 pojo -> User 类对象 user 的用户名
|
||||||
|
@Override
|
||||||
|
public String getUsername() {
|
||||||
|
return user.getUsername();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户账号是否没有过期
|
||||||
|
@Override
|
||||||
|
public boolean isAccountNonExpired() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户是否没有被锁定
|
||||||
|
@Override
|
||||||
|
public boolean isAccountNonLocked() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户授权是否没有过期
|
||||||
|
@Override
|
||||||
|
public boolean isCredentialsNonExpired() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户是否被启用
|
||||||
|
@Override
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,2 +1,8 @@
|
||||||
#??????
|
#default server port
|
||||||
server.port=3000
|
server.port=3000
|
||||||
|
|
||||||
|
#mysql database connect profile
|
||||||
|
spring.datasource.username=root
|
||||||
|
spring.datasource.password=mysqlPassword
|
||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/kob?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
|
||||||
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
|
|
@ -2,12 +2,17 @@ package com.kob.backend;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class BackendApplicationTests {
|
class BackendApplicationTests {
|
||||||
|
|
||||||
|
// 测试明文加密
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
|
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||||
|
System.out.println(passwordEncoder.encode("123"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue