# 实现用户登录

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUserForLogin(String username, String passwod) {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        criteria.andEqualTo("password", passwod);

        Users result = usersMapper.selectOneByExample(example);
        return result;
    }
1
2
3
4
5
6
7
8
9
10
11
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public JSONResult login(@RequestBody UserBO userBO) throws Exception {
        String username = userBO.getUsername();
        String password = userBO.getPassword();
        // 0. 判断用户名和密码必须不为空
        if (StringUtils.isBlank(username) ||
                StringUtils.isBlank(password)) {
            return JSONResult.errorMsg("用户名或密码不能为空");
        }
        password = MD5Utils.getMD5Str(password);
        Users user = userService.queryUserForLogin(username, password);
        if (user == null) {
            return JSONResult.errorMsg("用户名或密码不正确");
        }
        return JSONResult.ok(user);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

测试输出

POST http://localhost:8088/passport/login

HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 13 Feb 2021 08:57:55 GMT

{
  "status": 200,
  "msg": "OK",
  "data": {
    "id": "210213BX4KY0M5AW",
    "username": "test",
    "password": "4QrcOUm6Wau+VuBX8g+IPg==",
    "nickname": "test",
    "realname": null,
    "face": "http://localhost:900/xx.jpg",
    "mobile": null,
    "email": null,
    "sex": 2,
    "birthday": "1899-12-31T00:00:00.000+0000",
    "createdTime": "2021-02-13T08:40:20.000+0000",
    "updatedTime": "2021-02-13T08:40:20.000+0000"
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

响应的信息里面有一些敏感信息,比如密码,后面再解决这个 脱敏的问题