feat:加上注释,增强可读性

backend
poppoppuppylove 2 months ago
parent 935defe59d
commit 0551efed08

@ -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();
}

@ -17,30 +17,35 @@ public class StudentController {
@Autowired
private StudentService studentService;
//根据id获取学生信息
@GetMapping("/{id}")
public ResponseEntity<Student> getStudentById(@PathVariable Long id) {
Student student = studentService.findById(id);
return ResponseEntity.ok(student);
}
//根据学生编号获取学生信息
@GetMapping("/studentNumber/{studentNumber}")
public ResponseEntity<Student> getStudentByStudentNumber(@PathVariable String studentNumber) {
Student student = studentService.findByStudentNumber(studentNumber);
return ResponseEntity.ok(student);
}
//获取所有学生信息
@GetMapping
public ResponseEntity<List<Student>> getAllStudents() {
List<Student> students = studentService.findAll();
return ResponseEntity.ok(students);
}
//添加学生信息
@PostMapping
public ResponseEntity<String> addStudent(@RequestBody Student student) {
studentService.save(student);
return ResponseEntity.ok("Student added successfully");
}
//更新学生信息
@PutMapping("/{studentNumber}")
public ResponseEntity<String> 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<String> deleteStudent(@PathVariable Long id) {
studentService.delete(id);

@ -12,9 +12,11 @@ public class TeacherController {
@Autowired
private TeacherService teacherService;
// 注册接口
@PostMapping("/register")
public ResponseEntity<String> 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<String> 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) {

@ -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;
}
}

@ -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;
}
}

@ -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());
}

Loading…
Cancel
Save