|
|
@@ -1,6 +1,7 @@
|
|
1
|
1
|
package com.sundot.airport.framework.web.service;
|
|
2
|
2
|
|
|
3
|
3
|
import javax.annotation.Resource;
|
|
|
4
|
+
|
|
4
|
5
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
5
|
6
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
6
|
7
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
@@ -31,12 +32,11 @@ import com.sundot.airport.system.service.ISysUserService;
|
|
31
|
32
|
|
|
32
|
33
|
/**
|
|
33
|
34
|
* 登录校验方法
|
|
34
|
|
- *
|
|
|
35
|
+ *
|
|
35
|
36
|
* @author ruoyi
|
|
36
|
37
|
*/
|
|
37
|
38
|
@Component
|
|
38
|
|
-public class SysLoginService
|
|
39
|
|
-{
|
|
|
39
|
+public class SysLoginService {
|
|
40
|
40
|
@Autowired
|
|
41
|
41
|
private TokenService tokenService;
|
|
42
|
42
|
|
|
|
@@ -45,7 +45,7 @@ public class SysLoginService
|
|
45
|
45
|
|
|
46
|
46
|
@Autowired
|
|
47
|
47
|
private RedisCache redisCache;
|
|
48
|
|
-
|
|
|
48
|
+
|
|
49
|
49
|
@Autowired
|
|
50
|
50
|
private ISysUserService userService;
|
|
51
|
51
|
|
|
|
@@ -54,43 +54,34 @@ public class SysLoginService
|
|
54
|
54
|
|
|
55
|
55
|
/**
|
|
56
|
56
|
* 登录验证
|
|
57
|
|
- *
|
|
|
57
|
+ *
|
|
58
|
58
|
* @param username 用户名
|
|
59
|
59
|
* @param password 密码
|
|
60
|
|
- * @param code 验证码
|
|
61
|
|
- * @param uuid 唯一标识
|
|
|
60
|
+ * @param code 验证码
|
|
|
61
|
+ * @param uuid 唯一标识
|
|
62
|
62
|
* @return 结果
|
|
63
|
63
|
*/
|
|
64
|
|
- public String login(String username, String password, String code, String uuid)
|
|
65
|
|
- {
|
|
|
64
|
+ public String login(String username, String password, String code, String uuid) {
|
|
66
|
65
|
// 验证码校验
|
|
67
|
66
|
validateCaptcha(username, code, uuid);
|
|
68
|
67
|
// 登录前置校验
|
|
69
|
68
|
loginPreCheck(username, password);
|
|
70
|
69
|
// 用户验证
|
|
71
|
70
|
Authentication authentication = null;
|
|
72
|
|
- try
|
|
73
|
|
- {
|
|
|
71
|
+ try {
|
|
74
|
72
|
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
|
|
75
|
73
|
AuthenticationContextHolder.setContext(authenticationToken);
|
|
76
|
74
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
77
|
75
|
authentication = authenticationManager.authenticate(authenticationToken);
|
|
78
|
|
- }
|
|
79
|
|
- catch (Exception e)
|
|
80
|
|
- {
|
|
81
|
|
- if (e instanceof BadCredentialsException)
|
|
82
|
|
- {
|
|
|
76
|
+ } catch (Exception e) {
|
|
|
77
|
+ if (e instanceof BadCredentialsException) {
|
|
83
|
78
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
84
|
79
|
throw new UserPasswordNotMatchException();
|
|
85
|
|
- }
|
|
86
|
|
- else
|
|
87
|
|
- {
|
|
|
80
|
+ } else {
|
|
88
|
81
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
89
|
82
|
throw new ServiceException(e.getMessage());
|
|
90
|
83
|
}
|
|
91
|
|
- }
|
|
92
|
|
- finally
|
|
93
|
|
- {
|
|
|
84
|
+ } finally {
|
|
94
|
85
|
AuthenticationContextHolder.clearContext();
|
|
95
|
86
|
}
|
|
96
|
87
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
@@ -102,27 +93,23 @@ public class SysLoginService
|
|
102
|
93
|
|
|
103
|
94
|
/**
|
|
104
|
95
|
* 校验验证码
|
|
105
|
|
- *
|
|
|
96
|
+ *
|
|
106
|
97
|
* @param username 用户名
|
|
107
|
|
- * @param code 验证码
|
|
108
|
|
- * @param uuid 唯一标识
|
|
|
98
|
+ * @param code 验证码
|
|
|
99
|
+ * @param uuid 唯一标识
|
|
109
|
100
|
* @return 结果
|
|
110
|
101
|
*/
|
|
111
|
|
- public void validateCaptcha(String username, String code, String uuid)
|
|
112
|
|
- {
|
|
|
102
|
+ public void validateCaptcha(String username, String code, String uuid) {
|
|
113
|
103
|
boolean captchaEnabled = configService.selectCaptchaEnabled();
|
|
114
|
|
- if (captchaEnabled)
|
|
115
|
|
- {
|
|
|
104
|
+ if (captchaEnabled) {
|
|
116
|
105
|
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
|
|
117
|
106
|
String captcha = redisCache.getCacheObject(verifyKey);
|
|
118
|
|
- if (captcha == null)
|
|
119
|
|
- {
|
|
|
107
|
+ if (captcha == null) {
|
|
120
|
108
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
|
121
|
109
|
throw new CaptchaExpireException();
|
|
122
|
110
|
}
|
|
123
|
111
|
redisCache.deleteObject(verifyKey);
|
|
124
|
|
- if (!code.equalsIgnoreCase(captcha))
|
|
125
|
|
- {
|
|
|
112
|
+ if (!code.equalsIgnoreCase(captcha)) {
|
|
126
|
113
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
127
|
114
|
throw new CaptchaException();
|
|
128
|
115
|
}
|
|
|
@@ -131,35 +118,31 @@ public class SysLoginService
|
|
131
|
118
|
|
|
132
|
119
|
/**
|
|
133
|
120
|
* 登录前置校验
|
|
|
121
|
+ *
|
|
134
|
122
|
* @param username 用户名
|
|
135
|
123
|
* @param password 用户密码
|
|
136
|
124
|
*/
|
|
137
|
|
- public void loginPreCheck(String username, String password)
|
|
138
|
|
- {
|
|
|
125
|
+ public void loginPreCheck(String username, String password) {
|
|
139
|
126
|
// 用户名或密码为空 错误
|
|
140
|
|
- if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
|
|
141
|
|
- {
|
|
|
127
|
+ if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
|
|
142
|
128
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
|
|
143
|
129
|
throw new UserNotExistsException();
|
|
144
|
130
|
}
|
|
145
|
131
|
// 密码如果不在指定范围内 错误
|
|
146
|
132
|
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
|
147
|
|
- || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
|
|
148
|
|
- {
|
|
|
133
|
+ || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
|
149
|
134
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
150
|
135
|
throw new UserPasswordNotMatchException();
|
|
151
|
136
|
}
|
|
152
|
137
|
// 用户名不在指定范围内 错误
|
|
153
|
138
|
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
|
154
|
|
- || username.length() > UserConstants.USERNAME_MAX_LENGTH)
|
|
155
|
|
- {
|
|
|
139
|
+ || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
|
156
|
140
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
157
|
141
|
throw new UserPasswordNotMatchException();
|
|
158
|
142
|
}
|
|
159
|
143
|
// IP黑名单校验
|
|
160
|
144
|
String blackStr = configService.selectConfigByKey("sys.login.blackIPList");
|
|
161
|
|
- if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))
|
|
162
|
|
- {
|
|
|
145
|
+ if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
|
|
163
|
146
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("login.blocked")));
|
|
164
|
147
|
throw new BlackListException();
|
|
165
|
148
|
}
|
|
|
@@ -170,8 +153,7 @@ public class SysLoginService
|
|
170
|
153
|
*
|
|
171
|
154
|
* @param userId 用户ID
|
|
172
|
155
|
*/
|
|
173
|
|
- public void recordLoginInfo(Long userId)
|
|
174
|
|
- {
|
|
|
156
|
+ public void recordLoginInfo(Long userId) {
|
|
175
|
157
|
SysUser sysUser = new SysUser();
|
|
176
|
158
|
sysUser.setUserId(userId);
|
|
177
|
159
|
sysUser.setLoginIp(IpUtils.getIpAddr());
|