| package com.ruoyi.integration.iotda.utils.token; | 
|   | 
| import com.huaweicloud.sdk.core.exception.ConnectionException; | 
| import com.huaweicloud.sdk.core.exception.RequestTimeoutException; | 
| import com.huaweicloud.sdk.core.exception.ServiceResponseException; | 
| import com.huaweicloud.sdk.iam.v3.model.*; | 
| import com.ruoyi.common.redis.service.RedisService; | 
| import com.ruoyi.integration.iotda.constant.IotConstant; | 
| import com.ruoyi.integration.iotda.builder.IotBuilder; | 
| import com.ruoyi.integration.iotda.config.IotAccountConfig; | 
| import com.ruoyi.integration.iotda.utils.time.UtcToSeconds; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.util.ArrayList; | 
| import java.util.List; | 
| import java.util.concurrent.TimeUnit; | 
|   | 
| @Slf4j | 
| @Component | 
| public class IotTokenUtil { | 
|     @Autowired | 
|     private IotBuilder iotBuilder; | 
|     @Autowired | 
|     private IotAccountConfig accountConfig; | 
|     @Autowired | 
|     private RedisService redisService; | 
|   | 
|     /** | 
|      * 获取token | 
|      * @return | 
|      */ | 
|     public KeystoneCreateUserTokenByPasswordResponse getToken() { | 
|         KeystoneCreateUserTokenByPasswordRequest request = new KeystoneCreateUserTokenByPasswordRequest(); | 
|         KeystoneCreateUserTokenByPasswordRequestBody body = new KeystoneCreateUserTokenByPasswordRequestBody(); | 
|         PwdPasswordUserDomain domainUser = new PwdPasswordUserDomain(); | 
|         domainUser.withName(accountConfig.getHostAccount()); | 
|         PwdPasswordUser userPassword = new PwdPasswordUser(); | 
|         userPassword.withDomain(domainUser) | 
|                 .withName(accountConfig.getIamAccount()) | 
|                 .withPassword(accountConfig.getIamPassword()); | 
|         PwdPassword passwordIdentity = new PwdPassword(); | 
|         passwordIdentity.withUser(userPassword); | 
|         List<PwdIdentity.MethodsEnum> listIdentityMethods = new ArrayList<>(); | 
|         listIdentityMethods.add(PwdIdentity.MethodsEnum.fromValue(IotConstant.PASSWORD)); | 
|         PwdIdentity identityAuth = new PwdIdentity(); | 
|         identityAuth.withMethods(listIdentityMethods) | 
|                 .withPassword(passwordIdentity); | 
|         PwdAuth authbody = new PwdAuth(); | 
|         authbody.withIdentity(identityAuth); | 
|         body.withAuth(authbody); | 
|         request.withBody(body); | 
|         try { | 
|             KeystoneCreateUserTokenByPasswordResponse response = iotBuilder.buildIam().keystoneCreateUserTokenByPassword(request); | 
|             redisService.setCacheObject(IotConstant.IOT_TOKEN, response.getXSubjectToken(), UtcToSeconds.convertToSeconds(response.getToken().getExpiresAt()), TimeUnit.SECONDS); | 
|             return response; | 
|         } catch (ConnectionException e) { | 
|             e.printStackTrace(); | 
|         } catch (RequestTimeoutException e) { | 
|             e.printStackTrace(); | 
|         } catch (ServiceResponseException e) { | 
|             e.printStackTrace(); | 
|             System.out.println(e.getHttpStatusCode()); | 
|             System.out.println(e.getRequestId()); | 
|             System.out.println(e.getErrorCode()); | 
|             System.out.println(e.getErrorMsg()); | 
|         } | 
|         return null; | 
|     } | 
|   | 
| } |