From 3ad537052477fe31f45bd2e16045d58b35713bdc Mon Sep 17 00:00:00 2001 From: baoshiwei <baoshiwei@shlanbao.cn> Date: 星期六, 24 五月 2025 09:19:59 +0800 Subject: [PATCH] refactor(env): 更新环境配置并优化代码 --- zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java | 153 ++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 133 insertions(+), 20 deletions(-) diff --git a/zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java b/zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java index d881545..744319d 100644 --- a/zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java +++ b/zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java @@ -1,15 +1,7 @@ package com.zhitan.framework.web.service; -import javax.annotation.Resource; - -import com.zhitan.framework.manager.AsyncManager; -import com.zhitan.framework.manager.factory.AsyncFactory; -import com.zhitan.framework.security.context.AuthenticationContextHolder; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.authentication.BadCredentialsException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.stereotype.Component; +import cn.hutool.core.collection.CollUtil; +import com.zhitan.common.config.keycloak.AuthKeycloakRequest; import com.zhitan.common.constant.CacheConstants; import com.zhitan.common.constant.Constants; import com.zhitan.common.constant.UserConstants; @@ -17,21 +9,38 @@ import com.zhitan.common.core.domain.model.LoginUser; import com.zhitan.common.core.redis.RedisCache; import com.zhitan.common.exception.ServiceException; -import com.zhitan.common.exception.user.BlackListException; -import com.zhitan.common.exception.user.CaptchaException; -import com.zhitan.common.exception.user.CaptchaExpireException; -import com.zhitan.common.exception.user.UserNotExistsException; -import com.zhitan.common.exception.user.UserPasswordNotMatchException; +import com.zhitan.common.exception.user.*; import com.zhitan.common.utils.DateUtils; import com.zhitan.common.utils.MessageUtils; +import com.zhitan.common.utils.SocialUtils; import com.zhitan.common.utils.StringUtils; import com.zhitan.common.utils.ip.IpUtils; +import com.zhitan.framework.manager.AsyncManager; +import com.zhitan.framework.manager.factory.AsyncFactory; +import com.zhitan.framework.security.context.AuthenticationContextHolder; +import com.zhitan.framework.security.single.SingleAuthenticationToken; +import com.zhitan.system.domain.SysSocial; +import com.zhitan.system.domain.bo.SysSocialBo; +import com.zhitan.system.domain.vo.SysSocialVo; import com.zhitan.system.service.ISysConfigService; +import com.zhitan.system.service.ISysSocialService; import com.zhitan.system.service.ISysUserService; +import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.model.AuthResponse; +import me.zhyd.oauth.model.AuthToken; +import me.zhyd.oauth.model.AuthUser; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; /** * 鐧诲綍鏍¢獙鏂规硶 - * + * * @author zhitan */ @Component @@ -45,16 +54,22 @@ @Resource private RedisCache redisCache; - + @Resource private ISysUserService userService; @Resource private ISysConfigService configService; + @Resource + private ISysSocialService sysSocialService; + + @Resource + private SysPermissionService permissionService; + /** * 鐧诲綍楠岃瘉 - * + * * @param username 鐢ㄦ埛鍚� * @param password 瀵嗙爜 * @param code 楠岃瘉鐮� @@ -64,7 +79,7 @@ public String login(String username, String password, String code, String uuid) { // 楠岃瘉鐮佹牎楠� - validateCaptcha(username, code, uuid); +// validateCaptcha(username, code, uuid); // 鐧诲綍鍓嶇疆鏍¢獙 loginPreCheck(username, password); // 鐢ㄦ埛楠岃瘉 @@ -100,9 +115,72 @@ return tokenService.createToken(loginUser); } + + + + + public String loginByCode(String code,String state) + { + AuthKeycloakRequest authRequest = SocialUtils.getAuthKeyloakRequest(); + // AuthToken accessToken = authRequest.getAccessToken(passwordLoginBody); + AuthCallback callback = new AuthCallback(); + callback.setCode(code); + callback.setState(state); + AuthResponse<AuthUser> res = authRequest.login(callback); + AuthUser authUserData = res.getData(); + // 鏂板KEYCLOAK鐢ㄦ埛鑷姩鍒涘缓閫昏緫 + String authId = authUserData.getSource() + authUserData.getUuid(); + List<SysSocial> list = sysSocialService.selectByAuthId(authId); + if (CollUtil.isEmpty(list)) { + // 鑷姩鍒涘缓鏂扮敤鎴� + SysUser newUser = new SysUser(); + newUser.setUserName(authUserData.getUsername()); + newUser.setEmail(authUserData.getEmail()); + newUser.setNickName(authUserData.getNickname()); + newUser.setPassword("Initial123@"); // 鍒濆瀵嗙爜闇�绗﹀悎瀹夊叏绛栫暐 + newUser.setStatus("0"); + + userService.insertUser(newUser); // 鍋囪瀛樺湪鎻掑叆鏂规硶 + + // 鍒涘缓绀句氦缁戝畾璁板綍 + SysSocialBo newSocial = new SysSocialBo(); + newSocial.setUserId(newUser.getUserId()); + newSocial.setUserName(newUser.getUserName()); + newSocial.setAuthId(authId); + newSocial.setSource(authUserData.getSource()); + newSocial.setOpenId(authUserData.getUuid()); + newSocial.setAccessToken(authUserData.getToken().getAccessToken()); + newSocial.setRefreshToken(authUserData.getToken().getRefreshToken()); + newSocial.setIdToken(authUserData.getToken().getIdToken()); + sysSocialService.insertByBo(newSocial); // 闇�纭繚鏈嶅姟鏈夋柊澧炴柟娉� + + // 閲嶆柊鏌ヨ纭繚鏁版嵁鍙敤 + list = sysSocialService.selectByAuthId(authId); + } else { + // 鏇存柊绀句氦缁戝畾璁板綍 + SysSocialBo socialBo = new SysSocialBo(); + socialBo.setId(list.get(0).getId()); + socialBo.setAccessToken(authUserData.getToken().getAccessToken()); + socialBo.setRefreshToken(authUserData.getToken().getRefreshToken()); + socialBo.setIdToken(authUserData.getToken().getIdToken()); + sysSocialService.updateByBo(socialBo); + + } + list = sysSocialService.selectByAuthId(authUserData.getSource() + authUserData.getUuid()); + if (CollUtil.isEmpty(list)) { + throw new ServiceException("浣犺繕娌℃湁缁戝畾绗笁鏂硅处鍙凤紝缁戝畾鍚庢墠鍙互鐧诲綍锛�"); + } + LoginUser loginUser = new LoginUser(); + loginUser.setUser(userService.selectUserById(list.get(0).getUserId())); + loginUser.setUserId(list.get(0).getUserId()); + loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser())); + // 鐢熸垚token + return tokenService.createToken(loginUser); + } + /** * 鏍¢獙楠岃瘉鐮� - * + * * @param username 鐢ㄦ埛鍚� * @param code 楠岃瘉鐮� * @param uuid 鍞竴鏍囪瘑 @@ -178,4 +256,39 @@ sysUser.setLoginDate(DateUtils.getNowDate()); userService.updateUserProfile(sysUser); } + + + /** + * 鐧诲綍楠岃瘉 + * @param username + * @return 缁撴灉 + */ + public String loginNoCode(String username) + { + // 鐢ㄦ埛楠岃瘉 + Authentication authentication = null; + try + { + authentication = authenticationManager + .authenticate(new SingleAuthenticationToken(username)); + } + catch (Exception e) + { + if (e instanceof BadCredentialsException) + { + AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); + throw new UserPasswordNotMatchException(); + } + else + { + AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constants.LOGIN_FAIL, e.getMessage())); + throw new ServiceException(e.getMessage()); + } + } + AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); + LoginUser loginUser = (LoginUser) authentication.getPrincipal(); + recordLoginInfo(loginUser.getUserId()); + // 鐢熸垚token + return tokenService.createToken(loginUser); + } } -- Gitblit v1.9.3