增加头部名称

This commit is contained in:
zrh050423 2024-12-21 14:02:32 +08:00
parent 4884b8391e
commit 9ee7acadd0
4 changed files with 20 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import edu.zrh.healthsystem.service.LoginService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -29,13 +30,9 @@ public class LoginController {
*/
@PostMapping(value = "/login")
@ResponseBody
public LoginResult login(@RequestBody UserLogin userLogin, HttpSession session) {
public LoginResult login(@RequestBody UserLogin userLogin) {
LoginResult loginResult = userService.login(userLogin);
return loginResult;
return userService.login(userLogin);
}
/**
* 生成 JWT Token

View File

@ -12,7 +12,7 @@ public class LoginResult {
private String data;
private Integer code;
private String message;
private String username;
public LoginResult() {
}

View File

@ -7,7 +7,11 @@ import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author han
*/
@Repository
public interface LoginRepository extends JpaRepository<UserLoginInfo, Long> {
UserLoginInfo findByUsername(String username);
}

View File

@ -1,9 +1,12 @@
package edu.zrh.healthsystem.service;
import edu.zrh.healthsystem.entity.UserLoginInfo;
import edu.zrh.healthsystem.entity.UserMainInfo;
import edu.zrh.healthsystem.model.LoginResult;
import edu.zrh.healthsystem.model.UserLogin;
import edu.zrh.healthsystem.repository.LoginRepository;
import edu.zrh.healthsystem.repository.UserMainInfoRepository;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.jsonwebtoken.Jwts;
@ -13,9 +16,10 @@ public class LoginService {
@Autowired
private LoginRepository loginRepository;
public LoginService(LoginRepository loginRepository) {
private final UserMainInfoRepository userMainInfoRepository;
public LoginService(LoginRepository loginRepository, UserMainInfoRepository userMainInfoRepository) {
this.loginRepository = loginRepository;
this.userMainInfoRepository = userMainInfoRepository;
}
private UserLogin userLogin;
/**
@ -26,13 +30,16 @@ public class LoginService {
public LoginResult login(UserLogin userLogin) {
LoginResult result = new LoginResult();
UserLoginInfo storedUser = loginRepository.findByUsername(userLogin.getUsername());
if (storedUser != null && storedUser.getPwd().equals(userLogin.getPwd())) {
String userId = String.valueOf(storedUser.getId());
UserMainInfo userMainInfo = userMainInfoRepository.findById(Integer.parseInt(userId)).orElse(null);
if (storedUser.getPwd().equals(userLogin.getPwd())) {
// 密码正确生成 JWT Token
String token = generateToken(storedUser.getId());
// 假设 ID UserLogin 的字段
if (userMainInfo != null) {
result.setUsername(userMainInfo.getUsername());
}
result.setCode(200);
result.setMessage("登录成功");
result.setData(token);