修改 foodie-dev-api 模块下的 PassportController 类,将查询到的部分内容设置为空,不返回给前端显示,并将返回的内容放到 cookie 中

    1. package com.imooc.controller;
    2. import com.imooc.pojo.Users;
    3. import com.imooc.pojo.bo.UserBO;
    4. import com.imooc.service.UserService;
    5. import com.imooc.utils.CookieUtils;
    6. import com.imooc.utils.IMOOCJSONResult;
    7. import com.imooc.utils.JsonUtils;
    8. import com.imooc.utils.MD5Utils;
    9. import io.swagger.annotations.Api;
    10. import io.swagger.annotations.ApiOperation;
    11. import org.apache.commons.lang3.StringUtils;
    12. import org.springframework.beans.factory.annotation.Autowired;
    13. import org.springframework.web.bind.annotation.*;
    14. import javax.servlet.http.HttpServletRequest;
    15. import javax.servlet.http.HttpServletResponse;
    16. /**
    17. * @author 92578
    18. * @since 1.0
    19. */
    20. @Api(value = "注册登录", tags = {"用于注册登录的相关接口"})
    21. @RestController
    22. @RequestMapping("passport")
    23. public class PassportController {
    24. @Autowired
    25. private UserService userService;
    26. @ApiOperation(value = "用户名是否存在", notes = "用户名是否存在", httpMethod = "GET")
    27. @GetMapping("/usernameIsExist")
    28. public IMOOCJSONResult usernameIsExist(@RequestParam String username) {
    29. // 1. 判断用户名不能为空
    30. if (StringUtils.isBlank(username)) {
    31. return IMOOCJSONResult.errorMsg("用户名不能为空");
    32. }
    33. // 2. 查找注册的用户名是否存在
    34. boolean isExist = userService.queryUsernameIsExist(username);
    35. if (isExist) {
    36. return IMOOCJSONResult.errorMsg("用户名已经存在");
    37. }
    38. // 3. 请求成功,用户名没有重复
    39. return IMOOCJSONResult.ok();
    40. }
    41. @ApiOperation(value = "用户注册", notes = "用户注册", httpMethod = "POST")
    42. @PostMapping("/regist")
    43. public IMOOCJSONResult regist(@RequestBody UserBO userBO,
    44. HttpServletRequest request,
    45. HttpServletResponse response) {
    46. String username = userBO.getUsername();
    47. String password = userBO.getPassword();
    48. String confirmPwd = userBO.getConfirmPassword();
    49. // 0. 判断用户名和密码不为空
    50. if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(confirmPwd)) {
    51. return IMOOCJSONResult.errorMsg("用户名或密码不能为空");
    52. }
    53. // 1. 查询用户名是否存在
    54. boolean isExist = userService.queryUsernameIsExist(username);
    55. if (isExist) {
    56. return IMOOCJSONResult.errorMsg("用户名已经存在");
    57. }
    58. // 2. 密码长度不能少于 6 位
    59. if (password.length() < 6) {
    60. return IMOOCJSONResult.errorMsg("密码长度不能少于6");
    61. }
    62. // 3. 判断两次密码是否一致
    63. if (!password.equals(confirmPwd)) {
    64. return IMOOCJSONResult.errorMsg("两次密码输入不一致");
    65. }
    66. // 4. 实现注册
    67. Users userResult = userService.createUser(userBO);
    68. userResult = setNullProperty(userResult);
    69. CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(userResult), true);
    70. return IMOOCJSONResult.ok();
    71. }
    72. @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST")
    73. @PostMapping("/login")
    74. public IMOOCJSONResult login(@RequestBody UserBO userBO,
    75. HttpServletRequest request,
    76. HttpServletResponse response) throws Exception {
    77. String username = userBO.getUsername();
    78. String password = userBO.getPassword();
    79. // 0. 判断用户名和密码不为空
    80. if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
    81. return IMOOCJSONResult.errorMsg("用户名或密码不能为空");
    82. }
    83. // 1. 实现登录
    84. Users userResult = userService.queryUserForLogin(username, MD5Utils.getMD5Str(password));
    85. if (userResult == null) {
    86. return IMOOCJSONResult.errorMsg("用户名或密码不正确");
    87. }
    88. userResult = setNullProperty(userResult);
    89. CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(userResult), true);
    90. return IMOOCJSONResult.ok(userResult);
    91. }
    92. private Users setNullProperty(Users userResult) {
    93. userResult.setPassword(null);
    94. userResult.setMobile(null);
    95. userResult.setEmail(null);
    96. userResult.setCreatedTime(null);
    97. userResult.setUpdatedTime(null);
    98. userResult.setBirthday(null);
    99. return userResult;
    100. }
    101. }

    启动项目,打开浏览器,访问 http://localhost:8080/foodie-shop/login.html 输入用户名“imooc”,密码“123123”进行登录
    image.png
    登录成功后显示用户名、昵称和头像
    image.png