目录
在JavaWeb系统中,可以通过BO的webservice来获取LogenToken,然后使用LogonToken来打开BO报表,这样就可以实现在系统中免登陆打开BO报表了。在本文的示例代码中使用到的工具有:HttpClient、fastjson。
一、获取LogenToken
要想通过BO的webservice获取LogenToken,必须使用post请求,把用户名、密码、认证方式以json的格式放在body中,才能获取到LogonToken。示例代码如下:
/**
* 获取URL编码后的LogonToken
* @param url BO链接
* @param username 用户名
* @param password 密码
* @param authType 认证类型
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String getBoLogonToken(String url, String username, String password, String authType) throws ClientProtocolException, IOException {
String token = "";
/*
* 1、构建json参数
*/
JSONObject param = new JSONObject();
param.put("username", username);
param.put("password", password);
param.put("auth", authType);
/*
* 2、使用HttpClient发送post请求,获取token
*/
CloseableHttpClient httpClient = HttpClients.createDefault();
/*
* 2.1、构建POST请求
*/
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(param.toJSONString(), "UTF-8");
stringEntity.setContentEncoding("UTF-8");
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json");
/*
* 2.2、发送请求,获取token
*/
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
JSONObject responseBody = JSONObject.parseObject(EntityUtils.toString(httpEntity));
token = responseBody.getString("logonToken");
}
/*
* 3、将获取到的Token进行URL编码
*/
if (token != null && !"".equals(token)) {
token = URLEncoder.encode(token, "UTF-8");
}
return token;
}
二、使用LogonToken获取免登报表链接
BO报表的普通链接为:http://IP:8080/BOE/OpenDocument/opendoc/openDocument.jsp?sIDType=CUID&iDocID=cuid。如果想免登查看BO报表的话,需要在该链接后面添加一个token参数,该参数的值为URL编码后的LogonToken。示例代码如下:
/**
* 获取免登陆的BO报表链接
* @param ip IP
* @param cuid BO报表ID
* @param token token
* @return
*/
public static String getBoReportUrlByToken(String ip, String cuid, String token) {
StringBuilder url = new StringBuilder();
// 拼接URL
url.append("http://").append(ip).append(":8080")
.append("/BOE/OpenDocument/opendoc/openDocument.jsp")
.append("?sIDType=CUID")
.append("&iDocID=").append(cuid)
.append("&token=").append(token);
return url.toString();
}