From 0551efed084284c9babb756affd2f0e3ebb4c7ad Mon Sep 17 00:00:00 2001 From: poppoppuppylove <431792974@qq.com> Date: Sun, 6 Oct 2024 12:44:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=8A=A0=E4=B8=8A=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=BC=BA=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../attendance/config/SecurityConfig.java | 6 +-- .../controller/StudentController.java | 6 +++ .../controller/TeacherController.java | 4 ++ .../attendance/entity/PointsRequest.java | 14 +++---- .../example/attendance/entity/Student.java | 38 +++---------------- .../com/example/attendance/util/JWTUtil.java | 14 ++++--- 6 files changed, 34 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/example/attendance/config/SecurityConfig.java b/src/main/java/com/example/attendance/config/SecurityConfig.java index a4a6c48..b2132fd 100644 --- a/src/main/java/com/example/attendance/config/SecurityConfig.java +++ b/src/main/java/com/example/attendance/config/SecurityConfig.java @@ -18,13 +18,13 @@ public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http - .csrf(csrf -> csrf.disable()) // 关闭 CSRF 保护(适用于测试,生产环境中请谨慎处理) + .csrf(csrf -> csrf.disable()) // 关闭 CSRF 保护 .authorizeHttpRequests(authz -> authz .requestMatchers("/api/teacher/register", "/api/teacher/login").permitAll() // 允许注册和登录接口匿名访问 .anyRequest().authenticated() // 其他请求需要认证 ) - .formLogin(form -> form.disable()) // 关闭表单登录(如有需要可以调整) - .httpBasic(httpBasic -> httpBasic.disable()); // 关闭基本认证(如有需要可以调整) + .formLogin(form -> form.disable()) // 关闭表单登录 + .httpBasic(httpBasic -> httpBasic.disable()); // 关闭基本认证 return http.build(); } diff --git a/src/main/java/com/example/attendance/controller/StudentController.java b/src/main/java/com/example/attendance/controller/StudentController.java index 715a706..4d3df24 100644 --- a/src/main/java/com/example/attendance/controller/StudentController.java +++ b/src/main/java/com/example/attendance/controller/StudentController.java @@ -17,30 +17,35 @@ public class StudentController { @Autowired private StudentService studentService; + //根据id获取学生信息 @GetMapping("/{id}") public ResponseEntity getStudentById(@PathVariable Long id) { Student student = studentService.findById(id); return ResponseEntity.ok(student); } + //根据学生编号获取学生信息 @GetMapping("/studentNumber/{studentNumber}") public ResponseEntity getStudentByStudentNumber(@PathVariable String studentNumber) { Student student = studentService.findByStudentNumber(studentNumber); return ResponseEntity.ok(student); } + //获取所有学生信息 @GetMapping public ResponseEntity> getAllStudents() { List students = studentService.findAll(); return ResponseEntity.ok(students); } + //添加学生信息 @PostMapping public ResponseEntity addStudent(@RequestBody Student student) { studentService.save(student); return ResponseEntity.ok("Student added successfully"); } + //更新学生信息 @PutMapping("/{studentNumber}") public ResponseEntity updateStudent(@PathVariable String studentNumber, @RequestBody Student student) { student.setStudentNumber(studentNumber); @@ -48,6 +53,7 @@ public class StudentController { return ResponseEntity.ok("Student updated successfully"); } + //删除学生信息 @DeleteMapping("/{id}") public ResponseEntity deleteStudent(@PathVariable Long id) { studentService.delete(id); diff --git a/src/main/java/com/example/attendance/controller/TeacherController.java b/src/main/java/com/example/attendance/controller/TeacherController.java index 8e1fe04..5e63b32 100644 --- a/src/main/java/com/example/attendance/controller/TeacherController.java +++ b/src/main/java/com/example/attendance/controller/TeacherController.java @@ -12,9 +12,11 @@ public class TeacherController { @Autowired private TeacherService teacherService; + // 注册接口 @PostMapping("/register") public ResponseEntity register(@RequestParam String username, @RequestParam String password) { try { + // 调用 teacherService 的 register 方法注册用户 teacherService.register(username, password); return ResponseEntity.ok("注册成功"); // 成功时返回 200 状态和消息 } catch (Exception e) { @@ -22,9 +24,11 @@ public class TeacherController { } } + // 登录接口 @PostMapping("/login") public ResponseEntity login(@RequestParam String username, @RequestParam String password) { try { + // 调用 teacherService 的 login 方法登录 String token = teacherService.login(username, password); return ResponseEntity.ok("登录成功, Token: " + token); // 成功时返回 200 状态和 JWT token } catch (Exception e) { diff --git a/src/main/java/com/example/attendance/entity/PointsRequest.java b/src/main/java/com/example/attendance/entity/PointsRequest.java index 078bb99..dc680cb 100644 --- a/src/main/java/com/example/attendance/entity/PointsRequest.java +++ b/src/main/java/com/example/attendance/entity/PointsRequest.java @@ -1,15 +1,13 @@ package com.example.attendance.entity; import java.math.BigDecimal; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +@NoArgsConstructor +@AllArgsConstructor +@Data public class PointsRequest { private BigDecimal pointsDelta; - - public BigDecimal getPointsDelta() { - return pointsDelta; - } - - public void setPointsDelta(BigDecimal pointsDelta) { - this.pointsDelta = pointsDelta; - } } diff --git a/src/main/java/com/example/attendance/entity/Student.java b/src/main/java/com/example/attendance/entity/Student.java index e70e23b..38469b0 100644 --- a/src/main/java/com/example/attendance/entity/Student.java +++ b/src/main/java/com/example/attendance/entity/Student.java @@ -1,43 +1,17 @@ package com.example.attendance.entity; import java.math.BigDecimal; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +@NoArgsConstructor +@AllArgsConstructor +@Data public class Student { private Long id; private String studentNumber; // 学号 private String name; // 姓名 private BigDecimal points; // 积分 - // getters and setters - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getStudentNumber() { - return studentNumber; - } - - public void setStudentNumber(String studentNumber) { - this.studentNumber = studentNumber; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public BigDecimal getPoints() { - return points; - } - - public void setPoints(BigDecimal points) { - this.points = points; - } } diff --git a/src/main/java/com/example/attendance/util/JWTUtil.java b/src/main/java/com/example/attendance/util/JWTUtil.java index b5ab66c..0b3dc7a 100644 --- a/src/main/java/com/example/attendance/util/JWTUtil.java +++ b/src/main/java/com/example/attendance/util/JWTUtil.java @@ -13,30 +13,34 @@ public class JWTUtil { private static final SecretKey SECRET_KEY = Keys.secretKeyFor(SignatureAlgorithm.HS256); + // 生成 Token public static String generateToken(Teacher teacher) { return Jwts.builder() - .setSubject(teacher.getUsername()) - .setIssuedAt(new Date()) + .setSubject(teacher.getUsername()) // 设置 Token 主题(用户名) + .setIssuedAt(new Date()) // 设置 Token 签发时间 .setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 1000)) // Token 有效期 1 小时 - .signWith(SignatureAlgorithm.HS256, SECRET_KEY) + .signWith(SignatureAlgorithm.HS256, SECRET_KEY) // 使用 HS256 签名算法和 SECRET_KEY 签名 .compact(); } + // 从 Token 中提取声明 public static Claims extractClaims(String token) { try { return Jwts.parser() - .setSigningKey(SECRET_KEY) - .parseClaimsJws(token) + .setSigningKey(SECRET_KEY) // 设置签名密钥 + .parseClaimsJws(token) // 解析 Token .getBody(); } catch (Exception e) { throw new RuntimeException("Invalid JWT token", e); // 捕获并抛出异常 } } + // 从 Token 中获取用户名 public static String getUsernameFromToken(String token) { return extractClaims(token).getSubject(); } + // 判断 Token 是否过期 public static boolean isTokenExpired(String token) { return extractClaims(token).getExpiration().before(new Date()); }