帮我用springboot写个签到系统,要包括学生数据库、登陆界面
时间: 2023-12-06 07:41:33 浏览: 164
好的,以下是一个简单的签到系统的基本实现,包括学生数据库和登陆界面:
1. 创建一个Spring Boot项目并添加以下依赖:
```xml
<dependencies>
<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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
```
2. 创建学生实体类
```java
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String email;
private String password;
// getter & setter
}
```
3. 创建学生数据库访问接口
```java
public interface StudentRepository extends JpaRepository<Student, Long> {
Student findByEmail(String email);
}
```
4. 创建登陆表单
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form th:action="@{/login}" method="post">
<p><label>Email: <input type="email" name="email"/></label></p>
<p><label>Password: <input type="password" name="password"/></label></p>
<button type="submit">Login</button>
</form>
</body>
</html>
```
5. 创建登陆控制器
```java
@Controller
public class LoginController {
@GetMapping("/login")
public String showLoginForm() {
return "login";
}
@PostMapping("/login")
public String login(@RequestParam String email, @RequestParam String password, HttpSession session) {
Student student = studentRepository.findByEmail(email);
if (student != null && student.getPassword().equals(password)) {
session.setAttribute("student", student);
return "redirect:/";
}
return "login";
}
@GetMapping("/logout")
public String logout(HttpSession session) {
session.removeAttribute("student");
return "redirect:/login";
}
@Autowired
private StudentRepository studentRepository;
}
```
6. 添加安全配置
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, StudentRepository studentRepository) throws Exception {
auth.userDetailsService(email -> {
Student student = studentRepository.findByEmail(email);
if (student != null) {
return new User(student.getEmail(), student.getPassword(), Collections.emptyList());
}
throw new UsernameNotFoundException("Invalid email/password.");
});
}
}
```
7. 创建签到表单
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Attendance</title>
</head>
<body>
<h1>Attendance</h1>
<p>Welcome, <span th:text="${session.student.name}"/>!</p>
<form th:action="@{/attendance}" method="post">
<button type="submit">Sign in</button>
</form>
<a href="/logout">Logout</a>
</body>
</html>
```
8. 创建签到控制器
```java
@Controller
public class AttendanceController {
@GetMapping("/")
public String index() {
return "attendance";
}
@PostMapping("/attendance")
public String attendance(HttpSession session) {
Student student = (Student) session.getAttribute("student");
if (student != null) {
// 执行签到操作
return "redirect:/";
}
return "redirect:/login";
}
}
```
这是一个简单的签到系统的基本实现,你可以根据具体需求进行修改和优化。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)