From cd3f72a876674116b65cecaf00c87d481cf74648 Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Tue, 14 Mar 2023 14:59:52 +0800 Subject: [PATCH] =?UTF-8?q?perf(=E7=99=BB=E5=BD=95):=20=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E7=AB=AF=E4=B8=8D=E5=8F=97=E5=A4=9A=E7=AB=AF=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/dataease/auth/api/AuthApi.java | 4 ++ .../io/dataease/auth/server/AuthServer.java | 40 +++++++++++++++++++ .../auth/service/impl/ShiroServiceImpl.java | 1 + .../plugins/server/XDingtalkServer.java | 11 ++--- .../dataease/plugins/server/XLarkServer.java | 11 ++--- mobile/src/api/auth.js | 2 +- 6 files changed, 58 insertions(+), 11 deletions(-) diff --git a/backend/src/main/java/io/dataease/auth/api/AuthApi.java b/backend/src/main/java/io/dataease/auth/api/AuthApi.java index 9b7f232543..1b379bb2b5 100644 --- a/backend/src/main/java/io/dataease/auth/api/AuthApi.java +++ b/backend/src/main/java/io/dataease/auth/api/AuthApi.java @@ -22,6 +22,10 @@ public interface AuthApi { @PostMapping("/login") Object login(LoginDto loginDto) throws Exception; + @ApiOperation("移动端登录") + @PostMapping("/mobileLogin") + Object mobileLogin(LoginDto loginDto) throws Exception; + @PostMapping("/seizeLogin") Object seizeLogin(SeizeLoginDto loginDto) throws Exception; diff --git a/backend/src/main/java/io/dataease/auth/server/AuthServer.java b/backend/src/main/java/io/dataease/auth/server/AuthServer.java index 69f05c3f7a..9f1b2b082a 100644 --- a/backend/src/main/java/io/dataease/auth/server/AuthServer.java +++ b/backend/src/main/java/io/dataease/auth/server/AuthServer.java @@ -67,6 +67,46 @@ public class AuthServer implements AuthApi { @Autowired private WsService wsService; + @Override + public Object mobileLogin(@RequestBody LoginDto loginDto) throws Exception { + String username = RsaUtil.decryptByPrivateKey(RsaProperties.privateKey, loginDto.getUsername()); + String pwd = RsaUtil.decryptByPrivateKey(RsaProperties.privateKey, loginDto.getPassword()); + AccountLockStatus accountLockStatus = authUserService.lockStatus(username, 0); + if (accountLockStatus.getLocked()) { + String msg = Translator.get("I18N_ACCOUNT_LOCKED"); + msg = String.format(msg, username, accountLockStatus.getRelieveTimes().toString()); + DataEaseException.throwException(msg); + } + + SysUserEntity user = authUserService.getUserByName(username); + + if (ObjectUtils.isEmpty(user)) { + AccountLockStatus lockStatus = authUserService.recordLoginFail(username, 0); + DataEaseException.throwException(appendLoginErrorMsg(Translator.get("i18n_id_or_pwd_error"), lockStatus)); + } + if (user.getEnabled() == 0) { + AccountLockStatus lockStatus = authUserService.recordLoginFail(username, 0); + DataEaseException.throwException(appendLoginErrorMsg(Translator.get("i18n_user_is_disable"), lockStatus)); + } + String realPwd = user.getPassword(); + pwd = CodingUtil.md5(pwd); + + if (!StringUtils.equals(pwd, realPwd)) { + AccountLockStatus lockStatus = authUserService.recordLoginFail(username, 0); + DataEaseException.throwException(appendLoginErrorMsg(Translator.get("i18n_id_or_pwd_error"), lockStatus)); + } + TokenInfo tokenInfo = TokenInfo.builder().userId(user.getUserId()).username(username).build(); + String token = JWTUtils.sign(tokenInfo, realPwd, false); + // 记录token操作时间 + Map result = new HashMap<>(); + result.put("token", token); + ServletUtils.setToken(token); + DeLogUtils.save(SysLogConstants.OPERATE_TYPE.LOGIN, SysLogConstants.SOURCE_TYPE.USER, user.getUserId(), null, null, null); + authUserService.unlockAccount(username, 0); + authUserService.clearCache(user.getUserId()); + return result; + } + @Override public Object login(@RequestBody LoginDto loginDto) throws Exception { Map result = new HashMap<>(); diff --git a/backend/src/main/java/io/dataease/auth/service/impl/ShiroServiceImpl.java b/backend/src/main/java/io/dataease/auth/service/impl/ShiroServiceImpl.java index 6fb74fc1bf..c8f29c9af8 100644 --- a/backend/src/main/java/io/dataease/auth/service/impl/ShiroServiceImpl.java +++ b/backend/src/main/java/io/dataease/auth/service/impl/ShiroServiceImpl.java @@ -83,6 +83,7 @@ public class ShiroServiceImpl implements ShiroService { filterChainDefinitionMap.put("/api/auth/login", ANON); filterChainDefinitionMap.put("/api/auth/seizeLogin", ANON); filterChainDefinitionMap.put("/api/auth/logout", ANON); + filterChainDefinitionMap.put("/api/auth/mobileLogin", ANON); filterChainDefinitionMap.put("/api/auth/isPluginLoaded", ANON); filterChainDefinitionMap.put("/system/requestTimeOut", ANON); filterChainDefinitionMap.put("/api/auth/validateName", ANON); diff --git a/backend/src/main/java/io/dataease/plugins/server/XDingtalkServer.java b/backend/src/main/java/io/dataease/plugins/server/XDingtalkServer.java index 3105858c62..9a3964d444 100644 --- a/backend/src/main/java/io/dataease/plugins/server/XDingtalkServer.java +++ b/backend/src/main/java/io/dataease/plugins/server/XDingtalkServer.java @@ -80,7 +80,7 @@ public class XDingtalkServer { return dingtalkXpackService.getQrParam(); } - private ModelAndView privateCallBack(String code, Boolean withoutLogin) { + private ModelAndView privateCallBack(String code, Boolean withoutLogin, Boolean isMobile) { ModelAndView modelAndView = new ModelAndView("redirect:/"); HttpServletResponse response = ServletUtils.response(); DingtalkXpackService dingtalkXpackService = null; @@ -109,7 +109,7 @@ public class XDingtalkServer { } TokenInfo tokenInfo = TokenInfo.builder().userId(sysUserEntity.getUserId()).username(sysUserEntity.getUsername()).build(); String realPwd = sysUserEntity.getPassword(); - String token = JWTUtils.sign(tokenInfo, realPwd); + String token = JWTUtils.sign(tokenInfo, realPwd, !isMobile); ServletUtils.setToken(token); DeLogUtils.save(SysLogConstants.OPERATE_TYPE.LOGIN, SysLogConstants.SOURCE_TYPE.USER, sysUserEntity.getUserId(), null, null, null); @@ -144,13 +144,14 @@ public class XDingtalkServer { } @GetMapping("/callBackWithoutLogin") - public ModelAndView callBackWithoutLogin(@RequestParam("code") String code) { - return privateCallBack(code, true); + public ModelAndView callBackWithoutLogin(@RequestParam("code") String code, @RequestParam("mobile") String mobile) { + boolean isMobile = StringUtils.equals("1", mobile); + return privateCallBack(code, true, isMobile); } @GetMapping("/callBack") public ModelAndView callBack(@RequestParam("code") String code, @RequestParam("state") String state) { - return privateCallBack(code, false); + return privateCallBack(code, false, false); } private void bindError(HttpServletResponse response, String url, String errorMsg) { diff --git a/backend/src/main/java/io/dataease/plugins/server/XLarkServer.java b/backend/src/main/java/io/dataease/plugins/server/XLarkServer.java index 9651776b56..79f6fefbe0 100644 --- a/backend/src/main/java/io/dataease/plugins/server/XLarkServer.java +++ b/backend/src/main/java/io/dataease/plugins/server/XLarkServer.java @@ -92,11 +92,12 @@ public class XLarkServer { } @GetMapping("/callBackWithoutLogin") - public ModelAndView callBackWithoutLogin(@RequestParam("code") String code) { - return privateCallBack(code, null, true); + public ModelAndView callBackWithoutLogin(@RequestParam("code") String code, @RequestParam("mobile") String mobile) { + boolean isMobile = StringUtils.equals("1", mobile); + return privateCallBack(code, null, true, isMobile); } - private ModelAndView privateCallBack(String code, String state, Boolean withoutLogin) { + private ModelAndView privateCallBack(String code, String state, Boolean withoutLogin, Boolean isMobile) { ModelAndView modelAndView = new ModelAndView("redirect:/"); HttpServletResponse response = ServletUtils.response(); LarkXpackService larkXpackService = null; @@ -132,7 +133,7 @@ public class XLarkServer { } TokenInfo tokenInfo = TokenInfo.builder().userId(sysUserEntity.getUserId()).username(sysUserEntity.getUsername()).build(); String realPwd = sysUserEntity.getPassword(); - String token = JWTUtils.sign(tokenInfo, realPwd); + String token = JWTUtils.sign(tokenInfo, realPwd, !isMobile); ServletUtils.setToken(token); DeLogUtils.save(SysLogConstants.OPERATE_TYPE.LOGIN, SysLogConstants.SOURCE_TYPE.USER, sysUserEntity.getUserId(), null, null, null); @@ -168,7 +169,7 @@ public class XLarkServer { @GetMapping("/callBack") public ModelAndView callBack(@RequestParam("code") String code, @RequestParam("state") String state) { - return privateCallBack(code, state, false); + return privateCallBack(code, state, false, false); } private void bindError(HttpServletResponse response, String url, String errorMsg) { diff --git a/mobile/src/api/auth.js b/mobile/src/api/auth.js index cca85e3817..464816d0b9 100644 --- a/mobile/src/api/auth.js +++ b/mobile/src/api/auth.js @@ -2,7 +2,7 @@ import request from '@/common/js/request' export function login(data) { return request({ - url: '/api/auth/login', + url: '/api/auth/mobileLogin', method: 'post', data })