|
|
@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.dji.sample.common.model.CustomClaim;
|
|
|
+import com.dji.sample.common.util.DesUtil;
|
|
|
import com.dji.sample.common.util.JwtUtil;
|
|
|
import com.dji.sample.component.mqtt.config.MqttPropertyConfiguration;
|
|
|
import com.dji.sample.manage.dao.IUserMapper;
|
|
|
@@ -23,12 +24,14 @@ import com.dji.sample.manage.service.IWorkspaceService;
|
|
|
import com.dji.sdk.common.HttpResultResponse;
|
|
|
import com.dji.sdk.common.Pagination;
|
|
|
import com.dji.sdk.common.PaginationData;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
@@ -82,6 +85,10 @@ public class UserServiceImpl implements IUserService {
|
|
|
if (flag.intValue() != userEntity.getUserType().intValue()) {
|
|
|
return HttpResultResponse.error("The account type does not match.");
|
|
|
}
|
|
|
+
|
|
|
+ //密码加密
|
|
|
+ password = DesUtil.getEncryptData(password);
|
|
|
+
|
|
|
if (!password.equals(userEntity.getPassword())) {
|
|
|
return new HttpResultResponse()
|
|
|
.setCode(HttpStatus.UNAUTHORIZED.value())
|
|
|
@@ -123,32 +130,28 @@ public class UserServiceImpl implements IUserService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public HttpResultResponse getToken(String username, String password) {
|
|
|
- UserEntity userEntity = this.getUserByUsername(username);
|
|
|
+ public HttpResultResponse getToken(String clientId, String username, String password) {
|
|
|
+ UserEntity userEntity = this.getClientUser(clientId);
|
|
|
if (userEntity == null) {
|
|
|
return new HttpResultResponse()
|
|
|
.setCode(HttpStatus.UNAUTHORIZED.value())
|
|
|
- .setMessage("invalid username");
|
|
|
+ .setMessage("clientId不存在");
|
|
|
}
|
|
|
- if (UserTypeEnum.API.getVal() != userEntity.getUserType().intValue()) {
|
|
|
- return HttpResultResponse.error("The account type does not match.");
|
|
|
- }
|
|
|
- if (!password.equals(userEntity.getPassword())) {
|
|
|
- return new HttpResultResponse()
|
|
|
- .setCode(HttpStatus.UNAUTHORIZED.value())
|
|
|
- .setMessage("invalid password");
|
|
|
+ if (UserTypeEnum.CLIENT.getVal() != userEntity.getUserType().intValue()) {
|
|
|
+ return HttpResultResponse.error("用户类型不匹配");
|
|
|
}
|
|
|
|
|
|
- Optional<WorkspaceDTO> workspaceOpt = workspaceService.getWorkspaceByWorkspaceId(userEntity.getWorkspaceId());
|
|
|
- if (workspaceOpt.isEmpty()) {
|
|
|
+ //密码加密
|
|
|
+ String encryptPsw = DesUtil.getEncryptData(password);
|
|
|
+ if (!encryptPsw.equals(userEntity.getPassword()) || !username.equals(userEntity.getUsername())) {
|
|
|
return new HttpResultResponse()
|
|
|
.setCode(HttpStatus.UNAUTHORIZED.value())
|
|
|
- .setMessage("invalid workspace id:" + userEntity.getWorkspaceId());
|
|
|
+ .setMessage("用户名或密码不匹配");
|
|
|
}
|
|
|
|
|
|
CustomClaim customClaim = new CustomClaim(userEntity.getUserId(),
|
|
|
userEntity.getUsername(), userEntity.getUserType(),
|
|
|
- workspaceOpt.get().getWorkspaceId());
|
|
|
+ "");
|
|
|
|
|
|
// create token
|
|
|
String token = JwtUtil.createToken(customClaim.convertToMap());
|
|
|
@@ -196,23 +199,57 @@ public class UserServiceImpl implements IUserService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Boolean saveApiUser(String username,String password) {
|
|
|
+ public HttpResultResponse saveApiUser(String clientId, String username, String password) {
|
|
|
+ if(!StringUtils.hasText(clientId)) {
|
|
|
+ return new HttpResultResponse()
|
|
|
+ .setCode(HttpStatus.UNAUTHORIZED.value())
|
|
|
+ .setMessage("clientId不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ UserEntity userEntity = this.getClientUser(clientId);
|
|
|
+ if (userEntity == null) {
|
|
|
+ return new HttpResultResponse()
|
|
|
+ .setCode(HttpStatus.UNAUTHORIZED.value())
|
|
|
+ .setMessage("clientId不存在");
|
|
|
+ }
|
|
|
+
|
|
|
if(!StringUtils.hasText(username)) {
|
|
|
- throw new RuntimeException("用户名不能为空!");
|
|
|
+ return new HttpResultResponse()
|
|
|
+ .setCode(HttpStatus.BAD_REQUEST.value())
|
|
|
+ .setMessage("用户名不能为空");
|
|
|
}
|
|
|
|
|
|
if(!StringUtils.hasText(password)) {
|
|
|
- throw new RuntimeException("密码不能为空!");
|
|
|
+ return new HttpResultResponse()
|
|
|
+ .setCode(HttpStatus.BAD_REQUEST.value())
|
|
|
+ .setMessage("密码不能为空");
|
|
|
}
|
|
|
+ //密码加密
|
|
|
+ String encryptPsw = DesUtil.getEncryptData(password);
|
|
|
+
|
|
|
long curTime = System.currentTimeMillis();
|
|
|
- return mapper.insert(UserEntity.builder().
|
|
|
+ userEntity = UserEntity.builder().
|
|
|
userId(UUID.randomUUID().toString())
|
|
|
.username(username)
|
|
|
- .password(password)
|
|
|
+ .password(encryptPsw)
|
|
|
.userType(UserTypeEnum.API.getVal())
|
|
|
.createTime(curTime)
|
|
|
.updateTime(curTime)
|
|
|
- .build()) > 0;
|
|
|
+ .clientId(clientId)
|
|
|
+ .build();
|
|
|
+ UserEntity apiUser = getApiUser(clientId);
|
|
|
+ if(apiUser == null) {
|
|
|
+ int cnt = mapper.insert(userEntity);
|
|
|
+ return cnt > 0 ? HttpResultResponse.success():HttpResultResponse.error("添加失败");
|
|
|
+ } else {
|
|
|
+ userEntity.setUsername(username);
|
|
|
+ userEntity.setPassword(encryptPsw);
|
|
|
+ userEntity.setUpdateTime(System.currentTimeMillis());
|
|
|
+ int cnt = mapper.update(userEntity, new LambdaUpdateWrapper<UserEntity>()
|
|
|
+ .eq(UserEntity::getClientId, clientId)
|
|
|
+ .eq(UserEntity::getUserId, userEntity.getUserId()));
|
|
|
+ return cnt > 0 ? HttpResultResponse.success():HttpResultResponse.error("更新失败");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -282,6 +319,18 @@ public class UserServiceImpl implements IUserService {
|
|
|
.eq("username", username));
|
|
|
}
|
|
|
|
|
|
+ private UserEntity getApiUser(String clientId) {
|
|
|
+ return mapper.selectOne(new QueryWrapper<UserEntity>()
|
|
|
+ .eq("client_id", clientId)
|
|
|
+ .eq("user_type",UserTypeEnum.API.getVal()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserEntity getClientUser(String clientId) {
|
|
|
+ return mapper.selectOne(new QueryWrapper<UserEntity>()
|
|
|
+ .eq("client_id", clientId)
|
|
|
+ .eq("user_type",UserTypeEnum.CLIENT.getVal()));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Convert database entity objects into user data transfer object.
|
|
|
* @param entity
|