XiaoRuby
2023-07-07 efbed347c8b659ca13207a1dbe34e77fdb003949
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.yuanchu.limslaboratory.controller;
 
import com.yuanchu.limslaboratory.clients.UserLoginUtils;
import com.yuanchu.limslaboratory.pojo.User;
import com.yuanchu.limslaboratory.shiro.realm.ShiroRealm;
import com.yuanchu.limslaboratory.utils.SpringUtils;
import com.yuanchu.limslaboratory.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
import org.springframework.web.bind.annotation.RestController;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2023-07-07
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {
 
    @ApiOperation("用户登录")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "account", value = "账号", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/login")
    public Result<?> UserLogin(String account, String password){
        boolean loginSuccess = false;
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(account, password);
            try {
                subject.login(usernamePasswordToken);
                loginSuccess = true;
            } catch (UnknownAccountException e) {
                return Result.fail(202, "没有找到该账号,请检查输入!");
            } catch (IncorrectCredentialsException e) {
                return Result.fail(202, "密码不匹配,请检查输入!");
            }
        }
        if (loginSuccess) {
            // 获取shiroRealm中的数据
            ShiroRealm bean = SpringUtils.getBean(ShiroRealm.class);
            User user = bean.user;
            user.setPassword(null);
            UserLoginUtils bean1 = SpringUtils.getBean(UserLoginUtils.class);
            return bean1.LoginExamine(user);
        }else {
            return Result.fail("登录失败");
        }
    }
}