diff --git a/backend/pom.xml b/backend/pom.xml index f96adaa..e7d4e58 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -16,7 +16,59 @@ 1.8 + + + + + + org.springframework.boot + spring-boot-starter-jdbc + 3.0.2 + + + + + + org.projectlombok + lombok + 1.18.26 + provided + + + + + + com.mysql + mysql-connector-j + 8.0.32 + + + + + + com.baomidou + mybatis-plus-boot-starter + 3.5.3.1 + + + + + + com.baomidou + mybatis-plus-generator + 3.5.3.1 + + + + + + org.springframework.boot + spring-boot-starter-security + 3.0.2 + + + org.springframework.boot spring-boot-starter-thymeleaf diff --git a/backend/src/main/java/com/kob/backend/config/SecurityConfig.java b/backend/src/main/java/com/kob/backend/config/SecurityConfig.java new file mode 100644 index 0000000..935eed9 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/config/SecurityConfig.java @@ -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(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/kob/backend/controller/user/UserController.java b/backend/src/main/java/com/kob/backend/controller/user/UserController.java new file mode 100644 index 0000000..78610a1 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/controller/user/UserController.java @@ -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 getAll() { +// null 表示查询所有的 + return userMapper.selectList(null); + } + +/* + @GetMapping("/user/{userId}/") + public User getUser(@PathVariable int userId) { +// 查询某个用户 +// return userMapper.selectById(userId); + +// 封装查询语句:查询某个 id 的用户 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("id", userId); + return userMapper.selectOne(queryWrapper); + } +*/ + + @GetMapping("/user/{userId}") + public List getUser(@PathVariable int userId) { +// 封装查询语句:查询某个范围 id 的用户: ge 是大于等于, gt 是大于, le 是小于等于, lt 是小于 + QueryWrapper 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"; + } +} diff --git a/backend/src/main/java/com/kob/backend/mapper/UserMapper.java b/backend/src/main/java/com/kob/backend/mapper/UserMapper.java new file mode 100644 index 0000000..71646a8 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/mapper/UserMapper.java @@ -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 { + +} diff --git a/backend/src/main/java/com/kob/backend/pojo/User.java b/backend/src/main/java/com/kob/backend/pojo/User.java new file mode 100644 index 0000000..5db506e --- /dev/null +++ b/backend/src/main/java/com/kob/backend/pojo/User.java @@ -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; + +} diff --git a/backend/src/main/java/com/kob/backend/service/impl/UserDetailsServiceImpl.java b/backend/src/main/java/com/kob/backend/service/impl/UserDetailsServiceImpl.java new file mode 100644 index 0000000..8bd7d9f --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/impl/UserDetailsServiceImpl.java @@ -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 queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("username",username); + User user = userMapper.selectOne(queryWrapper); + if(user == null){ + throw new RuntimeException("用户不存在"); + } + return new UserDetailsImpl(user); + } +} diff --git a/backend/src/main/java/com/kob/backend/service/impl/utils/UserDetailsImpl.java b/backend/src/main/java/com/kob/backend/service/impl/utils/UserDetailsImpl.java new file mode 100644 index 0000000..aa04c18 --- /dev/null +++ b/backend/src/main/java/com/kob/backend/service/impl/utils/UserDetailsImpl.java @@ -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 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; + } +} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index e25ec12..2a217c6 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,2 +1,8 @@ -#?????? -server.port=3000 \ No newline at end of file +#default server port +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 diff --git a/backend/src/test/java/com/kob/backend/BackendApplicationTests.java b/backend/src/test/java/com/kob/backend/BackendApplicationTests.java index b19336e..b7a66fd 100644 --- a/backend/src/test/java/com/kob/backend/BackendApplicationTests.java +++ b/backend/src/test/java/com/kob/backend/BackendApplicationTests.java @@ -2,12 +2,17 @@ package com.kob.backend; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; @SpringBootTest class BackendApplicationTests { +// 测试明文加密 @Test void contextLoads() { + PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + System.out.println(passwordEncoder.encode("123")); } }