xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 package com.xxl.job.admin.service;
X 2
3 import com.xxl.job.admin.core.model.XxlJobUser;
4 import com.xxl.job.admin.core.util.CookieUtil;
5 import com.xxl.job.admin.core.util.I18nUtil;
6 import com.xxl.job.admin.core.util.JacksonUtil;
7 import com.xxl.job.admin.dao.XxlJobUserDao;
8 import com.xxl.job.core.biz.model.ReturnT;
9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.util.DigestUtils;
11
12 import javax.annotation.Resource;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import java.math.BigInteger;
16
17 /**
18  * @author xuxueli 2019-05-04 22:13:264
19  */
20 @Configuration
21 public class LoginService {
22
23     public static final String LOGIN_IDENTITY_KEY = "XXL_JOB_LOGIN_IDENTITY";
24
25     @Resource
26     private XxlJobUserDao xxlJobUserDao;
27
28
29     private String makeToken(XxlJobUser xxlJobUser) {
30         String tokenJson = JacksonUtil.writeValueAsString(xxlJobUser);
31         String tokenHex = new BigInteger(tokenJson.getBytes()).toString(16);
32         return tokenHex;
33     }
34
35     private XxlJobUser parseToken(String tokenHex) {
36         XxlJobUser xxlJobUser = null;
37         if (tokenHex != null) {
38             String tokenJson = new String(new BigInteger(tokenHex, 16).toByteArray());      // username_password(md5)
39             xxlJobUser = JacksonUtil.readValue(tokenJson, XxlJobUser.class);
40         }
41         return xxlJobUser;
42     }
43
44
45     public ReturnT<String> login(HttpServletRequest request, HttpServletResponse response, String username, String password, boolean ifRemember) {
46
47         // param
48         if (username == null || username.trim().length() == 0 || password == null || password.trim().length() == 0) {
49             return new ReturnT<String>(500, I18nUtil.getString("login_param_empty"));
50         }
51
52         // valid passowrd
53         XxlJobUser xxlJobUser = xxlJobUserDao.loadByUserName(username);
54         if (xxlJobUser == null) {
55             return new ReturnT<String>(500, I18nUtil.getString("login_param_unvalid"));
56         }
57         String passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
58         if (!passwordMd5.equals(xxlJobUser.getPassword())) {
59             return new ReturnT<String>(500, I18nUtil.getString("login_param_unvalid"));
60         }
61
62         String loginToken = makeToken(xxlJobUser);
63
64         // do login
65         CookieUtil.set(response, LOGIN_IDENTITY_KEY, loginToken, ifRemember);
66         return ReturnT.SUCCESS;
67     }
68
69     /**
70      * logout
71      *
72      * @param request
73      * @param response
74      */
75     public ReturnT<String> logout(HttpServletRequest request, HttpServletResponse response) {
76         CookieUtil.remove(request, response, LOGIN_IDENTITY_KEY);
77         return ReturnT.SUCCESS;
78     }
79
80     /**
81      * logout
82      *
83      * @param request
84      * @return
85      */
86     public XxlJobUser ifLogin(HttpServletRequest request, HttpServletResponse response) {
87         String cookieToken = CookieUtil.getValue(request, LOGIN_IDENTITY_KEY);
88         if (cookieToken != null) {
89             XxlJobUser cookieUser = null;
90             try {
91                 cookieUser = parseToken(cookieToken);
92             } catch (Exception e) {
93                 logout(request, response);
94             }
95             if (cookieUser != null) {
96                 XxlJobUser dbUser = xxlJobUserDao.loadByUserName(cookieUser.getUsername());
97                 if (dbUser != null) {
98                     if (cookieUser.getPassword().equals(dbUser.getPassword())) {
99                         return dbUser;
100                     }
101                 }
102             }
103         }
104         return null;
105     }
106
107
108 }