| | |
| | | package com.dji.sample.manage.service.impl; |
| | | |
| | | import com.auth0.jwt.JWT; |
| | | import com.auth0.jwt.exceptions.TokenExpiredException; |
| | | import com.auth0.jwt.interfaces.DecodedJWT; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.Optional; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | String token = JwtUtil.createToken(customClaim.convertToMap()); |
| | | |
| | | UserDTO userDTO = entityConvertToDTO(userEntity); |
| | | userDTO.setMqttAddr(new StringBuilder() |
| | | .append(mqttConfiguration.getProtocol().trim()) |
| | | .append("://") |
| | | .append(mqttConfiguration.getHost().trim()) |
| | | .append(":") |
| | | .append(mqttConfiguration.getPort()) |
| | | .toString()); |
| | | userDTO.setMqttAddr(MqttConfiguration.getBasicMqttAddress()); |
| | | userDTO.setAccessToken(token); |
| | | userDTO.setWorkspaceId(workspaceOpt.get().getWorkspaceId()); |
| | | return ResponseResult.success(userDTO); |
| | |
| | | if (!StringUtils.hasText(token)) { |
| | | return Optional.empty(); |
| | | } |
| | | CustomClaim customClaim = new CustomClaim(JWT.decode(token).getClaims()); |
| | | CustomClaim customClaim; |
| | | try { |
| | | DecodedJWT jwt = JwtUtil.verifyToken(token); |
| | | customClaim = new CustomClaim(jwt.getClaims()); |
| | | } catch (TokenExpiredException e) { |
| | | customClaim = new CustomClaim(JWT.decode(token).getClaims()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return Optional.empty(); |
| | | } |
| | | String refreshToken = JwtUtil.createToken(customClaim.convertToMap()); |
| | | |
| | | UserDTO user = entityConvertToDTO(this.getUserByUsername(customClaim.getUsername())); |
| | | if (Objects.isNull(user)) { |
| | | return Optional.empty(); |
| | | } |
| | | user.setWorkspaceId(customClaim.getWorkspaceId()); |
| | | user.setAccessToken(refreshToken); |
| | | return Optional.of(user); |
| | |
| | | */ |
| | | private UserDTO entityConvertToDTO(UserEntity entity) { |
| | | if (entity == null) { |
| | | return new UserDTO(); |
| | | return null; |
| | | } |
| | | return UserDTO.builder() |
| | | .userId(entity.getUserId()) |
| | |
| | | .userType(entity.getUserType()) |
| | | .mqttUsername(entity.getMqttUsername()) |
| | | .mqttPassword(entity.getMqttPassword()) |
| | | .mqttAddr(new StringBuilder() |
| | | .append(mqttConfiguration.getProtocol().trim()) |
| | | .append("://") |
| | | .append(mqttConfiguration.getHost().trim()) |
| | | .append(":") |
| | | .append(mqttConfiguration.getPort()) |
| | | .toString()) |
| | | .mqttAddr(MqttConfiguration.getBasicMqttAddress()) |
| | | .build(); |
| | | } |
| | | } |