whyclxw
2022-01-10 00b61a61b2a90c85aeae872e351ddf4bb2b516cf
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.whyc.service;
 
import com.whyc.dto.Response;
import com.whyc.pojo.UserClient;
import com.whyc.pojo.UserInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
 
@Service
public class LoginService {
 
    public Response login(String userName, String password, HttpServletRequest request) {
        UsernamePasswordToken userToken = new UsernamePasswordToken(userName, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(userToken);
        }catch (Exception e){
            String message = e.getMessage();
            if(message.contains("did not match the expected credentials")){
                return new Response<>().set(1,false,"密码错误");
            }
            return new Response<>().set(1,false,message);
        }
        if (subject.isAuthenticated()){
            //每个登录的用户都有一个全局变量,里面存着对应的SessionId;
            //同一个账号,后面登录的,会挤掉之前登录的SessionId,这个todo,做限制账号同时登陆人数为1
            request.getServletContext().setAttribute(userName,request.getSession().getId());
            //Session存储当前用户
            request.getSession().setAttribute("user",subject.getPrincipal());
            return new Response<>().setII(1,true,subject.getPrincipal(),"登录成功");
        }
        return new Response<>().set(1,false,"密码错误");
    }
 
    public Response loginWithUKey(String userName, String password, HttpServletRequest request) {
        UsernamePasswordToken userToken = new UsernamePasswordToken(userName, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(userToken);
        }catch (Exception e){
            String message = e.getMessage();
            if(message.contains("did not match the expected credentials")){
                return new Response<>().set(1,false,"密码错误");
            }
            return new Response<>().set(1,false,message);
        }
        if (subject.isAuthenticated()){
            //每个登录的用户都有一个全局变量,里面存着对应的SessionId;
            //同一个账号,后面登录的,会挤掉之前登录的SessionId
            System.out.println("全局存储中当前SessionId为:"+request.getSession().getId());
            request.getServletContext().setAttribute(userName,request.getSession().getId());
            //uKey和人脸识别 TODO
            return new Response<>().set(1,true,"登录成功");
        }
        return new Response<>().set(1,false,"密码错误");
    }
    /**
     * 开始查看application中是否有另一用使用该账号登陆
     *
     * @return
     */
 
    public Response checkUser(){
        Response model = new Response();
        Map<String, UserClient> map = (Map) ActionUtil.getApplication().getAttribute("users");
        // System.out.println(map);
        if (map != null && map.size() > 0) {
            HttpSession session = ActionUtil.getSession();
            // System.out.println(session);
            UserInf user = (UserInf) session.getAttribute("user");
            Long login_time = (Long) session.getAttribute("login_time");
            if (user != null && login_time != null) {
                UserClient client = map.get(user.getUName());
                if (client != null) {
                    if (login_time != client.getLogin_times()) {
                        model.setCode(1);
                        //model.setMsg(getText("The landing on the account in another host, please log in again"));
                        model.setMsg("The landing on the account in another host, please log in again");
                    }
                }
            } else {
                model.setCode(1);
                //model.setMsg(getText("You are not logged in, please log in"));
                model.setMsg("You are not logged in, please log in");
            }
        } else {
            model.setCode(1);
            //model.setMsg(getText("You are not logged in, please log in"));
            model.setMsg("You are not logged in, please log in");
        }
        return model;
    }
 
 
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }
}