用spring boot+html+mysql制作一个登录
时间: 2024-01-01 21:06:30 浏览: 141
好的,下面是一个简单的登录系统的步骤:
1. 创建一个 Spring Boot 工程,并添加所需的依赖。在 pom.xml 文件中添加以下依赖:
```
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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>
</dependency>
</dependencies>
```
2. 在 `application.properties` 文件中配置数据库连接信息:
```
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
```
3. 创建一个用户实体类,用于映射数据库表:
```java
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
// getter 和 setter 略
}
```
4. 创建一个 UserRepository 接口,继承自 JpaRepository 接口,用于操作数据库:
```java
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
```
5. 创建一个控制器类,用于处理登录相关的请求:
```java
@Controller
public class LoginController {
@Autowired
private UserRepository userRepository;
@GetMapping("/")
public String index() {
return "login";
}
@PostMapping("/login")
public String login(String username, String password, HttpSession session, Model model) {
User user = userRepository.findByUsername(username);
if (user != null && user.getPassword().equals(password)) {
session.setAttribute("user", user);
return "redirect:/home";
} else {
model.addAttribute("error", "用户名或密码错误");
return "login";
}
}
@GetMapping("/home")
public String home(HttpSession session, Model model) {
User user = (User) session.getAttribute("user");
if (user != null) {
model.addAttribute("username", user.getUsername());
return "home";
} else {
return "redirect:/";
}
}
@GetMapping("/logout")
public String logout(HttpSession session) {
session.removeAttribute("user");
return "redirect:/";
}
}
```
6. 创建登录页面和主页页面。在 `resources/templates` 目录下创建两个 HTML 文件:`login.html` 和 `home.html`。以 `login.html` 为例:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form method="post" action="/login">
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
</div>
<div>
<button type="submit">登录</button>
</div>
<div th:if="${error}">
<p th:text="${error}"></p>
</div>
</form>
</body>
</html>
```
7. 启动项目,在浏览器中访问 `http://localhost:8080/`,就可以看到登录页面了。输入正确的用户名和密码,点击登录按钮,就可以跳转到主页页面了。在主页页面中点击注销按钮,就可以退出登录。
以上就是一个简单的登录系统的制作过程,希望对你有所帮助。
阅读全文