fix:获取userId修改
parent
4a216306a2
commit
2e8dca3175
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,6 +19,11 @@
|
|||
<artifactId>sa-token-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-dao-redis-jackson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sa-Token 整合 jwt -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<Object> {
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
|
||||
String wrapResponse = request.getHeader("wrap-response");
|
||||
return "json".equals(wrapResponse);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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<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();
|
||||
// 根据域名进行筛选
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
7
pom.xml
7
pom.xml
|
@ -142,6 +142,13 @@
|
|||
<artifactId>sa-token-spring-boot-starter</artifactId>
|
||||
<version>${satoken.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-dao-redis-jackson</artifactId>
|
||||
<version>${satoken.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Sa-Token 整合 jwt -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
|
|
Loading…
Reference in New Issue