parent
ea43ca93ad
commit
ddf078bae5
@ -0,0 +1,146 @@
|
||||
<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>3.3.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>attendance</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>attendance</name>
|
||||
<description>attendance</description>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot Starter JDBC -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Starter Web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis Starter -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot DevTools -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- MySQL Connector -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Starter Test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis Starter Test -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter-test</artifactId>
|
||||
<version>3.0.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- EasyExcel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>3.0.5</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Security for password encryption -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- JWT Library -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-api</artifactId>
|
||||
<version>0.11.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-impl</artifactId>
|
||||
<version>0.11.5</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-jackson</artifactId>
|
||||
<version>0.11.5</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- BCrypt for password hashing -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-crypto</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- Compiler Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<release>17</release> <!-- 设置 release 参数来简化配置 -->
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Spring Boot Maven Plugin -->
|
||||
<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>
|
@ -0,0 +1,33 @@
|
||||
package com.example.attendance.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig {
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.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()); // 关闭基本认证(如有需要可以调整)
|
||||
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.example.attendance.controller;
|
||||
|
||||
import com.example.attendance.entity.RollCallResponse;
|
||||
import com.example.attendance.entity.RollCallSettings;
|
||||
import com.example.attendance.entity.Student;
|
||||
import com.example.attendance.service.RollCallService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/rollcall")
|
||||
public class RollCallController {
|
||||
|
||||
private final RollCallService rollCallService;
|
||||
|
||||
public RollCallController(RollCallService rollCallService) {
|
||||
this.rollCallService = rollCallService;
|
||||
}
|
||||
|
||||
@PostMapping("/start")
|
||||
public RollCallResponse startRollCall(@RequestBody List<Student> students, @RequestBody RollCallSettings settings) {
|
||||
return rollCallService.startRollCall(students, settings);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.example.attendance.controller;
|
||||
|
||||
import com.example.attendance.service.TeacherService;
|
||||
import com.example.attendance.service.impl.TeacherServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@RestController
|
||||
@RequestMapping("/api/teacher")
|
||||
public class TeacherController {
|
||||
|
||||
@Autowired
|
||||
private TeacherService teacherService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<String> register(@RequestParam String username, @RequestParam String password) {
|
||||
try {
|
||||
teacherService.register(username, password);
|
||||
return ResponseEntity.ok("注册成功"); // 成功时返回 200 状态和消息
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.badRequest().body("注册失败: " + e.getMessage()); // 失败时返回 400 状态和错误信息
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<String> login(@RequestParam String username, @RequestParam String password) {
|
||||
try {
|
||||
String token = teacherService.login(username, password);
|
||||
return ResponseEntity.ok("登录成功, Token: " + token); // 成功时返回 200 状态和 JWT token
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.badRequest().body("登录失败: " + e.getMessage()); // 失败时返回 400 状态和错误信息
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.example.attendance.entity;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class PointsRequest {
|
||||
private BigDecimal pointsDelta;
|
||||
|
||||
public BigDecimal getPointsDelta() {
|
||||
return pointsDelta;
|
||||
}
|
||||
|
||||
public void setPointsDelta(BigDecimal pointsDelta) {
|
||||
this.pointsDelta = pointsDelta;
|
||||
}
|
||||
}
|
@ -1,13 +1,18 @@
|
||||
package com.example.attendance.dto;
|
||||
package com.example.attendance.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@NoArgsConstructor
|
||||
public class StudentExcelDTO {
|
||||
private String name;
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class RollCallResponse {
|
||||
private String studentId;
|
||||
private String name;
|
||||
private BigDecimal points;
|
||||
private String message; // 显示结果信息
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.example.attendance.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class RollCallSettings {
|
||||
private boolean isRollCall; // 点名(true)或提问(false)
|
||||
private boolean triggerRandomEvent; // 是否触发随机事件
|
||||
private boolean wheelOfFortune; // 是否开启命运轮盘
|
||||
|
||||
}
|
@ -1,14 +1,15 @@
|
||||
package com.example.attendance.dto;
|
||||
package com.example.attendance.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class StudentDTO {
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class Teacher {
|
||||
private Long id;
|
||||
private String name;
|
||||
private int currentPoints;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.example.attendance.listener;
|
||||
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
import com.example.attendance.dto.StudentExcelDTO;
|
||||
import com.example.attendance.entity.Student;
|
||||
import com.example.attendance.mapper.StudentMapper;
|
||||
|
||||
public class StudentExcelListener extends AnalysisEventListener<StudentExcelDTO> {
|
||||
private final StudentMapper studentMapper;
|
||||
|
||||
public StudentExcelListener(StudentMapper studentMapper) {
|
||||
this.studentMapper = studentMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(StudentExcelDTO data, AnalysisContext context) {
|
||||
Student student = new Student();
|
||||
student.setName(data.getName());
|
||||
student.setId(Long.valueOf(data.getStudentId()));
|
||||
student.setCurrentPoints(0); // 初始积分为0
|
||||
studentMapper.insertStudent(student);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext context) {
|
||||
// 处理完成后的操作
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.example.attendance.mapper;
|
||||
|
||||
import com.example.attendance.entity.Teacher;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
@Mapper
|
||||
public interface TeacherMapper {
|
||||
|
||||
@Insert("INSERT INTO teacher (username, password) VALUES (#{username}, #{password})")
|
||||
void save(Teacher teacher);
|
||||
|
||||
@Select("SELECT * FROM teacher WHERE username = #{username}")
|
||||
Teacher findByUsername(String username);
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.attendance.service;
|
||||
import com.example.attendance.entity.RollCallResponse;
|
||||
import com.example.attendance.entity.RollCallSettings;
|
||||
import com.example.attendance.entity.Student;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface RollCallService {
|
||||
RollCallResponse startRollCall(List<Student> students, RollCallSettings settings); // 处理点名逻辑
|
||||
}
|
@ -1,15 +1,20 @@
|
||||
package com.example.attendance.service;
|
||||
|
||||
import com.example.attendance.dto.StudentExcelDTO;
|
||||
import com.example.attendance.entity.Student;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
public interface StudentService {
|
||||
void importStudents(MultipartFile file);
|
||||
List<Student> getAllStudents();
|
||||
Student getStudentById(Long id);
|
||||
void addStudent(Student student);
|
||||
void updateStudentPoints(Long id, int points);
|
||||
void importStudents(MultipartFile file) throws Exception ;
|
||||
void adjustPoints(String studentNumber, BigDecimal pointsDelta);
|
||||
List<Student> getStudentRanking(int page, int size);
|
||||
Student findById(Long id);
|
||||
Student findByStudentNumber(String studentNumber);
|
||||
List<Student> findAll();
|
||||
void save(Student student);
|
||||
void update(Student student);
|
||||
void delete(Long id);
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.example.attendance.service;
|
||||
|
||||
public interface TeacherService {
|
||||
|
||||
/**
|
||||
* 注册新教师
|
||||
*
|
||||
* @param username 教师的用户名
|
||||
* @param password 教师的密码
|
||||
* @throws Exception 注册过程中可能抛出的异常
|
||||
*/
|
||||
void register(String username, String password) throws Exception;
|
||||
|
||||
/**
|
||||
* 教师登录
|
||||
*
|
||||
* @param username 教师的用户名
|
||||
* @param password 教师的密码
|
||||
* @return 返回 JWT token
|
||||
* @throws Exception 登录失败时抛出的异常
|
||||
*/
|
||||
String login(String username, String password) throws Exception;
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.example.attendance.service.impl;
|
||||
|
||||
import com.example.attendance.mapper.TeacherMapper;
|
||||
import com.example.attendance.entity.Teacher;
|
||||
import com.example.attendance.service.TeacherService;
|
||||
import com.example.attendance.util.JWTUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TeacherServiceImpl implements TeacherService {
|
||||
|
||||
@Autowired
|
||||
private TeacherMapper teacherMapper;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder; // 使用 PasswordEncoder 接口
|
||||
|
||||
@Override
|
||||
public void register(String username, String password) {
|
||||
Teacher teacher = new Teacher();
|
||||
teacher.setUsername(username);
|
||||
teacher.setPassword(passwordEncoder.encode(password)); // 密码加密
|
||||
teacherMapper.save(teacher);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String login(String username, String password) {
|
||||
Teacher teacher = teacherMapper.findByUsername(username);
|
||||
if (teacher == null || !passwordEncoder.matches(password, teacher.getPassword())) {
|
||||
throw new RuntimeException("用户名或密码不正确");
|
||||
}
|
||||
// 返回 JWT token
|
||||
return JWTUtil.generateToken(teacher);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.attendance.util;
|
||||
|
||||
import com.example.attendance.entity.Teacher;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.util.Date;
|
||||
|
||||
public class JWTUtil {
|
||||
|
||||
private static final SecretKey SECRET_KEY = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
||||
|
||||
public static String generateToken(Teacher teacher) {
|
||||
return Jwts.builder()
|
||||
.setSubject(teacher.getUsername())
|
||||
.setIssuedAt(new Date())
|
||||
.setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 1000)) // Token 有效期 1 小时
|
||||
.signWith(SignatureAlgorithm.HS256, SECRET_KEY)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public static Claims extractClaims(String token) {
|
||||
try {
|
||||
return Jwts.parser()
|
||||
.setSigningKey(SECRET_KEY)
|
||||
.parseClaimsJws(token)
|
||||
.getBody();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Invalid JWT token", e); // 捕获并抛出异常
|
||||
}
|
||||
}
|
||||
|
||||
public static String getUsernameFromToken(String token) {
|
||||
return extractClaims(token).getSubject();
|
||||
}
|
||||
|
||||
public static boolean isTokenExpired(String token) {
|
||||
return extractClaims(token).getExpiration().before(new Date());
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.example.attendance;
|
||||
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
|
||||
public class PasswordEncryptor {
|
||||
public static void main(String[] args) {
|
||||
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
// 这里使用你希望的明文密码
|
||||
String rawPassword1 = "123456";
|
||||
String rawPassword2 = "654321";
|
||||
String rawPassword3 = "admin123";
|
||||
|
||||
// 加密密码
|
||||
String encodedPassword1 = passwordEncoder.encode(rawPassword1);
|
||||
String encodedPassword2 = passwordEncoder.encode(rawPassword2);
|
||||
String encodedPassword3 = passwordEncoder.encode(rawPassword3);
|
||||
|
||||
// 打印加密后的密码
|
||||
System.out.println("teacher1 密码: " + encodedPassword1);
|
||||
System.out.println("teacher2 密码: " + encodedPassword2);
|
||||
System.out.println("teacher3 密码: " + encodedPassword3);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue