1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sunxu</groupId>
<artifactId>mysqlconnector</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mysqlconnector</name>
<description>mysqlconnector</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2 application.yml
spring:
jpa:
database: mysql
show-sql: true
generate-ddl: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: update
########-spring datasource-########
datasource:
#账号配置
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: 123456
#hikari数据库连接池
hikari:
pool-name: Retail _HikariCP
minimum-idle: 5 #最小空闲连接数量
idle-timeout: 180000 #空闲连接存活最大时间,默认600000(10分钟)
maximum-pool-size: 10 #连接池最大连接数,默认是10
auto-commit: true #此属性控制从池返回的连接的默认自动提交行为,默认值:true
max-lifetime: 1800000 #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
connection-timeout: 30000 #数据库连接超时时间,默认30秒,即30000
connection-test-query: SELECT 1
3 LoginDao.java
package com.sunxu.mysqlconnector.dao;
import com.sunxu.mysqlconnector.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface LoginDao extends JpaRepository<User, Integer> {
User findByUsernameAndPassword(String name, String password);
User saveAndFlush(User user);
}
4 User.java
package com.sunxu.mysqlconnector.model;
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(){};
}
5 loginService.java
package com.sunxu.mysqlconnector.service;
import com.sunxu.mysqlconnector.dao.LoginDao;
import com.sunxu.mysqlconnector.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class loginService {
@Autowired
private LoginDao loginDao;//与dao层进行交互
public String chick(String username,String userpassword) {
User b = loginDao.findByUsernameAndPassword(username, userpassword);
if (b == null) {
return "null";
}
String aString = b.getUsername();
String bString = b.getPassword();
if (aString.equals(username) && bString.equals(userpassword)) {
return "success";
} else {
return "error";
}
}
public void save(User user) {
log.info("保存到数据库开始");
loginDao.saveAndFlush(user);
}
public String sha256(String secret,String data) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
secret="2131231@#42";
data="01TP\n544389574398";
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes("utf-8"), "HmacSHA256");
sha256_HMAC.init(secretKey);
byte[] hash = sha256_HMAC.doFinal(data.getBytes("utf-8"));
String encodeStr = Base64.encodeBase64String(hash);
String encodeStr16=byte2Hex(hash);
return encodeStr16;
}
public static String byte2Hex(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
//1得到一位的进行补0操作
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
}
6 LoginController.java
package com.sunxu.mysqlconnector.controller;
import com.sunxu.mysqlconnector.service.loginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.PostConstruct;
@Slf4j
@Component
public class LoginController {
@Autowired //与service层进行交互
private loginService loginservice;
@PostConstruct
public String login() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
String username = "sunxu1";
String password = "fngje";
log.info("password: {}",loginservice.sha256("",""));
String b;
b=loginservice.chick(username,password);
log.info("b:{}",b);
if(b == "success") {
log.info("查询成功");
return "login success";
} else {
if(b == "save") {
log.info("保存到数据库");
return "login save";
}
log.info("用户名或者密码错误");
return "login fail";
}
}
}
7、RegisterController.java
package com.sunxu.mysqlconnector.controller;
import com.sunxu.mysqlconnector.model.User;
import com.sunxu.mysqlconnector.service.loginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
public class RegisterController {
@Autowired
private loginService loginservice;
@GetMapping ("/second")
public User second(){
User u = new User("coolsunxu", "5456547");
return u;
}
@PostMapping("/register")
public User register(@RequestBody User user) {
log.info("{}请求注册",user.getUsername());
if(loginservice.chick(user.getUsername(), user.getPassword())=="success") {
log.info("用户已在数据库中,无需保存");
return user;
}
loginservice.save(user);
log.info("保存成功");
return user;
}
}
版权声明:本文为coolsunxu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。