| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.lang.Opt; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import com.baomidou.lock.annotation.Lock4j; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import org.dromara.common.core.constant.CacheConstants; |
| | | import org.dromara.common.core.constant.Constants; |
| | | import org.dromara.common.core.constant.SystemConstants; |
| | | import org.dromara.common.core.constant.TenantConstants; |
| | | import org.dromara.common.core.domain.dto.PostDTO; |
| | | import org.dromara.common.core.domain.dto.RoleDTO; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.enums.LoginType; |
| | | import org.dromara.common.core.enums.TenantStatus; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.exception.user.UserException; |
| | | import org.dromara.common.core.utils.*; |
| | |
| | | @Service |
| | | public class SysLoginService { |
| | | |
| | | @Value("${justauth.type.keycloak.server-url}") |
| | | private String keycloakServerUrl; |
| | | |
| | | @Value("${justauth.type.keycloak.realm}") |
| | | private String keycloakRealm; |
| | | |
| | | @Value("${user.password.maxRetryCount}") |
| | | private Integer maxRetryCount; |
| | | |
| | |
| | | private final ISysSocialService sysSocialService; |
| | | private final ISysRoleService roleService; |
| | | private final ISysDeptService deptService; |
| | | private final ISysPostService postService; |
| | | private final SysUserMapper userMapper; |
| | | |
| | | |
| | |
| | | TenantHelper.clearDynamic(); |
| | | } |
| | | recordLogininfor(loginUser.getTenantId(), loginUser.getUsername(), Constants.LOGOUT, MessageUtils.message("user.logout.success")); |
| | | // 新增Keycloak登出逻辑 |
| | | |
| | | Long userId = loginUser.getUserId(); |
| | | |
| | | SysSocialVo social = sysSocialService.selectByUserId(userId); |
| | | if (social == null) { |
| | | return; |
| | | } |
| | | |
| | | String logoutUrl = keycloakServerUrl + "/realms/" + keycloakRealm + "/protocol/openid-connect/logout"; |
| | | HttpRequest request = HttpRequest.get(logoutUrl) |
| | | .form("refresh_token", social.getRefreshToken()) |
| | | .form("id_token_hint", social.getIdToken()); |
| | | |
| | | |
| | | HttpResponse response = request.execute(); |
| | | if (response.isOk()) { |
| | | System.out.println("1234"); |
| | | } |
| | | |
| | | } catch (NotLoginException ignored) { |
| | | } finally { |
| | | try { |
| | |
| | | */ |
| | | public LoginUser buildLoginUser(SysUserVo user) { |
| | | LoginUser loginUser = new LoginUser(); |
| | | Long userId = user.getUserId(); |
| | | loginUser.setTenantId(user.getTenantId()); |
| | | loginUser.setUserId(user.getUserId()); |
| | | loginUser.setUserId(userId); |
| | | loginUser.setDeptId(user.getDeptId()); |
| | | loginUser.setUsername(user.getUserName()); |
| | | loginUser.setNickname(user.getNickName()); |
| | | loginUser.setUserType(user.getUserType()); |
| | | loginUser.setMenuPermission(permissionService.getMenuPermission(user.getUserId())); |
| | | loginUser.setRolePermission(permissionService.getRolePermission(user.getUserId())); |
| | | Opt<SysDeptVo> deptOpt = Opt.of(user.getDeptId()).map(deptService::selectDeptById); |
| | | loginUser.setDeptName(deptOpt.map(SysDeptVo::getDeptName).orElse(StringUtils.EMPTY)); |
| | | loginUser.setDeptCategory(deptOpt.map(SysDeptVo::getDeptCategory).orElse(StringUtils.EMPTY)); |
| | | List<SysRoleVo> roles = roleService.selectRolesByUserId(user.getUserId()); |
| | | loginUser.setMenuPermission(permissionService.getMenuPermission(userId)); |
| | | loginUser.setRolePermission(permissionService.getRolePermission(userId)); |
| | | if (ObjectUtil.isNotNull(user.getDeptId())) { |
| | | Opt<SysDeptVo> deptOpt = Opt.of(user.getDeptId()).map(deptService::selectDeptById); |
| | | loginUser.setDeptName(deptOpt.map(SysDeptVo::getDeptName).orElse(StringUtils.EMPTY)); |
| | | loginUser.setDeptCategory(deptOpt.map(SysDeptVo::getDeptCategory).orElse(StringUtils.EMPTY)); |
| | | } |
| | | List<SysRoleVo> roles = roleService.selectRolesByUserId(userId); |
| | | List<SysPostVo> posts = postService.selectPostsByUserId(userId); |
| | | loginUser.setRoles(BeanUtil.copyToList(roles, RoleDTO.class)); |
| | | loginUser.setPosts(BeanUtil.copyToList(posts, PostDTO.class)); |
| | | return loginUser; |
| | | } |
| | | |
| | |
| | | if (!TenantHelper.isEnable()) { |
| | | return; |
| | | } |
| | | if (TenantConstants.DEFAULT_TENANT_ID.equals(tenantId)) { |
| | | return; |
| | | } |
| | | if (StringUtils.isBlank(tenantId)) { |
| | | throw new TenantException("tenant.number.not.blank"); |
| | | } |
| | | if (TenantConstants.DEFAULT_TENANT_ID.equals(tenantId)) { |
| | | return; |
| | | } |
| | | SysTenantVo tenant = tenantService.queryByTenantId(tenantId); |
| | | if (ObjectUtil.isNull(tenant)) { |
| | | log.info("登录租户:{} 不存在.", tenantId); |
| | | throw new TenantException("tenant.not.exists"); |
| | | } else if (TenantStatus.DISABLE.getCode().equals(tenant.getStatus())) { |
| | | } else if (SystemConstants.DISABLE.equals(tenant.getStatus())) { |
| | | log.info("登录租户:{} 已被停用.", tenantId); |
| | | throw new TenantException("tenant.blocked"); |
| | | } else if (ObjectUtil.isNotNull(tenant.getExpireTime()) |