需求:BIM大屏需要进行展示物料网站的一些数据,但是如果每次都让物料给你提供接口进行调用的话,太过繁琐,所以现在的需求就是
-
拿到物料网站的system/jwtLogin接口,也就是登录接口,输入账号密码的时候回显token。
-
刚开始想的是直接后端设置为明文的进行密码的同意加密然后进行调用接口返回,后来总监说这个跟直接加密的没啥区别,所以这块直接密码设置为加密模式。
3.当拿到token的时候根据接口的地址、参数、token进行访问物料接口进行返回数据
/**
* @Author gao_y
* @Description //根据物料平台的账号密码进行登录获取到Token然后进行请求物料系统的接口
**/
public class WlLoginUtils {
//账号
private static final String USER_NAME_ID = "账号";
//密码
private static final String PASSWORD = "密码";
//获取token地址
private static final String GET_TOKEN_URL = "http://www.baidu.com/system/jwtLogin";
static Logger logger = LoggerFactory.getLogger(WlLoginUtils.class);
/**
* 访问物料系统的获取Token接口来获取Token
* @return token
* @throws Exception
*/
public static Object parsePassWord() throws Exception {
JSONObject jsonParam = new JSONObject();
jsonParam.put("userNameId", USER_NAME_ID);
jsonParam.put("password", PASSWORD);
String doPosts = HttpClientUtil.doPostJson(GET_TOKEN_URL, String.valueOf(jsonParam));
JSONObject jsonObject = JSON.parseObject(doPosts);
return jsonObject.getString("message");
}
/**
* @Author gao_y
* @Description //根据传入的JSON格式的参数,然后访问第三方接口进行调用回显
**/
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
/**
* POST---有参(普通参数 + 对象参数)
* query参数放入url中处理即可
* 可以携带token进行访问第三方接口
*
* @date
*/
public static String doPosts(String url, String token, Map<String, String> params) {
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
// 创建json参数
JSONObject jsonObject = new JSONObject();
for (String key : params.keySet()) {
jsonObject.put(key, params.get(key));
}
// 模拟表单
//UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
// 将user对象转换为json字符串,并放入entity中
StringEntity entity = new StringEntity(jsonObject.toJSONString(), ContentType.APPLICATION_JSON);
// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
//因为这边获取的token前面自带了bearer;所以需要去掉进。
//也有的必须要带,我们的系统是不带然后进行传递的
String substring = token.substring(0, token.indexOf(";"));
String tokens = token.substring(substring.length() + 1);
httpPost.setHeader("Token", tokens);
// 响应模型
CloseableHttpResponse response = null;
String result = null;
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
//System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
result = EntityUtils.toString(responseEntity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
以上是获取token跟携带token进行访问第三方接口的一个操作!
@ResponseBody
@RequestMapping(value = "getPageSetForItem")
public Object getPageSetForItem(String startTime, String endTime, String source, String pageNo, String pageSize) throws Exception {
//要访问的第三方的接口地址
String url = "http://www.baidu.com/concrete/inventoryReceivesItem/getPageSetForItem";
//需要传入的参数
Map<String, String> map = new HashMap<>();
map.put("startTime", startTime.toString());
map.put("endTime", endTime.toString());
map.put("source", source);
map.put("pageNo", pageNo);
map.put("pageSize", pageSize);
//获取token
String toKen = (String) WlLoginUtils.parsePassWord();
//调用doPosts方法把参数、接口地址、token传入进行进行调用
String result = HttpClientUtil.doPosts(url, toKen, map);
return result;
}
最后用apipost测试一下成功回显!
记录每一次成长