提交 | 用户 | 时间
|
e7c126
|
1 |
package com.xxl.job.admin.service; |
H |
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 |
private XxlJobUser parseToken(String tokenHex){ |
|
35 |
XxlJobUser xxlJobUser = null; |
|
36 |
if (tokenHex != null) { |
|
37 |
String tokenJson = new String(new BigInteger(tokenHex, 16).toByteArray()); // username_password(md5) |
|
38 |
xxlJobUser = JacksonUtil.readValue(tokenJson, XxlJobUser.class); |
|
39 |
} |
|
40 |
return xxlJobUser; |
|
41 |
} |
|
42 |
|
|
43 |
|
|
44 |
public ReturnT<String> login(HttpServletRequest request, HttpServletResponse response, String username, String password, boolean ifRemember){ |
|
45 |
|
|
46 |
// param |
|
47 |
if (username==null || username.trim().length()==0 || password==null || password.trim().length()==0){ |
|
48 |
return new ReturnT<String>(500, I18nUtil.getString("login_param_empty")); |
|
49 |
} |
|
50 |
|
|
51 |
// valid passowrd |
|
52 |
XxlJobUser xxlJobUser = xxlJobUserDao.loadByUserName(username); |
|
53 |
if (xxlJobUser == null) { |
|
54 |
return new ReturnT<String>(500, I18nUtil.getString("login_param_unvalid")); |
|
55 |
} |
|
56 |
String passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes()); |
|
57 |
if (!passwordMd5.equals(xxlJobUser.getPassword())) { |
|
58 |
return new ReturnT<String>(500, I18nUtil.getString("login_param_unvalid")); |
|
59 |
} |
|
60 |
|
|
61 |
String loginToken = makeToken(xxlJobUser); |
|
62 |
|
|
63 |
// do login |
|
64 |
CookieUtil.set(response, LOGIN_IDENTITY_KEY, loginToken, ifRemember); |
|
65 |
return ReturnT.SUCCESS; |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* logout |
|
70 |
* |
|
71 |
* @param request |
|
72 |
* @param response |
|
73 |
*/ |
|
74 |
public ReturnT<String> logout(HttpServletRequest request, HttpServletResponse response){ |
|
75 |
CookieUtil.remove(request, response, LOGIN_IDENTITY_KEY); |
|
76 |
return ReturnT.SUCCESS; |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* logout |
|
81 |
* |
|
82 |
* @param request |
|
83 |
* @return |
|
84 |
*/ |
|
85 |
public XxlJobUser ifLogin(HttpServletRequest request, HttpServletResponse response){ |
|
86 |
String cookieToken = CookieUtil.getValue(request, LOGIN_IDENTITY_KEY); |
|
87 |
if (cookieToken != null) { |
|
88 |
XxlJobUser cookieUser = null; |
|
89 |
try { |
|
90 |
cookieUser = parseToken(cookieToken); |
|
91 |
} catch (Exception e) { |
|
92 |
logout(request, response); |
|
93 |
} |
|
94 |
if (cookieUser != null) { |
|
95 |
XxlJobUser dbUser = xxlJobUserDao.loadByUserName(cookieUser.getUsername()); |
|
96 |
if (dbUser != null) { |
|
97 |
if (cookieUser.getPassword().equals(dbUser.getPassword())) { |
|
98 |
return dbUser; |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
return null; |
|
104 |
} |
|
105 |
|
|
106 |
|
|
107 |
} |