更新 UserController.java
parent
ed0204aae5
commit
29ec339e8f
|
|
@ -1,12 +1,17 @@
|
||||||
package com.gao.finalhw.controller;
|
package com.gao.finalhw.controller;
|
||||||
|
|
||||||
|
import com.gao.finalhw.model.PasswordData;
|
||||||
import com.gao.finalhw.model.User;
|
import com.gao.finalhw.model.User;
|
||||||
import com.gao.finalhw.pojo.LoginRequest;
|
import com.gao.finalhw.pojo.LoginRequest;
|
||||||
import com.gao.finalhw.response.ServerResponseEntity;
|
import com.gao.finalhw.response.ServerResponseEntity;
|
||||||
import com.gao.finalhw.service.UserService;
|
import com.gao.finalhw.service.UserService;
|
||||||
|
import jakarta.servlet.http.Cookie;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
@ -16,19 +21,55 @@ public class UserController {
|
||||||
private final Logger logger = LoggerFactory.getLogger(UserController.class);
|
private final Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public ServerResponseEntity<User> login(@RequestBody LoginRequest loginRequest) {
|
public ServerResponseEntity<User> login(@RequestBody LoginRequest loginRequest, HttpSession session, HttpServletResponse response) {
|
||||||
logger.info("username: {},password: {}",loginRequest.getUsername(),loginRequest.getPassword());
|
logger.info("username: {},password: {}", loginRequest.getUsername(), loginRequest.getPassword());
|
||||||
User user = userService.getUserByUsernameAndPassword(loginRequest.getUsername(), loginRequest.getPassword());
|
User user = userService.getUserByUsernameAndPassword(loginRequest.getUsername(), loginRequest.getPassword());
|
||||||
logger.info("user: {}",user.toString());
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
logger.info("user: {}", user);
|
||||||
// 隐藏密码
|
// 隐藏密码
|
||||||
user.setPassword("null");
|
user.setPassword("null");
|
||||||
|
Cookie cookie = new Cookie("user_id", String.valueOf(user.getUserId()));
|
||||||
|
response.addCookie(cookie);
|
||||||
|
session.setAttribute("user", user);
|
||||||
// 如果验证成功
|
// 如果验证成功
|
||||||
return ServerResponseEntity.success("1", user);
|
return ServerResponseEntity.success(0, user);
|
||||||
} else {
|
} else {
|
||||||
// 如果验证失败
|
// 如果验证失败
|
||||||
return ServerResponseEntity.fail("username or password error.");
|
return ServerResponseEntity.fail("username or password error.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/register")
|
||||||
|
public ServerResponseEntity<Boolean> register(@RequestBody User user) {
|
||||||
|
if (userService.register(user)) {
|
||||||
|
return ServerResponseEntity.success(true);
|
||||||
|
}
|
||||||
|
return ServerResponseEntity.fail("register fail");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/user/update")
|
||||||
|
public ServerResponseEntity<Boolean> updatePassword(@RequestBody PasswordData passwordData) {
|
||||||
|
if(passwordData != null){
|
||||||
|
boolean isSuccess = userService.updateUserPasswordByUserId(passwordData);
|
||||||
|
if (isSuccess) {
|
||||||
|
return ServerResponseEntity.success(true);
|
||||||
|
}
|
||||||
|
return ServerResponseEntity.fail("update password fail");
|
||||||
|
}
|
||||||
|
return ServerResponseEntity.fail("data error");
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/logout")
|
||||||
|
public ServerResponseEntity<Boolean> logout(HttpSession session) {
|
||||||
|
try {
|
||||||
|
session.removeAttribute("user");
|
||||||
|
return ServerResponseEntity.success(true);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("server error", e);
|
||||||
|
return ServerResponseEntity.fail("server error");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in New Issue