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