华企号 元宇宙 API接口加密方式JAVA版API接口如何加密?

API接口加密方式JAVA版API接口如何加密?

PI接口加密方式代码分享API接口加密源代码JAVA版,一下是JAVA完整的加密源代码,含有服务端和客户端demo

我之前写好了一版PHP版本的API接口加密通信DEMO代码

现在这一版是JAVA版API接口加密通信代码,并且这一版的客户端Test.java和我写好的PHP服务端是完全互通的

当然下面写好的服务端Encry.java和之前的写的PHP客户端也是互通的,这样做也是为了让同时有JAVA和PHP的程序员们能更好地协作通信

 

1.加密签名类EncryBean.java

2.服务端:Encry.java

3.客户端:Test.java

这和我之前写好的PHP服务端代码是互通的,这样子也方便用JAVA的小白尽快上手

以下是JAVA版的API接口加密通信源代码,代码含客户端和服务端

1.EncryBean.java是封装的加密生成签名的类文件

“`cpp

“`java
package encry;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import bean.EncryBean;
public class Encry extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html”);
response.setCharacterEncoding(“UTF-8”);
String timeStamp = request.getParameter(“t”);//获取时间戳
String randomStr = request.getParameter(“r”);//获取随机串
String signature = request.getParameter(“s”);//获取签名
PrintWriter out = response.getWriter();
Map<String, Object> map = new HashMap<String, Object>();
if(timeStamp==null && timeStamp==””){
map.put(“status”, “100”);//状态码100时间戳不能为空
}
if(randomStr==null && randomStr==””){
map.put(“status”, “101”);//状态码101随机串不能为空
}
if(signature==null && signature==””){
map.put(“status”, “102”);//状态码102签名不能为空
}

System.out.println(“timeStamp”+timeStamp);
System.out.println(“randomStr”+randomStr);
System.out.println(“signature”+signature);

String TOKEN = “API”;//令牌字符串(双方约定)
EncryBean a = new EncryBean();
String str = a.arithmetic(timeStamp,randomStr,TOKEN);//通过前台传过来的时间戳跟随机数重新按照签名函数进行生成一遍签名
//然后将传过来签名跟,自己重新生成的签名进行比对

if(str.equals(signature)){
map.put(“status”, “1”);//状态码1成功
}else{
map.put(“status”, “0”);//状态码0签名错误
}
String mapJson = JSON.toJSONString(map);
out.print(mapJson);
out.flush();
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}

}
“`
2.服务端是Encry.java ,这个文件接收客户端请求

“`java
package encry;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import bean.EncryBean;
public class Encry extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType(“text/html”);
response.setCharacterEncoding(“UTF-8”);
String timeStamp = request.getParameter(“t”);//获取时间戳
String randomStr = request.getParameter(“r”);//获取随机串
String signature = request.getParameter(“s”);//获取签名
PrintWriter out = response.getWriter();
Map<String, Object> map = new HashMap<String, Object>();
if(timeStamp==null && timeStamp==””){
map.put(“status”, “100”);//状态码100时间戳不能为空
}
if(randomStr==null && randomStr==””){
map.put(“status”, “101”);//状态码101随机串不能为空
}
if(signature==null && signature==””){
map.put(“status”, “102”);//状态码102签名不能为空
}

System.out.println(“timeStamp”+timeStamp);
System.out.println(“randomStr”+randomStr);
System.out.println(“signature”+signature);

String TOKEN = “API”;//令牌字符串(双方约定)
EncryBean a = new EncryBean();
String str = a.arithmetic(timeStamp,randomStr,TOKEN);//通过前台传过来的时间戳跟随机数重新按照签名函数进行生成一遍签名
//然后将传过来签名跟,自己重新生成的签名进行比对

if(str.equals(signature)){
map.put(“status”, “1”);//状态码1成功
}else{
map.put(“status”, “0”);//状态码0签名错误
}
String mapJson = JSON.toJSONString(map);
out.print(mapJson);
out.flush();
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}

}
“`
3.客户端调用是Test.java

“`java
package bean;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

public static String interfaceUtil(String path,String data) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty(“accept”, “*/*”);
conn.setRequestProperty(“connection”, “Keep-Alive”);
conn.setRequestProperty(“user-agent”, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)”);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(“GET”);//GET和POST必须全大写
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = “”;

while ((str = br.readLine()) != null) {
str=new String(str.getBytes(),”UTF-8″);//解决中文乱码问题
sb.append(str);
}
is.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}

public static void main(String[] args) {

String tk = “API”;//密钥
String t = System.currentTimeMillis()+”” ;//时间戳;
String r = “zf4edLXLl666666666”; //随机串
EncryBean a = new EncryBean();
String s = a.arithmetic(t,r,tk);//生成签名
//String response = interfaceUtil(“http://localhost:8080/servlet/Encry?t=”+t+”&r=”+r+”&s=”+s,””);//get请求
String response = interfaceUtil(“http://www.myhost.com/api.php?t=”+t+”&r=”+r+”&s=”+s,””);//get请求

System.out.println(response);
}

}
“`
以上就是JAVA版的API接口加密代码,技术交流微信:15889726201

 

作者: 李铁牛程序员

李铁牛,一直致力于企业客户软件定制开发,计算机专业毕业后,一直从事于互联网产品开发到现在。微信技术交流:tieniu6636
上一篇
下一篇

发表回复

联系我们

联系我们

028-84868647

在线咨询: QQ交谈

邮箱: tech@68v8.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部