Commit a3f29f4f by luozhenzhen

新增数据业务上传接口

parent 0503cb2a
No preview for this file type
......@@ -14,7 +14,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
</properties>
......@@ -44,20 +43,8 @@
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>lib</directory>
......@@ -66,6 +53,18 @@
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
......
package com;
import com.reyun.Algorithm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......
......@@ -9,14 +9,20 @@ import com.reyun.Algorithm;
*/
public class SpecialSecret {
// public static final String dllPath = "C:\\eclipseWorkSpace\\deviceCheat\\lib\\cryptoEx.dll";
public static final String dllPath = "/data/application/deviceCheck/crypto/libcrypto.so";
static {
System.load(dllPath);
// Algorithm.InitCrypto();
}
public static byte [] aes256EncodeB(byte [] data,byte [] key){
Algorithm alg = new Algorithm();
return alg.aes256EncodeB(data, key);
}
/**
*
* 解密数据
......
package com.demo.dao;
import com.demo.entity.po.BusinData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class BusinDataDao {
@Autowired
private MongoTemplate mongoTemplate;
public void insert(BusinData data){
mongoTemplate.insert(data);
}
}
package com.demo.entity.po;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection="BusinData")
public class BusinData {
private String StBusinData;
public String getStBusinData() {
return StBusinData;
}
public void setStBusinData(String stBusinData) {
StBusinData = stBusinData;
}
}
......@@ -736,14 +736,14 @@ public class DataImportController {
"\t\t\t]\n" +
"\t\t}\n" +
"\t],\n" +
"\t\"updata\": {\n" +
"\t\t\"isupdata\": \"true\",\n" +
"\t\t\"jarurl\": \"jar包下载地址\",\n" +
"\t\t\"key\": \"解密jar包key\"\n" +
"\t},\n" +
"\t\"white_apps\": [\n" +
"\n" +
"\t],\n" +
"\"update\":{"+
"\"isupdate\":true,"+
"\"jarurl\":\"https://39.104.115.147:10081/downloadFile\","+
"\"key\":\"5662e7c3d61fd5e0e67acd8bace6d1b0\""+
"},"+
"\"period\":1800"+
"}";
str = str.replaceAll("\n","");
......
package com.demo.mongodb;
import com.alibaba.fastjson.JSONObject;
import com.demo.common.SpecialSecret;
import com.demo.constant.ResultStatus;
import com.demo.entity.dto.DeviceReturnDto;
import com.demo.entity.po.BusinData;
import com.demo.entity.po.ExceptionRecord;
import com.demo.entity.vo.DeviceConfVo;
import com.demo.entity.vo.DeviceIdVo;
import com.demo.entity.vo.SensorVo;
import com.demo.service.DeviceService;
import com.demo.util.DeformedBase64;
import com.demo.util.FileEncryptor;
import com.demo.util.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/")
......@@ -93,4 +96,57 @@ public class DevicecheatController {
return new ResultObject(ResultStatus.SUCCESS);
}
@RequestMapping(value = "/encryptorFile", method = RequestMethod.GET)
@ResponseBody
public ResultObject deviceconf() throws Exception {
String sourceFilePath = "/data/application/deviceCheck/updateJar/update.jar";
String destFilePath = "/data/application/deviceCheck/updateJar/encryptorFile/update.jar";
// String sourceFilePath = "C:\\Users\\reyun\\Desktop\\update.jar";
// String destFilePath = "C:\\Users\\reyun\\Desktop\\update_2.jar";
String key = "5662e7c3d61fd5e0e67acd8bace6d1b0";
FileEncryptor.decryptFile(key,sourceFilePath,destFilePath);
return new ResultObject(ResultStatus.SUCCESS);
}
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
@ResponseBody
public ResultObject downloadFile(HttpServletResponse response, String fileName) throws Exception {
String sourceFilePath = "/data/application/deviceCheck/updateJar/encryptorFile/update.jar";
// String sourceFilePath = "C:\\Users\\reyun\\Desktop\\update_2.jar";
FileEncryptor.downloadFileToPath(sourceFilePath,fileName, response);
return new ResultObject(ResultStatus.SUCCESS);
}
@RequestMapping(value = "/getIp", method = RequestMethod.GET)
@ResponseBody
public ResultObject getIp(HttpServletRequest request) throws Exception {
String ip = deviceService.getIpAddr(request);
Map<String, Object> result = new HashMap<>();
result.put("ip",ip);
return new ResultObject(result);
}
/**
*
* 接收上传的数据:
* "StBusinData":{
* "app_id": "11111111111", //String 创建产品时获得的32位字符长度的AppID
* "user_id": "22222222222", //String 账号标识识,支持英文、数字、下划线
* "user_name": "33333333333" //String 账号名称,该值不存在时请传user_id
* }
* @param data
* @return
* @throws Exception
*/
@RequestMapping(value = "/businData", method = RequestMethod.POST)
@ResponseBody
public ResultObject businData(BusinData data) throws Exception {
deviceService.insertBusinData(data);
return new ResultObject(ResultStatus.SUCCESS);
}
}
......@@ -7,14 +7,18 @@ import com.demo.util.ResultObject;
import com.reyun.Algorithm;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/index")
public class IndexController {
@RequestMapping(value = "/testSo", method = RequestMethod.GET)
@ResponseBody
public ResultObject testSo(DeviceIdVo deviceIdVo) throws Exception{
// deviceIdVo = new DeviceIdVo();
public ResultObject testSo(DeviceIdVo deviceIdVo, HttpServletRequest request) throws Exception{
// deviceIdVo = new DeviceIdVo();
// deviceIdVo.setData("ajT2aaaaGClM8q2TCEQVu192GxAjba==");
// Algorithm algorithm = new Algorithm();
......
......@@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@Service
......@@ -37,7 +38,8 @@ public class DeviceService {
private DeviceConfDao deviceConfDao;
@Autowired
private SensorInfoDao sensorInfoDao;
@Autowired
private BusinDataDao businDataDao;
// /**
// *
......@@ -340,4 +342,38 @@ public class DeviceService {
}
return secret;
}
public void insertBusinData(BusinData data){
businDataDao.insert(data);
}
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
if( ip.indexOf(",")!=-1 ){
ip = ip.split(",")[0];
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
package com.demo.util;
import com.demo.common.SpecialSecret;
import com.reyun.Algorithm;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.security.Key;
import java.util.Random;
/**
*
* 文件加密
*
*/
public class FileEncryptor {
private static final String ALGORITHM = "AES";
private static final int CACHE_SIZE = 1024;
/**
* <p>
* 文件加密
* </p>
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @throws Exception
*/
public static void encryptFile(String key, String sourceFilePath, String destFilePath) throws Exception {
File sourceFile = new File(sourceFilePath);
File destFile = new File(destFilePath);
if (sourceFile.exists() && sourceFile.isFile()) {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destFile);
byte[] data = inputStreamToByte(in);
byte[] aesData = SpecialSecret.aes256EncodeB(data, key.getBytes());
ByteArrayInputStream cin = StringToInputStream(aesData);
// byte[] raw = key.getBytes();
// SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
// Cipher cipher = Cipher.getInstance(ALGORITHM);
// cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
// CipherInputStream cin = new CipherInputStream(in, cipher);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = cin.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
cin.close();
in.close();
}
}
public static byte[] inputStreamToByte(InputStream input) throws IOException{
byte[] b = new byte[4096];
for (int n; (n = input.read(b)) != -1;) {
}
return b;
}
public static ByteArrayInputStream StringToInputStream(byte[] str){
ByteArrayInputStream is = new ByteArrayInputStream(str);
return is;
}
/**
* <p>
* 文件解密
* </p>
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @throws Exception
*/
public static void decryptFile(String key, String sourceFilePath, String destFilePath) throws Exception {
File sourceFile = new File(sourceFilePath);
File destFile = new File(destFilePath);
if (sourceFile.exists() && sourceFile.isFile()) {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
FileInputStream in = new FileInputStream(sourceFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] raw = key.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
CipherOutputStream cout = new CipherOutputStream(out, cipher);
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
cout.write(cache, 0, nRead);
cout.flush();
}
cout.close();
out.close();
in.close();
}
}
// public static void downloadFileToPath(String sourceFilePath, String destFilePath) throws Exception{
// File sourceFile = new File(sourceFilePath);
// File destFile = new File(destFilePath);
// if (sourceFile.exists() && sourceFile.isFile()) {
// if (!destFile.getParentFile().exists()) {
// destFile.getParentFile().mkdirs();
// }
// destFile.createNewFile();
// FileInputStream in = new FileInputStream(sourceFile);
// FileOutputStream out = new FileOutputStream(destFile);
//
// byte[] cache = new byte[CACHE_SIZE];
// int nRead = 0;
// while ((nRead = in.read(cache)) != -1) {
// out.write(cache, 0, nRead);
// out.flush();
// }
// out.close();
// in.close();
// }
public static void downloadFileToPath(String sourceFilePath, String fileName, HttpServletResponse resp) throws Exception{
String realName = fileName;
try{
realName = URLEncoder.encode(realName,"utf-8");
}catch (Exception e){
}
resp.setContentType("multipart/form-data");//通知浏览器数据类型
resp.addHeader("Content-Disposition", "attachment;filename=" + realName);//告诉浏览器文件名
ServletOutputStream out = null;
try {
File sourceFile = new File(sourceFilePath);
if (sourceFile.exists() && sourceFile.isFile()) {
FileInputStream in = new FileInputStream(sourceFile);
out = resp.getOutputStream();
byte[] cache = new byte[CACHE_SIZE];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
// public static void main(String args[]) throws Exception{
// String key = "5662e7c3d61fd5e0e67acd8bace6d1b0";
// String sourceFilePath = "C:\\Users\\reyun\\Desktop\\reyunCrypto.jar";
// String destFilePath = "C:\\Users\\reyun\\Desktop\\new.jar";
// encryptFile(key, sourceFilePath,destFilePath);
//// decryptFile(key, destFilePath, sourceFilePath);
//
//// InputStream in = new FileInputStream(sourceFilePath);
//// System.out.println(inputStreamToString(in));
//// downloadFileToPath(sourceFilePath, "C:\\Users\\reyun\\Desktop\\mongo.jar");
// }
}
package com.demo.util;
package com.reyun;
public class Algorithm {
......@@ -9,8 +9,6 @@ public class Algorithm {
public native String rsaEncode(String plantext,String pubkey);
/**
* @param ciphertext
* @param privatekey
......@@ -26,4 +24,8 @@ public class Algorithm {
public native String aes256Decode(String ciphertext,String key);
public native byte [] aes256EncodeB(byte [] data,byte [] key);
public native byte [] aes256DecodeB(byte [] data,byte [] key);
}
......@@ -2,7 +2,7 @@ server.port=8081
spring.data.mongodb.host=192.168.168.81
spring.data.mongodb.port=8763
spring.data.mongodb.database=device
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store=classpath:keystore.p12_win
server.ssl.key-store-password=123456
server.ssl.key-store-type=PKCS12
server.ssl.key-alias= tomcat
\ No newline at end of file
......@@ -2,7 +2,7 @@ server.port=8081
spring.data.mongodb.host=192.168.168.81
spring.data.mongodb.port=8763
spring.data.mongodb.database=device
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store=classpath:keystore.p12_win
server.ssl.key-store-password=123456
server.ssl.key-store-type=PKCS12
server.ssl.key-alias= tomcat
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment