添加jwt支持

This commit is contained in:
高子兴 2024-07-01 17:47:59 +08:00
parent 57b75e4813
commit 442eaf2479

View File

@ -4,6 +4,7 @@ package org.cmh.backend.authentication.controller;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.cmh.backend.Utils.JwtUtil;
import org.cmh.backend.authentication.service.UserService; import org.cmh.backend.authentication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -45,7 +46,7 @@ class AuthenticationController {
boolean isValidUser = userService.loginUser(loginRequest.getUsername(), loginRequest.getPassword()); boolean isValidUser = userService.loginUser(loginRequest.getUsername(), loginRequest.getPassword());
if (isValidUser) { if (isValidUser) {
return new ResponseEntity<>(new LoginResponse("登录成功", loginRequest.getUsername()), HttpStatus.OK); return new ResponseEntity<>(new LoginResponse("登录成功", JwtUtil.generateToken(loginRequest.getUsername())), HttpStatus.OK);
} else { } else {
return new ResponseEntity<>(new LoginResponse("用户名或密码错误", ""), HttpStatus.UNAUTHORIZED); return new ResponseEntity<>(new LoginResponse("用户名或密码错误", ""), HttpStatus.UNAUTHORIZED);
} }
@ -84,5 +85,5 @@ class LoginRequest {
@AllArgsConstructor @AllArgsConstructor
class LoginResponse { class LoginResponse {
private String message; private String message;
private String username; private String jwt;
} }