LoginCommonController.java
3.74 KB
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
package com.glsx.usedcar.web.api;
import com.glsx.usedcar.web.WebConstants;
import com.glsx.usedcar.web.dto.LoginRequest;
import jakarta.security.auth.message.AuthException;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.summerframework.core.common.ResponseResult;
public class LoginCommonController {
public static final int EXPIRE_SECOND = 120;
// @Value("${spring.code.default}")
private boolean defaultCode = false;
/**
* 获取短信验证码
*
* @param loginRequest
* @param httpSession
* @return
*/
// @PostMapping(value = "smsCode")
// @ResponseBody
// public ResponseResult smsCode(@RequestBody LoginRequest loginRequest, HttpSession httpSession) {
// ResponseResult result;
// try {
// validateCode(httpSession, loginRequest.getImgCode());
// final String code = defaultCode ? "111111" : generateCode(6);
// result = smsProvider.sendCommonCode(loginRequest.getMobile(), code);
// if (result.getResult() == ResponseResult.Result.SUCCESS) {
// httpSession.setAttribute(loginRequest.getMobile(), code);
// // 5分钟内有效
// httpSession.setAttribute(WebConstants.VALID_CODE_EXPIRE_TIME, System.currentTimeMillis() + 60 * 5000);
// }
// } catch (AuthException authException) {
// result = new ResponseResult(ResponseResult.Result.FAILURE, authException.getMessage());
// }
// return result;
// }
public void validateCode(HttpSession session, String code) throws AuthException {
String codeInSession = (String) session.getAttribute(WebConstants.CODE_VALID);
long expireTime = 0L;
Long val = (Long) session.getAttribute(WebConstants.VALID_CODE_EXPIRE_TIME);
if (val != null) {
expireTime = val;
}
session.removeAttribute(WebConstants.CODE_VALID);
session.removeAttribute(WebConstants.VALID_CODE_EXPIRE_TIME);
if (defaultCode) {
return;
}
if (StringUtils.isBlank(code)) {
throw new AuthException("图形验证码不存在");
} else if (codeInSession == null) {
throw new AuthException("请先获取图形验证码");
} else if (System.currentTimeMillis() - expireTime > 0L) {
throw new AuthException("图形验证码已过期");
} else if (!codeInSession.equalsIgnoreCase(code)) {
throw new AuthException("图形验证码不正确");
}
}
public void validateSmsCode(HttpSession session, String mobile, String smsCode) throws AuthException {
String codeInSession = (String) session.getAttribute(mobile);
long expireTime = 0L;
Long val = (Long) session.getAttribute(WebConstants.VALID_CODE_EXPIRE_TIME);
if (val != null) {
expireTime = val;
}
session.removeAttribute(WebConstants.VALID_CODE_EXPIRE_TIME);
if (defaultCode) {
return;
}
if (StringUtils.isBlank(smsCode)) {
throw new AuthException("短信验证码不存在");
} else if (codeInSession == null) {
throw new AuthException("请先获取短信验证码");
} else if (System.currentTimeMillis() - expireTime > 0L) {
throw new AuthException("短信验证码已过期");
} else if (!codeInSession.equalsIgnoreCase(smsCode)) {
throw new AuthException("短信验证码不正确");
}
}
}