无关风月
2024-08-21 10de85f85ad0874b2d93c851bb4d3eacab24a183
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
    }
 
}