liuxm
2024-05-28 3a24dda899fcde40e2edb90c319eb5644805a639
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
package com.dy.pmsGlobal.util;
 
import com.alibaba.excel.util.StringUtils;
import com.dy.pmsGlobal.pojoBa.BaUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
@Slf4j
@Component
public class UserUtil {
    @Value("${pms.sso.curUserUrl}")
    public String SsoCurUserUrl ;
 
    private RestTemplate restTemplate;
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate){
        this.restTemplate = restTemplate ;
    }
 
    public BaUser getUser(String token) {
        if(StringUtils.isBlank(token) || StringUtils.isBlank(SsoCurUserUrl)){
            return null;
        }
        try{
            String url = UriComponentsBuilder.fromUriString(SsoCurUserUrl)
                    .queryParam("token", token)
                    .build()
                    .toUriString();
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<?> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<BaUser> myResponse = null;
            try {
                // 通过Get方式调用接口
                myResponse = restTemplate.exchange(url, HttpMethod.GET, httpEntity, BaUser.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            assert myResponse != null;
            return myResponse.getBody();
        }catch (Exception e){
        }
        return null;
    }
}