fix:获取userId修改
parent
4a216306a2
commit
2e8dca3175
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据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>
|
<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>
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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();
|
||||||
// 根据域名进行筛选
|
// 根据域名进行筛选
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
||||||
|
|
7
pom.xml
7
pom.xml
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue