From 2e8dca31752df103f4480bb6ea31475888a8290f Mon Sep 17 00:00:00 2001 From: xiwa Date: Thu, 8 Jun 2023 08:08:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=8E=B7=E5=8F=96userId=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cc/iotkit/common/undefined/LoginUser.java | 23 +++++++++++++++++++ iot-common/iot-common-satoken/pom.xml | 5 ++++ .../common/satoken/utils/LoginHelper.java | 14 +++++------ .../web/handler/ResponseResultHandler.java | 8 +------ .../iotkit/web/controller/AuthController.java | 8 +++++-- .../cc/iotkit/web/domain/vo/TenantListVo.java | 6 +++++ pom.xml | 7 ++++++ 7 files changed, 54 insertions(+), 17 deletions(-) diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/undefined/LoginUser.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/undefined/LoginUser.java index 3e167e38..d93e713d 100644 --- a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/undefined/LoginUser.java +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/undefined/LoginUser.java @@ -1,5 +1,6 @@ package cc.iotkit.common.undefined; +import cc.iotkit.common.utils.StringUtils; import lombok.Data; import lombok.NoArgsConstructor; @@ -117,4 +118,26 @@ public class LoginUser implements Serializable { return userType + ":" + userId; } + /** + * 根据loginId构造loginUser对象 + * + * @param loginId 登录id + * @return LoginUser + * @see LoginUser::getLoginId + */ + public static LoginUser from(String loginId) { + if (StringUtils.isBlank(loginId)) { + return null; + } + String[] split = loginId.split(":"); + if (split.length < 2) { + return null; + } + + LoginUser user = new LoginUser(); + user.setUserType(split[0]); + user.setUserId(Long.parseLong(split[1])); + return user; + } + } diff --git a/iot-common/iot-common-satoken/pom.xml b/iot-common/iot-common-satoken/pom.xml index 2e10d678..22600dc9 100644 --- a/iot-common/iot-common-satoken/pom.xml +++ b/iot-common/iot-common-satoken/pom.xml @@ -19,6 +19,11 @@ sa-token-spring-boot-starter + + cn.dev33 + sa-token-dao-redis-jackson + + cn.dev33 diff --git a/iot-common/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/utils/LoginHelper.java b/iot-common/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/utils/LoginHelper.java index 49392f2c..bea9fbbe 100644 --- a/iot-common/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/utils/LoginHelper.java +++ b/iot-common/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/utils/LoginHelper.java @@ -60,8 +60,8 @@ public class LoginHelper { model.setDevice(deviceType.getDevice()); } StpUtil.login(loginUser.getLoginId(), - model.setExtra(TENANT_KEY, loginUser.getTenantId()) - .setExtra(USER_KEY, loginUser.getUserId())); + model.setExtra(TENANT_KEY, loginUser.getTenantId()) + .setExtra(USER_KEY, loginUser.getUserId())); StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser); } @@ -89,17 +89,15 @@ public class LoginHelper { * 获取用户id */ public static Long getUserId() { - Long userId; try { - userId = Convert.toLong(SaHolder.getStorage().get(USER_KEY)); - if (ObjectUtil.isNull(userId)) { - userId = Convert.toLong(StpUtil.getExtra(USER_KEY)); - SaHolder.getStorage().set(USER_KEY, userId); + LoginUser user = LoginUser.from(StpUtil.getLoginIdAsString()); + if (user == null) { + return null; } + return user.getUserId(); } catch (Exception e) { return null; } - return userId; } /** diff --git a/iot-common/iot-common-web/src/main/java/cc/iotkit/common/web/handler/ResponseResultHandler.java b/iot-common/iot-common-web/src/main/java/cc/iotkit/common/web/handler/ResponseResultHandler.java index b943fa38..09e3e8f8 100644 --- a/iot-common/iot-common-web/src/main/java/cc/iotkit/common/web/handler/ResponseResultHandler.java +++ b/iot-common/iot-common-web/src/main/java/cc/iotkit/common/web/handler/ResponseResultHandler.java @@ -10,7 +10,6 @@ package cc.iotkit.common.web.handler; import cc.iotkit.common.api.Response; - import cn.dev33.satoken.util.SaResult; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; @@ -18,20 +17,15 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; -import javax.servlet.http.HttpServletRequest; import java.util.Map; @ControllerAdvice public class ResponseResultHandler implements ResponseBodyAdvice { @Override public boolean supports(MethodParameter returnType, Class> converterType) { - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); - String wrapResponse = request.getHeader("wrap-response"); - return "json".equals(wrapResponse); + return true; } @Override diff --git a/iot-starter/src/main/java/cc/iotkit/web/controller/AuthController.java b/iot-starter/src/main/java/cc/iotkit/web/controller/AuthController.java index ec6f048a..cf06426b 100644 --- a/iot-starter/src/main/java/cc/iotkit/web/controller/AuthController.java +++ b/iot-starter/src/main/java/cc/iotkit/web/controller/AuthController.java @@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.net.URL; import java.util.List; +import java.util.stream.Collectors; /** * 认证 @@ -138,7 +139,6 @@ public class AuthController { throw new BizException("当前租户不允许注册"); } registerService.register(user); - } /** @@ -150,7 +150,11 @@ public class AuthController { @GetMapping("/tenant/list") public LoginTenantVo tenantList(HttpServletRequest request) throws Exception { List tenantList = tenantService.queryList(new SysTenantBo()); - List voList = MapstructUtils.convert(tenantList, TenantListVo.class); + List voList = tenantList.stream().map(t -> TenantListVo.builder() + .tenantId(t.getTenantId()) + .companyName(t.getCompanyName()) + .domain(t.getDomain()) + .build()).collect(Collectors.toList()); // 获取域名 String host = new URL(request.getRequestURL().toString()).getHost(); // 根据域名进行筛选 diff --git a/iot-starter/src/main/java/cc/iotkit/web/domain/vo/TenantListVo.java b/iot-starter/src/main/java/cc/iotkit/web/domain/vo/TenantListVo.java index 8ce2d679..e769296d 100644 --- a/iot-starter/src/main/java/cc/iotkit/web/domain/vo/TenantListVo.java +++ b/iot-starter/src/main/java/cc/iotkit/web/domain/vo/TenantListVo.java @@ -2,7 +2,10 @@ package cc.iotkit.web.domain.vo; import cc.iotkit.system.dto.vo.SysTenantVo; import io.github.linpeilie.annotations.AutoMapper; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; /** @@ -11,6 +14,9 @@ import lombok.Data; * @author Lion Li */ @Data +@NoArgsConstructor +@AllArgsConstructor +@Builder @AutoMapper(target = SysTenantVo.class) public class TenantListVo { diff --git a/pom.xml b/pom.xml index 715da538..bd2f99dd 100755 --- a/pom.xml +++ b/pom.xml @@ -142,6 +142,13 @@ sa-token-spring-boot-starter ${satoken.version} + + + cn.dev33 + sa-token-dao-redis-jackson + ${satoken.version} + + cn.dev33