private static String HZG_BASE_URL=ConfigUtil.getConfigValue("HZG_BASE_URL", "http://localhost:47518");
private static String HZG_USER_NAME=ConfigUtil.getConfigValue("HZG_USER_NAME", "NULL");
private static String HZG_PASSWORD=ConfigUtil.getConfigValue("HZG_PASSWORD", "123456");
public static String getToken() {
String userName = "admin";
if(!HZG_USER_NAME.equals("NULL")){
userName=HZG_USER_NAME;
}else {
userName=Toolkit.getCurrentUser().getLoginname();
}
String password = HZG_PASSWORD; // 这个密码是单点登录密
String hzg_baseurl=HZG_BASE_URL;
StringBuffer resultStr=new StringBuffer();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(hzg_baseurl+"/SSO/GetUserToken");
post.setHeader("Accept", "application/json, text/javascript, */*");
post.setHeader("Content-Type", "application/json; charset=UTF-8");
String params = "{userName:\"" + userName + "\", password:\"" + password + "\"}";
StringEntity s = new StringEntity(params, "UTF-8");
s.setContentType("application/json;charset=UTF-8");
s.setContentEncoding("UTF-8");
post.setEntity(s);
try {
HttpResponse res = httpClient.execute(post);
HttpEntity entity = res.getEntity();
InputStreamReader is = new InputStreamReader(entity.getContent(), "UTF-8");
BufferedReader in = new BufferedReader(is);
for(String inputLine = null; (inputLine = in.readLine()) != null;)
resultStr.append(inputLine);
String msg=resultStr.toString();
if(msg.startsWith("Error")) return "";
return msg;
}catch(Exception e) {
e.printStackTrace();
}
return "";
} |