LoginController.java
22.8 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package com.skua.modules.system.controller;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.skua.core.util.encryption.MD5Util;
import com.skua.modules.common.vo.AliCode;
import com.skua.modules.system.vo.SsoLogin;
import com.skua.tool.util.AliSmsUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.core.api.ISysBaseAPI;
import com.skua.core.api.vo.LoginUser;
import com.skua.core.api.vo.Result;
import com.skua.core.cache.RedisUtil;
import com.skua.core.constant.CommonConstant;
import com.skua.core.context.BaseContextHandler;
import com.skua.core.service.ISysLogService;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.DateUtils;
import com.skua.core.util.JwtUtil;
import com.skua.core.util.PasswordUtil;
import com.skua.core.util.encryption.AesEncryptUtil;
import com.skua.core.util.encryption.EncryptedString;
import com.skua.modules.shiro.vo.DefContants;
import com.skua.modules.system.entity.SysBaseConfig;
import com.skua.modules.system.entity.SysDepart;
import com.skua.modules.system.entity.SysRole;
import com.skua.modules.system.entity.SysUser;
import com.skua.modules.system.model.SysLoginModel;
import com.skua.modules.system.service.ISysBaseConfigService;
import com.skua.modules.system.service.ISysDepartService;
import com.skua.modules.system.service.ISysRoleService;
import com.skua.modules.system.service.ISysUserService;
import com.skua.modules.system.service.impl.SysUserDepartServiceImpl;
import com.skua.modules.system.vo.SysAppUserInfo;
import com.skua.modules.system.vo.SysThirdUserInfo;
import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/**
*
*/
@RestController
@RequestMapping("/sys")
@Api(tags = "用户登录")
@Slf4j
public class LoginController {
@Autowired
private ISysUserService sysUserService;
@Autowired
private ISysBaseAPI sysBaseAPI;
@Autowired
private ISysLogService logService;
@Autowired
private RedisUtil redisUtil;
@Autowired
private ISysDepartService sysDepartService;
@Autowired
private ISysRoleService sysRoleService;
@Autowired
SysUserDepartServiceImpl sysUserDepartService;
@Autowired
private ISysBaseConfigService sysBaseConfigService;
@RequestMapping(value = "/login", method = RequestMethod.POST)
//@ApiOperation("登录接口")
public Result<JSONObject> login(@RequestBody SysLoginModel sysLoginModel) throws Exception {
Result<JSONObject> result = new Result<JSONObject>();
String username = sysLoginModel.getUsername();
String password = sysLoginModel.getPassword();
//update-begin--Author:scott Date:20190805 for:暂时注释掉密码加密逻辑,有点问题
//前端密码加密,后端进行密码解密
//password = AesEncryptUtil.desEncrypt(sysLoginModel.getPassword().replaceAll("%2B", "\\+")).trim();//密码解密
//update-begin--Author:scott Date:20190805 for:暂时注释掉密码加密逻辑,有点问题
//1. 校验用户是否有效
SysUser sysUser = sysUserService.getUserByName(username);
result = sysUserService.checkUserIsEffective(sysUser);
if (!result.isSuccess()) {
return result;
}
//2. 校验用户名或密码是否正确
String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
String syspassword = sysUser.getPassword();
if (!syspassword.equals(userpassword)) {
result.error500("用户名或密码错误");
return result;
}
//用户登录信息
userInfo(sysUser, result);
sysBaseAPI.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
return result;
}
@RequestMapping(value = "/appLogin", method = RequestMethod.POST)
//@ApiOperation("APP登录接口")
public Result<SysAppUserInfo> appLogin(@RequestBody SysLoginModel sysLoginModel) throws Exception {
Result<SysAppUserInfo> result = new Result<SysAppUserInfo>();
String username = sysLoginModel.getUsername();
String password = sysLoginModel.getPassword();
//1. 校验用户是否有效
SysUser sysUser = sysUserService.getUserByName(username);
result = sysUserService.checkUserIsEffective(sysUser);
if (!result.isSuccess()) {
return result;
}
//2. 校验用户名或密码是否正确
String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
String syspassword = sysUser.getPassword();
if (!syspassword.equals(userpassword)) {
result.error500("用户名或密码错误");
return result;
}
//生成token存储redis
appUserInfo(sysUser, result);
//判断集团或者单厂登录
String userId = result.getResult().getId();
List<SysDepart> userFactoryList = sysDepartService.getUserFactoryInfo(userId);
if (userFactoryList!=null && userFactoryList.size()>0) {
SysDepart sysDepart = userFactoryList.get(0);
String departType = sysDepart.getDepartType();
if (userFactoryList.size() > 1) {
departType = "1";
}
SysAppUserInfo userInfo = result.getResult();
if (!departType.isEmpty()) {
if ("-1".equals(departType)) {
userInfo.setOrgType("group");
} else if ("1".equals(departType)||"2".equals(departType)) {
userInfo.setOrgType("factory");
userInfo.setDepartId(userFactoryList.get(0).getId());
}
}
}
sysBaseAPI.addLog("用户名: " + username + ",移动端登录成功!", CommonConstant.LOG_TYPE_1, null);
return result;
}
@RequestMapping(value = "/thirdLogin", method = RequestMethod.POST)
@ApiOperation("三方系统登录接口")
public Result<SysThirdUserInfo> thirdLogin(@RequestBody SysLoginModel sysLoginModel) throws Exception {
Result<SysThirdUserInfo> result = new Result<SysThirdUserInfo>();
String username = sysLoginModel.getUsername();
String password = sysLoginModel.getPassword();
//1. 校验用户是否有效
SysUser sysUser = sysUserService.getUserByName(username);
result = sysUserService.checkUserIsEffective(sysUser);
if (!result.isSuccess()) {
return result;
}
//2. 校验用户名或密码是否正确
String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
String syspassword = sysUser.getPassword();
if (!syspassword.equals(userpassword)) {
result.error500("用户名或密码错误");
return result;
}
//生成token存储redis
thirdUserInfo(sysUser, result);
sysBaseAPI.addLog("用户名: " + username + ",三方系统登录成功!", CommonConstant.LOG_TYPE_1, null);
return result;
}
/**
* 单点登录接口
* @param ssoLogin
* @return
*/
@RequestMapping(value = "/sso/login", method = RequestMethod.POST)
@ApiOperation("单点登录接口")
public Result<JSONObject> ssoLogin(@RequestBody SsoLogin ssoLogin){
Result<JSONObject> result = new Result<JSONObject>();
String phone = ssoLogin.getPhone();//手机号
String time = ssoLogin.getTime();//时间戳
String sign = ssoLogin.getSign();//加密信息
String key = "szhqydn";//约定标识
SysUser sysUser = sysUserService.getUserByPhone(ssoLogin.getPhone());
if(sysUser!=null){
String origin = key + time + phone;//标识
String md5 = MD5Util.MD5Encode(origin, null);
if(sign.equals(md5)){
//校验用户是否有效
result = sysUserService.checkUserIsEffective(sysUser);
if (!result.isSuccess()) {
return result;
}
//用户登录信息
userInfo(sysUser, result);
sysBaseAPI.addLog("手机号: " + phone + ",单点登录成功!", CommonConstant.LOG_TYPE_1, null);
}
}else{
result.error500("手机号不存在本系统");
}
return result;
}
@RequestMapping(value = "/getSv30Token", method = RequestMethod.POST)
@ApiOperation("SV30获取token")
public Result<JSONObject> getSv30Token(){
Result<JSONObject> result = new Result<>();
JSONObject resData = new JSONObject();
try {
Object sv30OrgCode = BaseContextHandler.get("sv30OrgCodes");
if(Objects.isNull(sv30OrgCode)){
result.error500("未找到SV30系统机构");
return result;
}
resData.put("token", AesEncryptUtil.encrypt(DateUtils.format(new Date(),"yyyyMMddHHmmss")+"#"+sv30OrgCode));
} catch (Exception e) {
e.printStackTrace();
result.error500("系统异常");
}
result.setResult(resData);
return result;
}
/**
* 生成token存储redis,并将数据封装到移动端需要的用户信息对象中
*
* @param sysUser
* @param result
* @return
*/
private Result<SysAppUserInfo> appUserInfo(SysUser sysUser, Result<SysAppUserInfo> result) {
SysAppUserInfo sysAppUserInfo = new SysAppUserInfo();
String syspassword = sysUser.getPassword();
String username = sysUser.getUsername();
// 生成token
String token = JwtUtil.sign(username, syspassword);
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
// 设置超时时间
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
sysAppUserInfo.setId(sysUser.getId());
sysAppUserInfo.setLogingName(username);
sysAppUserInfo.setToken(token);
if (!StringUtils.isEmpty(sysUser.getAvatar())) {
sysAppUserInfo.setIcon(sysUser.getAvatar());
}
sysAppUserInfo.setRealName(sysUser.getRealname());
sysAppUserInfo.setSex(sysUser.getSex());
sysAppUserInfo.setBirthday(sysUser.getBirthday());
sysAppUserInfo.setEmail(sysUser.getEmail());
sysAppUserInfo.setPhone(sysUser.getPhone());
//用户的厂区
String departId = BaseContextHandler.getDeparts();
sysAppUserInfo.setDepartId(departId);
// 获取用户部门信息
List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
if (departs.size() > 0) {
sysAppUserInfo.setDepart(departs.get(0).getDepartName());
} else {
sysAppUserInfo.setDepart("");
}
result.setResult(sysAppUserInfo);
result.success("登录成功");
return result;
}
/**
* 生成token存储redis,并将数据封装到三方系统需要的用户信息对象中
*
* @param sysUser
* @param result
* @return
*/
private Result<SysThirdUserInfo> thirdUserInfo(SysUser sysUser, Result<SysThirdUserInfo> result) {
SysThirdUserInfo sysThirdUserInfo = new SysThirdUserInfo();
String syspassword = sysUser.getPassword();
String username = sysUser.getUsername();
// 生成token
String token = JwtUtil.sign(username, syspassword);
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
// 设置超时时间
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
sysThirdUserInfo.setId(username);
sysThirdUserInfo.setToken(token);
sysThirdUserInfo.setRealName(sysUser.getRealname());
result.setResult(sysThirdUserInfo);
result.success("登录成功");
return result;
}
/**
* 用户信息
*
* @param sysUser
* @param result
* @return
*/
private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
String syspassword = sysUser.getPassword();
String username = sysUser.getUsername();
//判断该用户是否配置像素流地址,如果配置使用自己的,没有配置则默认通用的
String tempPixelStreamPath = sysUser.getPixelStreamPath();
if(StringUtils.isEmpty(tempPixelStreamPath)) {
//将默认通用地址set到用户对象里
QueryWrapper<SysBaseConfig> baseQW = new QueryWrapper<SysBaseConfig>();
SysBaseConfig sysBaseConfig = sysBaseConfigService.getOne(baseQW);
sysUser.setPixelStreamPath(sysBaseConfig.getPixelStreamPath());
}
// 生成token
String token = JwtUtil.sign(username, syspassword);
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
// 设置超时时间
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
// 获取用户部门信息
JSONObject obj = new JSONObject();
List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
obj.put("departs", departs);
// 获取用户角色信息
List<SysRole> roles = sysRoleService.queryUserRoles(sysUser.getId());
obj.put("roles", roles);
if (departs == null || departs.size() == 0) {
obj.put("multi_depart", 0);
} else if (departs.size() == 1) {
sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
obj.put("multi_depart", 1);
} else {
obj.put("multi_depart", 2);
}
obj.put("token", token);
obj.put("userInfo", sysUser);
result.setResult(obj);
result.success("登录成功");
return result;
}
/**
* 退出登录
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/logout")
public Result<Object> logout(HttpServletRequest request, HttpServletResponse response) {
//用户退出逻辑
String token = request.getHeader(DefContants.X_ACCESS_TOKEN);
if (ConvertUtils.isEmpty(token)) {
return Result.error("退出登录失败!");
}
String username = JwtUtil.getUsername(token);
SysUser sysUser = sysUserService.getUserByName(username);
if (sysUser != null) {
sysBaseAPI.addLog("用户名: " + sysUser.getRealname() + ",退出成功!", CommonConstant.LOG_TYPE_1, null);
log.info(" 用户名: " + sysUser.getRealname() + ",退出成功! ");
//清空用户Token缓存
redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token);
//清空用户权限缓存:权限Perms和角色集合
redisUtil.del(CommonConstant.LOGIN_USER_CACHERULES_ROLE + username);
redisUtil.del(CommonConstant.LOGIN_USER_CACHERULES_PERMISSION + username);
return Result.ok("退出登录成功!");
} else {
return Result.error("无效的token");
}
}
/**
* 获取访问量
*
* @return
*/
@GetMapping("loginfo")
public Result<JSONObject> loginfo() {
Result<JSONObject> result = new Result<JSONObject>();
JSONObject obj = new JSONObject();
//update-begin--Author:zhangweijian Date:20190428 for:传入开始时间,结束时间参数
// 获取一天的开始和结束时间
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date dayStart = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date dayEnd = calendar.getTime();
// 获取系统访问记录
Long totalVisitCount = logService.findTotalVisitCount();
obj.put("totalVisitCount", totalVisitCount);
Long todayVisitCount = logService.findTodayVisitCount(dayStart, dayEnd);
obj.put("todayVisitCount", todayVisitCount);
Long todayIp = logService.findTodayIp(dayStart, dayEnd);
//update-end--Author:zhangweijian Date:20190428 for:传入开始时间,结束时间参数
obj.put("todayIp", todayIp);
result.setResult(obj);
result.success("登录成功");
return result;
}
/**
* 获取访问量
*
* @return
*/
@GetMapping("visitInfo")
public Result<List<Map<String, Object>>> visitInfo() {
Result<List<Map<String, Object>>> result = new Result<List<Map<String, Object>>>();
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date dayEnd = calendar.getTime();
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date dayStart = calendar.getTime();
List<Map<String, Object>> list = logService.findVisitCount(dayStart, dayEnd);
result.setResult(ConvertUtils.toLowerCasePageList(list));
return result;
}
/**
* 登陆成功选择用户当前部门
*
* @param user
* @return
*/
@RequestMapping(value = "/selectDepart", method = RequestMethod.PUT)
public Result<JSONObject> selectDepart(@RequestBody SysUser user) {
Result<JSONObject> result = new Result<JSONObject>();
String username = user.getUsername();
if (ConvertUtils.isEmpty(username)) {
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
username = sysUser.getUsername();
}
// String orgCode= user.getOrgCode();
// this.sysUserService.updateUserDepart(username, orgCode);
SysUser sysUser = sysUserService.getUserByName(username);
JSONObject obj = new JSONObject();
obj.put("userInfo", sysUser);
result.setResult(obj);
return result;
}
/**
* 短信登录接口
*
* @param jsonObject
* @return
*/
@PostMapping(value = "/sms")
public Result<String> sms(@RequestBody JSONObject jsonObject) {
Result<String> result = new Result<String>();
String mobile = jsonObject.get("mobile").toString();
String smsmode = jsonObject.get("smsmode").toString();
log.info(mobile);
Object object = redisUtil.get(mobile);
if (object != null) {
result.setMessage("验证码10分钟内,仍然有效!");
result.setSuccess(false);
return result;
}
//随机数
String captcha = RandomUtil.randomNumbers(6);
AliCode aliCode = new AliCode();
aliCode.setProduct("数字化企业大脑");
aliCode.setCode(captcha);
try {
boolean b = false;
//注册模板
if (CommonConstant.SMS_TPL_TYPE_1.equals(smsmode)) {
SysUser sysUser = sysUserService.getUserByPhone(mobile);
if (sysUser != null) {
result.error500(" 手机号已经注册,请直接登录!");
sysBaseAPI.addLog("手机号已经注册,请直接登录!", CommonConstant.LOG_TYPE_1, null);
return result;
}
aliCode.setType("register");
} else {
//登录模式,校验用户有效性
SysUser sysUser = sysUserService.getUserByPhone(mobile);
result = sysUserService.checkUserIsEffective(sysUser);
if (!result.isSuccess()) {
return result;
}
/**
* smsmode 短信模板方式 0 .登录模板、1.注册模板、2.忘记密码模板
*/
if (CommonConstant.SMS_TPL_TYPE_0.equals(smsmode)) {
//登录模板
aliCode.setType("login");
} else if (CommonConstant.SMS_TPL_TYPE_2.equals(smsmode)) {
//忘记密码模板
aliCode.setType("update");
}
}
b = AliSmsUtil.sendCodeMsg(aliCode);
if (b == false) {
result.setMessage("短信验证码发送失败,请稍后重试");
result.setSuccess(false);
return result;
}
//验证码10分钟内有效
redisUtil.set(mobile, captcha, 600);
result.setResult(captcha);
result.setSuccess(true);
} catch (Exception e) {
e.printStackTrace();
result.error500(" 短信接口未配置,请联系管理员!");
return result;
}
return result;
}
/**
* 手机号登录接口
*
* @param jsonObject
* @return
*/
@PostMapping("/phoneLogin")
public Result<JSONObject> phoneLogin(@RequestBody JSONObject jsonObject) {
Result<JSONObject> result = new Result<JSONObject>();
String phone = jsonObject.getString("mobile");
//校验用户有效性
SysUser sysUser = sysUserService.getUserByPhone(phone);
result = sysUserService.checkUserIsEffective(sysUser);
if (!result.isSuccess()) {
return result;
}
String smscode = jsonObject.getString("captcha");
Object code = redisUtil.get(phone);
if (!smscode.equals(code)) {
result.setMessage("手机验证码错误");
return result;
}
//用户信息
userInfo(sysUser, result);
//添加日志
sysBaseAPI.addLog("用户名: " + sysUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
return result;
}
/**
* 获取加密字符串
*
* @return
*/
@GetMapping(value = "/getEncryptedString")
public Result<Map<String, String>> getEncryptedString() {
Result<Map<String, String>> result = new Result<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("key", EncryptedString.key);
map.put("iv", EncryptedString.iv);
result.setResult(map);
return result;
}
}