fix:获取userId修改

V0.5.x
xiwa 2023-06-08 08:08:03 +08:00
parent 4a216306a2
commit 2e8dca3175
7 changed files with 54 additions and 17 deletions

View File

@ -1,5 +1,6 @@
package cc.iotkit.common.undefined; package cc.iotkit.common.undefined;
import cc.iotkit.common.utils.StringUtils;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -117,4 +118,26 @@ public class LoginUser implements Serializable {
return userType + ":" + userId; return userType + ":" + userId;
} }
/**
* loginIdloginUser
*
* @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;
}
} }

View File

@ -19,6 +19,11 @@
<artifactId>sa-token-spring-boot-starter</artifactId> <artifactId>sa-token-spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId>
</dependency>
<!-- Sa-Token 整合 jwt --> <!-- Sa-Token 整合 jwt -->
<dependency> <dependency>
<groupId>cn.dev33</groupId> <groupId>cn.dev33</groupId>

View File

@ -60,8 +60,8 @@ public class LoginHelper {
model.setDevice(deviceType.getDevice()); model.setDevice(deviceType.getDevice());
} }
StpUtil.login(loginUser.getLoginId(), StpUtil.login(loginUser.getLoginId(),
model.setExtra(TENANT_KEY, loginUser.getTenantId()) model.setExtra(TENANT_KEY, loginUser.getTenantId())
.setExtra(USER_KEY, loginUser.getUserId())); .setExtra(USER_KEY, loginUser.getUserId()));
StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser); StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
} }
@ -89,17 +89,15 @@ public class LoginHelper {
* id * id
*/ */
public static Long getUserId() { public static Long getUserId() {
Long userId;
try { try {
userId = Convert.toLong(SaHolder.getStorage().get(USER_KEY)); LoginUser user = LoginUser.from(StpUtil.getLoginIdAsString());
if (ObjectUtil.isNull(userId)) { if (user == null) {
userId = Convert.toLong(StpUtil.getExtra(USER_KEY)); return null;
SaHolder.getStorage().set(USER_KEY, userId);
} }
return user.getUserId();
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
return userId;
} }
/** /**

View File

@ -10,7 +10,6 @@
package cc.iotkit.common.web.handler; package cc.iotkit.common.web.handler;
import cc.iotkit.common.api.Response; import cc.iotkit.common.api.Response;
import cn.dev33.satoken.util.SaResult; import cn.dev33.satoken.util.SaResult;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType; 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.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse; import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice; 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 org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.Map; import java.util.Map;
@ControllerAdvice @ControllerAdvice
public class ResponseResultHandler implements ResponseBodyAdvice<Object> { public class ResponseResultHandler implements ResponseBodyAdvice<Object> {
@Override @Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); return true;
String wrapResponse = request.getHeader("wrap-response");
return "json".equals(wrapResponse);
} }
@Override @Override

View File

@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* *
@ -138,7 +139,6 @@ public class AuthController {
throw new BizException("当前租户不允许注册"); throw new BizException("当前租户不允许注册");
} }
registerService.register(user); registerService.register(user);
} }
/** /**
@ -150,7 +150,11 @@ public class AuthController {
@GetMapping("/tenant/list") @GetMapping("/tenant/list")
public LoginTenantVo tenantList(HttpServletRequest request) throws Exception { public LoginTenantVo tenantList(HttpServletRequest request) throws Exception {
List<SysTenantVo> tenantList = tenantService.queryList(new SysTenantBo()); List<SysTenantVo> tenantList = tenantService.queryList(new SysTenantBo());
List<TenantListVo> voList = MapstructUtils.convert(tenantList, TenantListVo.class); List<TenantListVo> 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(); String host = new URL(request.getRequestURL().toString()).getHost();
// 根据域名进行筛选 // 根据域名进行筛选

View File

@ -2,7 +2,10 @@ package cc.iotkit.web.domain.vo;
import cc.iotkit.system.dto.vo.SysTenantVo; import cc.iotkit.system.dto.vo.SysTenantVo;
import io.github.linpeilie.annotations.AutoMapper; import io.github.linpeilie.annotations.AutoMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
@ -11,6 +14,9 @@ import lombok.Data;
* @author Lion Li * @author Lion Li
*/ */
@Data @Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@AutoMapper(target = SysTenantVo.class) @AutoMapper(target = SysTenantVo.class)
public class TenantListVo { public class TenantListVo {

View File

@ -142,6 +142,13 @@
<artifactId>sa-token-spring-boot-starter</artifactId> <artifactId>sa-token-spring-boot-starter</artifactId>
<version>${satoken.version}</version> <version>${satoken.version}</version>
</dependency> </dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId>
<version>${satoken.version}</version>
</dependency>
<!-- Sa-Token 整合 jwt --> <!-- Sa-Token 整合 jwt -->
<dependency> <dependency>
<groupId>cn.dev33</groupId> <groupId>cn.dev33</groupId>