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;
|
}
|
}
|