<%@ page contentType="text/html; charset=UTF-8" %> <%@ page import="com.alibaba.fastjson.JSONObject" %> <%@ page import="javax.crypto.Cipher" %> <%@ page import="java.nio.charset.StandardCharsets" %> <%@ page import="javax.crypto.spec.SecretKeySpec" %> <%@ page import="javax.crypto.spec.IvParameterSpec" %> <%@ page import="org.apache.commons.codec.binary.Base64" %> <%@ page import="java.util.Map" %> <%@ page import="org.apache.http.client.methods.CloseableHttpResponse" %> <%@ page import="org.apache.http.impl.client.CloseableHttpClient" %> <%@ page import="org.apache.http.impl.client.HttpClients" %> <%@ page import="org.apache.http.client.methods.HttpPost" %> <%@ page import="org.apache.http.entity.StringEntity" %> <%@ page import="org.apache.http.HttpEntity" %> <%@ page import="org.apache.http.util.EntityUtils" %> <%@ page import="java.io.IOException" %> <%@ page import="java.util.HashMap" %> <% JSONObject jsonObject = new JSONObject(); String APP_SECRET = "YVBKbcEQSXlwcpOxjOGRhODWASSkzg"; String data = System.currentTimeMillis() + "||" + APP_SECRET; String IV = APP_SECRET.substring(APP_SECRET.length() - 16); String key = APP_SECRET.substring(0, 16); String sign = encryptData(data, key, IV); System.out.println(sign); String url = "http://192.168.10.54:7123/hklearn/api/access/getUserToken"; String username = "SZ001"; Map headers = new HashMap<>(); String encode = "utf-8"; JSONObject param = new JSONObject(); param.put("username",username); param.put("sign",sign); jsonObject.put("url",url); jsonObject.put("sign",sign); jsonObject.put("username",username); String token = httpGetToken(url,param.toString(),headers,"utf-8"); jsonObject.put("token",token); out.print(jsonObject.toJSONString()); %> <%! /** * * @param data 需要加密的数据 * @param key 秘钥 * @param IV 偏移量 * @return */ private static String encryptData(String data, String key, String IV) { try { //算法/模式/填充模式 AES/CBC/PKCS5Padding Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); int plaintextLength = dataBytes.length; byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); //输出内容,Base64处理 return new String(Base64.encodeBase64(encrypted)); } catch (Exception e) { return null; } } /** * 发送 http post 请求,参数以原生字符串进行提交 * @param url * @param encode * @return */ public static String httpGetToken(String url, String stringJson, Map headers, String encode){ if(encode == null){ encode = "utf-8"; } String result = ""; CloseableHttpResponse httpResponse = null; CloseableHttpClient closeableHttpClient = null; try { //HttpClients.createDefault()等价于 HttpClientBuilder.create().build(); closeableHttpClient = HttpClients.createDefault(); HttpPost httpost = new HttpPost(url); //设置header httpost.setHeader("Content-type", "application/json"); if (headers != null && headers.size() > 0) { for (Map.Entry entry : headers.entrySet()) { httpost.setHeader(entry.getKey(),entry.getValue()); } } //组织请求参数 StringEntity stringEntity = new StringEntity(stringJson, encode); httpost.setEntity(stringEntity); //响应信息 httpResponse = closeableHttpClient.execute(httpost); HttpEntity entity = httpResponse.getEntity(); String content = EntityUtils.toString(entity, encode); if(content != null && !"".equals(content)){ JSONObject jsonObject = JSONObject.parseObject(content); int code = (int) jsonObject.get("code"); if(code==10000){ String data = jsonObject.get("data").toString(); JSONObject jsonObject1 = JSONObject.parseObject(data); result = jsonObject1.get("accessToken").toString(); } } } catch (Exception e) { e.printStackTrace(); }finally{ try { httpResponse.close(); } catch (IOException e) { e.printStackTrace(); } } try { //关闭连接、释放资源 closeableHttpClient.close(); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 发送 http post 请求,参数以原生字符串进行提交 * @param url * @param encode * @return */ public static boolean httpPostJson(String url, String stringJson, Map headers, String encode){ if(encode == null){ encode = "utf-8"; } boolean result = false; CloseableHttpResponse httpResponse = null; CloseableHttpClient closeableHttpClient = null; try { //HttpClients.createDefault()等价于 HttpClientBuilder.create().build(); closeableHttpClient = HttpClients.createDefault(); HttpPost httpost = new HttpPost(url); //设置header httpost.setHeader("Content-type", "application/json"); if (headers != null && headers.size() > 0) { for (Map.Entry entry : headers.entrySet()) { httpost.setHeader(entry.getKey(),entry.getValue()); } } //组织请求参数 StringEntity stringEntity = new StringEntity(stringJson, encode); httpost.setEntity(stringEntity); //响应信息 httpResponse = closeableHttpClient.execute(httpost); HttpEntity entity = httpResponse.getEntity(); String content = EntityUtils.toString(entity, encode); if(content != null && !"".equals(content)){ JSONObject jsonObject = JSONObject.parseObject(content); int code = (int) jsonObject.get("code"); if(code==10000){ result = true; } } } catch (Exception e) { e.printStackTrace(); }finally{ try { httpResponse.close(); } catch (IOException e) { e.printStackTrace(); } } try { //关闭连接、释放资源 closeableHttpClient.close(); } catch (IOException e) { e.printStackTrace(); } return result; } %>