leutu
2024-05-20 a4e99a6a39284643fdf2b04dbe55212e5954973c
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
73
74
75
package com.skyline.electricity.interceptor;
 
import com.skyline.electricity.controller.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.client.*;
import com.skyline.electricity.utils.*;
import org.springframework.util.*;
import com.alibaba.fastjson.*;
import org.springframework.http.*;
import org.apache.commons.lang3.*;
 
public class IAMUtils
{
    private static final String PREFIX = "bearer ";
    @Autowired
    private RemoteController remoteController;
 
    public Object getUserInfoFromIam(final String token) {
        final RestTemplate restTemplate = new RestTemplate();
        final String url = PropertiesUtil.getProperty("userInfo_url");
        String result = null;
        final HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "bearer " + token);
        final HttpEntity httpEntity = new HttpEntity((Object)null, (MultiValueMap)headers);
        final ResponseEntity<String> responseEntity = (ResponseEntity<String>)restTemplate.exchange(url, HttpMethod.GET, httpEntity, (Class)String.class, new Object[0]);
        if (responseEntity.getStatusCode().value() == 200) {
            result = (String)responseEntity.getBody();
            //System.out.println(result);
        }
        if ("".equals(result)) {
            return null;
        }
        final JSONObject resultJson = JSONObject.parseObject(result);
        if (resultJson.containsKey((Object)"error")) {
            return null;
        }
        return resultJson.getJSONObject("data");
    }
 
    public String getTokenByCode(final String code) {
        if (StringUtils.isBlank((CharSequence)code)) {
            return null;
        }
        String url = PropertiesUtil.getProperty("token_url");
        String result = null;
        try {
            url = url + "?code=" + code;
            final RestTemplate restTemplate = new RestTemplate();
            final ResponseEntity<String> responseEntity = (ResponseEntity<String>)restTemplate.getForEntity(url, (Class)String.class, new Object[0]);
            if (responseEntity.getStatusCode().value() == 200) {
                result = (String)responseEntity.getBody();
                //System.out.println(result);
            }
            if (StringUtils.isBlank((CharSequence)result)) {
                return null;
            }
            final JSONObject resultJson = JSONObject.parseObject(result);
            if (resultJson.containsKey((Object)"error")) {
                //System.out.println(resultJson.toJSONString());
                return null;
            }
            return resultJson.getString("data");
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public String getInfo() {
        //System.out.println(PropertiesUtil.getProperty("userInfo_url") + PropertiesUtil.getProperty("token_url"));
        return PropertiesUtil.getProperty("userInfo_url") + PropertiesUtil.getProperty("token_url");
    }
}