You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

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