|
|
@@ -0,0 +1,103 @@
|
|
|
+package com.usoftchina.saas.operation.auth.controller;
|
|
|
+
|
|
|
+import com.usoftchina.saas.account.api.AccountApi;
|
|
|
+import com.usoftchina.saas.account.dto.AccountDTO;
|
|
|
+import com.usoftchina.saas.account.dto.CompanyBaseDTO;
|
|
|
+import com.usoftchina.saas.auth.common.jwt.JwtHelper;
|
|
|
+import com.usoftchina.saas.auth.common.jwt.JwtInfo;
|
|
|
+import com.usoftchina.saas.auth.common.jwt.JwtToken;
|
|
|
+import com.usoftchina.saas.auth.dto.AuthDTO;
|
|
|
+import com.usoftchina.saas.auth.dto.TokenDTO;
|
|
|
+import com.usoftchina.saas.base.Result;
|
|
|
+import com.usoftchina.saas.cache.CacheKeyHelper;
|
|
|
+import com.usoftchina.saas.exception.ExceptionCode;
|
|
|
+import com.usoftchina.saas.operation.auth.config.AuthConfig;
|
|
|
+import com.usoftchina.saas.operation.auth.po.AuthorizeLog;
|
|
|
+import com.usoftchina.saas.operation.auth.service.AuthorizeCountService;
|
|
|
+import com.usoftchina.saas.operation.auth.service.AuthorizeLogService;
|
|
|
+import com.usoftchina.saas.utils.BeanMapper;
|
|
|
+import com.usoftchina.saas.utils.CollectionUtils;
|
|
|
+import com.usoftchina.saas.utils.RedisUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author yingp
|
|
|
+ * @date 2019/1/3
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping
|
|
|
+@EnableConfigurationProperties(AuthConfig.class)
|
|
|
+public class AuthController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AccountApi accountApi;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuthConfig authConfig;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuthorizeCountService authorizeCountService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuthorizeLogService authorizeLogService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录认证获取token
|
|
|
+ *
|
|
|
+ * @param username
|
|
|
+ * @param password
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("/authorize")
|
|
|
+ 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();
|
|
|
+ if (!CollectionUtils.isEmpty(accountDTO.getCompanies())) {
|
|
|
+ return Result.error(ExceptionCode.USER_NOT_ENABLE);
|
|
|
+ }
|
|
|
+ // 是否绑定到了允许登录运营系统的公司
|
|
|
+ Optional<CompanyBaseDTO> companyDTO = accountDTO.getCompanies().stream()
|
|
|
+ .filter(c -> authConfig.getCompanies().stream().anyMatch(id -> id.equals(c.getId())))
|
|
|
+ .findFirst();
|
|
|
+ if (!companyDTO.isPresent()) {
|
|
|
+ return Result.error(ExceptionCode.USER_NOT_ENABLE);
|
|
|
+ }
|
|
|
+ String appId = "operation";
|
|
|
+
|
|
|
+ JwtInfo info = new JwtInfo(appId, companyDTO.get().getId(), accountDTO.getId(), accountDTO.getUsername(), accountDTO.getRealname());
|
|
|
+ JwtToken jwtToken = JwtHelper.generateToken(info, authConfig.getPrivateKey(), authConfig.getExpire());
|
|
|
+ TokenDTO tokenDTO = BeanMapper.map(jwtToken, TokenDTO.class);
|
|
|
+ // 登录成功记入redis
|
|
|
+ String key = CacheKeyHelper.generatePublicKey(tokenDTO.getToken());
|
|
|
+ RedisUtil.set(key, info, authConfig.getExpire());
|
|
|
+ // 登录日志
|
|
|
+ authorizeLogService.save(AuthorizeLog.from(request)
|
|
|
+ .setAccountId(accountDTO.getId())
|
|
|
+ .setAppId(appId).build());
|
|
|
+ return Result.success(new AuthDTO(tokenDTO, accountDTO));
|
|
|
+ } else {
|
|
|
+ // 失败次数超过最大限制
|
|
|
+ if (authorizeCountService.increaseAndGet(username) > authConfig.getMaxErrors()) {
|
|
|
+ return Result.error(ExceptionCode.AUTH_MAX_ERRORS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.error(result.getCode(), result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|