|
|
@@ -1,14 +1,21 @@
|
|
|
package com.usoftchina.saas.auth.controller;
|
|
|
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
import com.usoftchina.saas.account.api.AccountApi;
|
|
|
import com.usoftchina.saas.account.dto.AccountDTO;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtHelper;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtInfo;
|
|
|
import com.usoftchina.saas.auth.common.jwt.TokenVO;
|
|
|
import com.usoftchina.saas.auth.dto.AuthDTO;
|
|
|
+import com.usoftchina.saas.auth.dto.AuthorizeLogDTO;
|
|
|
import com.usoftchina.saas.auth.dto.TokenDTO;
|
|
|
+import com.usoftchina.saas.auth.po.AuthorizeLog;
|
|
|
+import com.usoftchina.saas.auth.service.AuthorizeCountService;
|
|
|
+import com.usoftchina.saas.auth.service.AuthorizeLogService;
|
|
|
import com.usoftchina.saas.base.Result;
|
|
|
import com.usoftchina.saas.exception.ExceptionCode;
|
|
|
+import com.usoftchina.saas.page.PageDefault;
|
|
|
+import com.usoftchina.saas.page.PageRequest;
|
|
|
import com.usoftchina.saas.utils.BeanMapper;
|
|
|
import com.usoftchina.saas.utils.CollectionUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -40,6 +47,15 @@ public class AuthController {
|
|
|
@Value("${auth.expire:18000}")
|
|
|
private int expire;
|
|
|
|
|
|
+ @Value("${auth.max-errors:5}")
|
|
|
+ private int maxErrors;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuthorizeLogService authorizeLogService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuthorizeCountService authorizeCountService;
|
|
|
+
|
|
|
/**
|
|
|
* 登录认证获取token
|
|
|
*
|
|
|
@@ -48,9 +64,16 @@ public class AuthController {
|
|
|
* @return
|
|
|
*/
|
|
|
@PostMapping("/authorize")
|
|
|
- public Result<AuthDTO> authorize(@RequestParam String username, @RequestParam String password) {
|
|
|
+ public Result<AuthDTO> authorize(HttpServletRequest request, @RequestParam String username, @RequestParam String password) {
|
|
|
+ // 非法操作(登录失败次数过多...)导致被冻结
|
|
|
+ if (authorizeCountService.isFrozen(username)) {
|
|
|
+ return Result.error(ExceptionCode.AUTH_FROZEN);
|
|
|
+ }
|
|
|
+
|
|
|
Result<AccountDTO> result = accountApi.validByUsernameAndPwd(username, password);
|
|
|
if (result.isSuccess()) {
|
|
|
+ authorizeCountService.clear(username);
|
|
|
+
|
|
|
AccountDTO accountDTO = result.getData();
|
|
|
Long companyId = null;
|
|
|
if (!CollectionUtils.isEmpty(accountDTO.getCompanies())) {
|
|
|
@@ -62,7 +85,16 @@ public class AuthController {
|
|
|
JwtInfo info = new JwtInfo(appId, companyId, accountDTO.getId(), accountDTO.getUsername());
|
|
|
TokenVO tokenVO = JwtHelper.generateToken(info, privateKeyPath, expire);
|
|
|
TokenDTO tokenDTO = BeanMapper.map(tokenVO, TokenDTO.class);
|
|
|
+ // 登录日志
|
|
|
+ authorizeLogService.save(AuthorizeLog.from(request)
|
|
|
+ .setAccountId(accountDTO.getId())
|
|
|
+ .setAppId(appId).build());
|
|
|
return Result.success(new AuthDTO(tokenDTO, accountDTO));
|
|
|
+ } else {
|
|
|
+ // 失败次数超过最大限制
|
|
|
+ if (authorizeCountService.increaseAndGet(username) > maxErrors) {
|
|
|
+ return Result.error(ExceptionCode.AUTH_MAX_ERRORS);
|
|
|
+ }
|
|
|
}
|
|
|
return Result.error(result.getCode(), result.getMessage());
|
|
|
}
|
|
|
@@ -121,4 +153,15 @@ public class AuthController {
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询当前用户登录日志
|
|
|
+ *
|
|
|
+ * @param page
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/log")
|
|
|
+ public Result<PageInfo<AuthorizeLogDTO>> getLogs(@PageDefault PageRequest page) {
|
|
|
+ return Result.success(authorizeLogService.findByPage(page));
|
|
|
+ }
|
|
|
}
|