Commit b80788ce by gdj

增加ufile验证

parent ab5a23e4
package com.dji.sample;
import com.dji.sample.common.util.CommonUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -13,6 +14,12 @@ import org.springframework.scheduling.annotation.EnableScheduling;
public class CloudApiSampleApplication {
public static void main(String[] args) {
CommonUtil.InitAuth();
if(!CommonUtil.isAuthValid()){
throw new RuntimeException("环境配置错误");
}
SpringApplication.run(CloudApiSampleApplication.class, args);
}
......
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package com.dji.sample.common.util;
import org.springframework.boot.system.ApplicationHome;
import javax.crypto.Cipher;
import java.io.*;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.HashMap;
/**
* 通用工具类
*
* @author Chill
*/
public class CommonUtil {
private static HashMap authItems = null;
public static void InitAuth() {
final String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVU5UPfDeNjolq0VH6oG+GXTdbJ3ZHdhybkv7URSu94DiqpW703SZmV6zpsNtnsBLeGk+k4ELVPcThDS9st/2Y+IkK0gshu4G7SqqusZhGj3c3BCT/0sDgHr84htl2WUZlY/WofajyKNMEPo/NFt5814os2gFvg8wV6gOj+2m/QQIDAQAB";
final int MAX_DECRYPT_BLOCK = 128;
try {
// 现场环境
ApplicationHome home = new ApplicationHome(CommonUtil.class);
String jarPath = home.getSource().getParentFile().toString();
// 现场环境
File file;
file = new File(jarPath + File.separator + "ufile");
if (!file.exists())
file = new File("C:/app/nginx/conf/ufile");
// 本机测试
// File file = new File("/Users/Ryan/ufile");
String lic = null;
if (file.isFile() && file.exists()) {
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
fileInputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileInputStream);
bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String text;
while((text = bufferedReader.readLine()) != null){
sb.append(text);
}
lic = sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
if (lic != null && lic.length() > 0) {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] decodedKey = Base64.getDecoder().decode(publicKeyStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] dataBytes = Base64.getDecoder().decode(lic);
int offset = 0, i = 0, inputLen = dataBytes.length;
byte[] cache;
// 对数据分段解密
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
// 反序列化数据
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
bis = new ByteArrayInputStream(decryptedData);
ois = new ObjectInputStream(bis);
authItems = (HashMap) ois.readObject();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean isAuthValid() {
// return true;
try {
if (authItems != null) {
if (authItems.containsKey("period")) {
String period = (String) authItems.get("period");
if (period.equalsIgnoreCase("infinite"))
return true;
else {
if (!period.contains(":"))
period = period + " 23:59:59";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime deadline = LocalDateTime.parse(period, formatter);
Duration duration = Duration.between(LocalDateTime.now(), deadline);
if (duration.toMinutes() > 0)
return true;
}
}
}
} catch (Exception ex) {
}
return false;
}
}
package com.dji.sample.manage.controller;
import com.dji.sample.common.util.CommonUtil;
import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sample.manage.model.dto.DeviceDictionaryDTO;
import com.dji.sample.manage.model.dto.DeviceFirmwareUpgradeDTO;
......@@ -42,6 +43,10 @@ public class DeviceController {
*/
@GetMapping("/{workspace_id}/devices")
public HttpResultResponse<List<DeviceDTO>> getDevices(@PathVariable("workspace_id") String workspaceId) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
List<DeviceDTO> devicesList = deviceService.getDevicesTopoForWeb(workspaceId);
return HttpResultResponse.success(devicesList);
......@@ -55,6 +60,10 @@ public class DeviceController {
*/
@PostMapping("/{device_sn}/binding")
public HttpResultResponse bindDevice(@RequestBody DeviceDTO device, @PathVariable("device_sn") String deviceSn) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
device.setDeviceSn(deviceSn);
boolean isUpd = deviceService.bindDevice(device);
return isUpd ? HttpResultResponse.success() : HttpResultResponse.error();
......@@ -69,6 +78,10 @@ public class DeviceController {
@GetMapping("/{workspace_id}/devices/{device_sn}")
public HttpResultResponse getDevice(@PathVariable("workspace_id") String workspaceId,
@PathVariable("device_sn") String deviceSn) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
Optional<DeviceDTO> deviceOpt = deviceService.getDeviceBySn(deviceSn);
return deviceOpt.isEmpty() ? HttpResultResponse.error("device not found.") : HttpResultResponse.success(deviceOpt.get());
}
......@@ -82,6 +95,10 @@ public class DeviceController {
@GetMapping("/{workspace_id}/deviceInfo")
public HttpResultResponse getDeviceInfo(@PathVariable("workspace_id") String workspaceId,
@RequestParam("device_sn") String deviceSn) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
Optional<DeviceDTO> deviceOpt = deviceService.getDeviceBySn(deviceSn);
return deviceOpt.isEmpty() ? HttpResultResponse.error("device not found.") : HttpResultResponse.success(deviceOpt.get());
}
......@@ -99,6 +116,10 @@ public class DeviceController {
@RequestParam(value = "device_name", required = false) String deviceName,
@RequestParam(defaultValue = "1") Long page,
@RequestParam(value = "page_size", defaultValue = "50") Long pageSize) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
PaginationData<DeviceDTO> devices = deviceService.getBoundDevicesWithDomain(workspaceId, deviceName, page, pageSize, domain);
return HttpResultResponse.success(devices);
......@@ -111,6 +132,10 @@ public class DeviceController {
*/
@DeleteMapping("/{device_sn}/unbinding")
public HttpResultResponse unbindingDevice(@PathVariable("device_sn") String deviceSn) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
deviceService.unbindDevice(deviceSn);
return HttpResultResponse.success();
}
......@@ -126,6 +151,10 @@ public class DeviceController {
public HttpResultResponse updateDevice(@RequestBody DeviceDTO device,
@PathVariable("workspace_id") String workspaceId,
@PathVariable("device_sn") String deviceSn) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
device.setDeviceSn(deviceSn);
boolean isUpd = deviceService.updateDevice(device);
return isUpd ? HttpResultResponse.success() : HttpResultResponse.error();
......@@ -140,6 +169,10 @@ public class DeviceController {
@PostMapping("/{workspace_id}/devices/ota")
public HttpResultResponse createOtaJob(@PathVariable("workspace_id") String workspaceId,
@RequestBody List<DeviceFirmwareUpgradeDTO> upgradeDTOS) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
return deviceService.createDeviceOtaJob(workspaceId, upgradeDTOS);
}
......@@ -154,6 +187,10 @@ public class DeviceController {
public HttpResultResponse devicePropertySet(@PathVariable("workspace_id") String workspaceId,
@PathVariable("device_sn") String dockSn,
@RequestBody JsonNode param) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
if (param.size() != 1) {
return HttpResultResponse.error(CloudSDKErrorEnum.INVALID_PARAMETER);
}
......@@ -170,6 +207,10 @@ public class DeviceController {
*/
@PostMapping("/{workspace_id}/addDrone")
public HttpResultResponse addDrone(@RequestBody DeviceDTO aircraft, @PathVariable("workspace_id") String workspaceId) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
aircraft.setWorkspaceId(workspaceId);
boolean isAdd = deviceService.addDrone(aircraft);
return isAdd ? HttpResultResponse.success() : HttpResultResponse.error();
......@@ -182,6 +223,10 @@ public class DeviceController {
*/
@PostMapping("/{workspace_id}/editDrone")
public HttpResultResponse editDrone(@RequestBody DeviceDTO aircraft, @PathVariable("workspace_id") String workspaceId) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
aircraft.setWorkspaceId(workspaceId);
boolean isAdd = deviceService.editDrone(aircraft);
return isAdd ? HttpResultResponse.success() : HttpResultResponse.error();
......@@ -194,6 +239,10 @@ public class DeviceController {
*/
@PostMapping("/{workspace_id}/addAirport")
public HttpResultResponse addAirport(@RequestBody DeviceDTO airport, @PathVariable("workspace_id") String workspaceId) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
airport.setWorkspaceId(workspaceId);
boolean isAdd = deviceService.addAirport(airport);
return isAdd ? HttpResultResponse.success() : HttpResultResponse.error();
......@@ -205,6 +254,10 @@ public class DeviceController {
*/
@PostMapping("/{workspace_id}/editAirport")
public HttpResultResponse editAirport(@RequestBody DeviceDTO airport, @PathVariable("workspace_id") String workspaceId) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
airport.setWorkspaceId(workspaceId);
boolean isAdd = deviceService.editAirport(airport);
return isAdd ? HttpResultResponse.success() : HttpResultResponse.error();
......@@ -219,6 +272,9 @@ public class DeviceController {
@GetMapping("/{workspace_id}/deviceDictionary")
public HttpResultResponse getDeviceDictionary(@PathVariable("workspace_id") String workspaceId,
@RequestParam(value = "domain", required = false) Integer domain) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
List<DeviceDictionaryDTO> infoList = deviceDictionaryService.getDictionaryInfoList(domain);
return CollectionUtils.isEmpty(infoList) ? HttpResultResponse.error("deviceDictionary not found.") : HttpResultResponse.success(infoList);
......@@ -237,6 +293,10 @@ public class DeviceController {
@PathVariable("workspace_id") String workspaceId,
@RequestParam(defaultValue = "1") Long page,
@RequestParam(value = "page_size", defaultValue = "50") Long pageSize) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
PaginationData<DeviceDTO> devices = deviceService.getDevicesByParam(param, workspaceId, page, pageSize);
return HttpResultResponse.success(devices);
......@@ -249,6 +309,10 @@ public class DeviceController {
*/
@DeleteMapping("/{device_sn}/delete")
public HttpResultResponse deleteDevice(@PathVariable("device_sn") String deviceSn) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
deviceService.deleteDevice(deviceSn);
return HttpResultResponse.success();
}
......@@ -266,6 +330,10 @@ public class DeviceController {
@RequestParam(value = "device_name", required = false) String deviceName,
@RequestParam(defaultValue = "1") Long page,
@RequestParam(value = "page_size", defaultValue = "50") Long pageSize) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
PaginationData<DeviceDTO> devices = deviceService.getNoWorkspaceDevices(workspaceId, deviceName, page, pageSize, domain);
return HttpResultResponse.success(devices);
......@@ -280,6 +348,10 @@ public class DeviceController {
@DeleteMapping("/{workspace_id}/deleteById")
public HttpResultResponse deleteDeviceById(@PathVariable("workspace_id") String workspaceId,
@RequestParam(name = "id") Integer id) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
deviceService.deleteDeviceById(workspaceId, id);
return HttpResultResponse.success();
}
......@@ -296,6 +368,10 @@ public class DeviceController {
@PathVariable("workspace_id") String workspaceId,
@RequestParam(defaultValue = "1") Long page,
@RequestParam(name = "page_size", defaultValue = "50") Long pageSize) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
PaginationData<DeviceDTO> devices = deviceService.getDockAndDrone(workspaceId, page, pageSize);
return HttpResultResponse.success(devices);
......@@ -308,6 +384,10 @@ public class DeviceController {
*/
@PostMapping("/{workspace_id}/share")
public HttpResultResponse shareDevice(@RequestBody DeviceDTO device, @PathVariable("workspace_id") String workspaceId) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
device.setWorkspaceId(workspaceId);
deviceService.shareDevice(device);
return HttpResultResponse.success();
......@@ -319,6 +399,10 @@ public class DeviceController {
*/
@PostMapping("/{workspace_id}/cancelShare")
public HttpResultResponse cancelShareDevice(@RequestBody DeviceDTO device, @PathVariable("workspace_id") String workspaceId) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
device.setWorkspaceId(workspaceId);
deviceService.cancelShareDevice(device);
return HttpResultResponse.success();
......@@ -331,6 +415,9 @@ public class DeviceController {
*/
@PostMapping("/{workspace_id}/recoverOnline")
public HttpResultResponse recoverOnline(@RequestBody DeviceDTO device, @PathVariable("workspace_id") String workspaceId) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
deviceService.recoverDeviceOnline(device.getDeviceSn());
return HttpResultResponse.success();
......
package com.dji.sample.manage.controller;
import com.dji.sample.common.error.CommonErrorEnum;
import com.dji.sample.common.util.CommonUtil;
import com.dji.sample.manage.model.dto.UserDTO;
import com.dji.sample.manage.model.dto.UserLoginDTO;
import com.dji.sample.manage.service.IUserService;
......@@ -25,6 +26,10 @@ public class LoginController {
@PostMapping("/login")
public HttpResultResponse login(@RequestBody UserLoginDTO loginDTO) {
if(!CommonUtil.isAuthValid()){
return HttpResultResponse.error("环境配置错误");
}
String username = loginDTO.getUsername();
String password = loginDTO.getPassword();
// return userService.userLogin(username, password, loginDTO.getFlag());
......
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