mysql和SpringBoot结合

master
barney 2 years ago
parent 997e5e6532
commit 8018aa025c
  1. BIN
      backend/lib/javax.annotation.jar
  2. BIN
      backend/lib/javax.ejb.jar
  3. BIN
      backend/lib/javax.jms.jar
  4. BIN
      backend/lib/javax.persistence.jar
  5. BIN
      backend/lib/javax.resource.jar
  6. BIN
      backend/lib/javax.servlet.jar
  7. BIN
      backend/lib/javax.servlet.jsp.jar
  8. BIN
      backend/lib/javax.servlet.jsp.jstl.jar
  9. BIN
      backend/lib/javax.transaction.jar
  10. 39
      backend/pom.xml
  11. 20
      backend/src/main/java/com/kob/backend/config/SecurityConfig.java
  12. 20
      backend/src/main/java/com/kob/backend/controller/pojo/User.java
  13. 35
      backend/src/main/java/com/kob/backend/controller/service/impl/UserDetailsServiceImpl.java
  14. 56
      backend/src/main/java/com/kob/backend/controller/service/impl/utils/UserDetailsImpl.java
  15. 73
      backend/src/main/java/com/kob/backend/controller/user/UserController.java
  16. 13
      backend/src/main/java/com/kob/backend/mapper/UserMapper.java
  17. 4
      backend/src/main/resources/application.properties
  18. 19
      backend/src/test/java/com/kob/backend/BackendApplicationTests.java

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -17,6 +17,45 @@
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>

@ -0,0 +1,20 @@
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;
/**
* @author new
*/
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

@ -0,0 +1,20 @@
package com.kob.backend.controller.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author new
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
// 相当于User表中的一行数据
private Integer id;
private String username;
private String password;
}

@ -0,0 +1,35 @@
package com.kob.backend.controller.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.controller.pojo.User;
import com.kob.backend.controller.service.impl.utils.UserDetailsImpl;
import com.kob.backend.mapper.UserMapper;
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;
/**
* @author new
*/
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserMapper userMapper;
// 传入username,返回对应的username和password
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
QueryWrapper<User> qw = new QueryWrapper<>();
qw.eq("username",username);
User user = userMapper.selectOne(qw);
if (user == null) {
throw new RuntimeException("用户不存在!");
}
return new UserDetailsImpl(user);
}
}

@ -0,0 +1,56 @@
package com.kob.backend.controller.service.impl.utils;
import com.kob.backend.controller.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 org.springframework.security.core.userdetails.UserDetailsService;
import java.util.Collection;
/**
* @author new
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDetailsImpl implements UserDetails {
private User user;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
return user.getPassword();
}
@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;
}
}

@ -0,0 +1,73 @@
package com.kob.backend.controller.user;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.controller.pojo.User;
import com.kob.backend.mapper.UserMapper;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author new
*/
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
// 返回user表的所有数据
@GetMapping("/user/all/")
public List<User> getAll() {
return userMapper.selectList(null);
}
// 返回指定id的用户信息
@GetMapping("/user/{userId}/")
public User getUser(@PathVariable int userId) {
QueryWrapper<User> qw = new QueryWrapper<>();
// 返回指定id的用户信息
// return userMapper.selectById(userId);
// 等价于上面的查询语句
qw.eq("id",userId);
// 返回编号大于等于2小于等于3的所有用户信息
// qw.ge("id",2).le("id",3);
return userMapper.selectOne(qw);
}
// 添加用户
@GetMapping("/user/add/{userid}/{username}/{password}/")
public String addUser(@PathVariable int userid,
@PathVariable String username,
@PathVariable String password) {
// 存入数据库直接存入的是密文,不存明文密码
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,13 @@
package com.kob.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kob.backend.controller.pojo.User;
import org.apache.ibatis.annotations.Mapper;
/**
* @author new
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

@ -1 +1,5 @@
server.port=3000
spring.datasource.username=root
spring.datasource.password=zfp251217
spring.datasource.url=jdbc:mysql://101.33.213.197:3307/kob?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

@ -2,12 +2,31 @@ 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();
// // 编码后的值不同
// String str1 = passwordEncoder.encode("zfp");
// String str2 = passwordEncoder.encode("zfp");
// System.out.println(str1);
// System.out.println(str2);
// // 但是匹配的结果正确
// System.out.println(passwordEncoder.matches("zfp",str1));
// System.out.println(passwordEncoder.matches("zfp",str2));
// // 匹配失败
// System.out.println(passwordEncoder.matches("zfp1",str2));
// 将数据库中的明文密码编码为密文
// user1,user2,user3
System.out.println(passwordEncoder.encode("123456"));
System.out.println(passwordEncoder.encode("test"));
System.out.println(passwordEncoder.encode("root"));
}
}

Loading…
Cancel
Save