java实现接口签名
为了保证数据传输的安全性,跟其他系统进行数据交互时,双方应该约定好密钥,把数据进行加密,接口签名,这样双方调用接口时,验证接口签名一致时就表明数据传输过程中没有被修改。
后端数据签名主要代码:
控制器:
@Controller
public class SignController {
@Autowired
private ISignService signService;
@RequestMapping("/testSign")
@ResponseBody
public String sign(@RequestBody SignDTO dto) throws Exception{
return signService.testSign(dto);
}
}
实现类:
@Service
public class SignServiceImpl implements ISignService {
public SignDTO getSignData() throws Exception{
User user = new User();
user.setName("测试");
user.setSex("男");
String data = JSONUtil.toJsonStr(user);
//密钥
String accessKey = "accessKey";
String secretKey = "secretKey";
String version = "1.0";
int encrypt = 1;
long time = System.currentTimeMillis();
String nonce = RandomUtil.randomString(4);
//本地加签
SignReqParam signReqParam = new SignReqParam(
accessKey, time, nonce, encrypt, version,
secretKey, data);
//数据加密
data = EncryptUtils.aesEncrypt(data, EncryptUtils.KEY);
System.out.println("加密后数据:" + data);
SignDTO dto = new SignDTO();
dto.setAccessKey(accessKey);
dto.setVersion(version);
dto.setEncrypt(encrypt);
dto.setNonce(nonce);
dto.setTimestamp(time);
dto.setSign(signReqParam.getSign());
dto.setData(data);
return dto;
}
public String getSignDataJson() throws Exception{
SignDTO dto = getSignData();
return JSONUtil.toJsonStr(dto);
}
@Override
public String testSign(SignDTO dto) throws Exception{
String result = "签名不合法";
//密钥
String accessKey = "accessKey";
String secretKey = "secretKey";
String version = "1.0";
int encrypt = 1;
if(ObjectUtils.isEmpty(dto.getVersion()) || !dto.getVersion().equals(version) || ObjectUtils.isEmpty(dto.getData()) || ObjectUtils.isEmpty(dto.getAccessKey()) || !dto.getAccessKey().equals(accessKey)){
return result;
}
// 解密为字符串
String data = EncryptUtils.aesDecrypt(dto.getData(), EncryptUtils.KEY);
System.out.println("解密后数据:" + data);
//本地加签
SignReqParam signReqParam = new SignReqParam(
accessKey, dto.getTimestamp(), dto.getNonce(), encrypt, version,
secretKey, data);
//签名对比
if(dto.getSign().equals(signReqParam.getSign())){
User user = JSONUtil.toBean(data, User.class);
System.out.println("接口签名合法,可以处理请求中的用户信息,可以进行一系列数据库操作:" + user.toString());
result = "接口签名合法";
}
return result;
}
public static void main(String[] args) throws Exception {
SignServiceImpl obj = new SignServiceImpl();
SignDTO dto = obj.getSignData();
String result = obj.testSign(dto);
System.out.println("接口签名结果:" + result);
}
实体类:
@Data
public class SignDTO {
private String accessKey;
private String secretKey;
private String version;
private int encrypt;
private String nonce;
private String sign;
private String data;
private long timestamp;
}
@Data
public class User implements Serializable {
private String name;
private String sex;
}
main方法测试结果:
Connected to the target VM, address: '127.0.0.1:52259', transport: 'socket'
加密后数据:IkoCXfjUX0dawZkLQlqkcex3TJzzQM9VsLg1UuCSvz4=
获取接口请求数据json:{"data":"IkoCXfjUX0dawZkLQlqkcex3TJzzQM9VsLg1UuCSvz4=","accessKey":"accessKey","encrypt":1,"sign":"49EA679C9ED2C49E7FDB7221ECD5784F","version":"1.0","nonce":"oe9w","timestamp":1622972771180}
加密后数据:IkoCXfjUX0dawZkLQlqkcex3TJzzQM9VsLg1UuCSvz4=
解密后数据:{"sex":"男","name":"测试"}
接口签名合法,可以处理请求中的用户信息,可以进行一系列数据库操作:User(name=测试, sex=男)
接口签名结果:接口签名合法
Disconnected from the target VM, address: '127.0.0.1:52259', transport: 'socket'
postman测试结果:
下载demo
到此这篇关于接口签名怎么用Java实现的文章就介绍到这了,更多相关java实现接口签名内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!