实现login
This commit is contained in:
parent
4eb09c3bca
commit
2848312363
@ -40,6 +40,18 @@ class AuthenticationController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
|
||||||
|
boolean isValidUser = userService.loginUser(loginRequest.getUsername(), loginRequest.getPassword());
|
||||||
|
|
||||||
|
if (isValidUser) {
|
||||||
|
return new ResponseEntity<>(new LoginResponse("登录成功", loginRequest.getUsername()), HttpStatus.OK);
|
||||||
|
} else {
|
||||||
|
return new ResponseEntity<>(new LoginResponse("用户名或密码错误", ""), HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,3 +71,18 @@ class RegisterResponse {
|
|||||||
private String message;
|
private String message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
class LoginRequest {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private String verificationCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
class LoginResponse {
|
||||||
|
private String message;
|
||||||
|
private String username;
|
||||||
|
}
|
||||||
|
|||||||
@ -52,6 +52,11 @@ public class UserService {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean loginUser(String username, String password) {
|
||||||
|
UserHS user = userRepository.findByUsername(username);
|
||||||
|
return user != null && passwordEncoder.matches(password, user.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 验证用户名格式
|
// 验证用户名格式
|
||||||
private boolean isValidUsername(String username) {
|
private boolean isValidUsername(String username) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user