package com.mm.controller; import cn.hutool.crypto.SmUtil; import com.mm.util.*; import com.mm.vo.*; import org.apache.commons.lang3.StringUtils; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.method.HandlerMethod; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.InetAddress; import java.net.URLDecoder; import java.net.UnknownHostException; @RestController @RequestMapping("/sys/api") public class ServiceMdcController { @Value("${giveServiceTo.appIdCheck}") private String appIdCheck; @Value("${giveServiceTo.passwordCheck}") private String passwordCheck; @Value("${giveServiceTo.logIp}") private String logIp; @Value("${giveServiceTo.logPort}") private String logPort; @Value("${giveServiceTo.serverIp}") private String serverIp; @Value("${giveServiceTo.clientIp}") private String clientIp; @Value("${fileNumPath}") private String fileNumPath; @PostMapping(value = "/appAuth") public TokenResp getToken(@RequestBody PostParams postParams) { //获取请求头中传入的appId和password String appId = postParams.getAppId(); String password = postParams.getPassword(); //用于校验的正确的appId和password(根据业务调整) /*String appIdCheck = "admin"; String passwordCheck = "123";*/ if (appIdCheck.equals(appId)) { //校验通过,生成token签名 if (SM3Util.verify(passwordCheck, password)) { String token = JwTUtil.sign(appId, passwordCheck); return new TokenResp("200", "认证成功", token); } else { return new TokenResp("101", "密码错误", null); } } else { return new TokenResp("101", "账号错误", null); } // //校验appId和paaword,由于测试用的明文密码,SM3Util.verify()先将铭文密码SM3加密后再与传入password对比 // if (appIdCheck.equals(appId) && SM3Util.verify(passwordCheck, password)) { // //校验通过,生成token签名 // String token = JwTUtil.sign(appId, passwordCheck); // return new TokenResp("200", "认证成功", token); // } else { // //to-do认证失败 // return null; // } } // public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { // //如果不是映射到方法直接通过 // if (!(handler instanceof HandlerMethod)) { // return true; // } // //从 http 请求头中取出 token // String token = request.getHeader("Token"); // if (token == null) { // throw new RuntimeException("无 token ,请重新登陆"); // } // /*//验证 token,JwtUtil为生成token和验证token的工具类 // JwTUtil.checkSign(token); // // //获取 token 中的 appId // String appId = JwTUtil.getUserId(token); // //获取 token 中的其他数据 // Map info = JwTUtil.getInfo(token); // info.forEach((k, v) -> System.out.println(k + ":" + v));*/ // return true; // } @PostMapping(value = "/fileUpload") public RespData uploadFiles(HttpServletRequest request) { SysLogTypeObjectDto objectName = new SysLogTypeObjectDto(); objectName.setDateTime(DateUtil.format(DateUtil.getNow(), DateUtil.STR_DATE_TIME_SMALL)); FileDetail fileDetail = new FileDetail(); try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //获取FileName if (request.getHeader("FileName") != null) { try { fileDetail.setFileName(URLDecoder.decode(request.getHeader("FileName"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //获取Content-Length if (request.getHeader("Content-Length") != null) { try { fileDetail.setContentLength(URLDecoder.decode(request.getHeader("Content-Length"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //获取文件流 ServletInputStream inputStream = null; try { inputStream = request.getInputStream(); } catch (IOException e) { e.printStackTrace(); } if (inputStream == null) { System.out.println("inputStream为空"); } //操作文件流,上传文件 //fileService.uploadFile(inputStream,fileDetail.getFileName()); //DataInputStream in = new DataInputStream(inputStream); FileOutputStream fileOut = null; try { fileOut = new FileOutputStream(fileDetail.getFileName());//这里可以改路径 IOUtils.copy(inputStream, fileOut); fileOut.flush(); // fileOut.flush(); objectName.setResult("成功"); File file = new File(fileDetail.getFileName()); objectName.setFileSize(file.getName()); objectName.setDateTime(DateUtil.format(DateUtil.getNow(), DateUtil.STR_DATE_TIME_SMALL)); objectName.setFileSize(FileUtil.changeFileFormatKb(String.valueOf(file.length()))); objectName.setDestination(serverIp);//服务端IP objectName.setSourceAddress(clientIp);//客户端 String sm3 = SmUtil.sm3(file); objectName.setAbstract1(sm3); //不记录数据量 String fileNum = FileNumUtil.readFileSum(fileNumPath); if (StringUtils.isBlank(fileNum)) { objectName.setFileNum(Integer.toString(1)); FileNumUtil.fileWriterSql(Integer.toString(1), fileNumPath); } else { Integer num = Integer.valueOf(fileNum) + 1; objectName.setFileNum(Integer.toString(num)); FileNumUtil.fileWriterSql(Integer.toString(num), fileNumPath); } InetAddress address = null; try { address = InetAddress.getLocalHost(); String ip = address.getHostAddress(); objectName.setAddress(ip); } catch (UnknownHostException e) { objectName.setAddress("20.10.17.19"); } try { objectName.setTypes("Info"); SyslogClient.sendClient(logIp, Integer.valueOf(logPort), objectName.toString()); } catch (Exception e) { } } catch (IOException e) { objectName.setResult("失败"); objectName.setTypes("error"); SyslogClient.sendClient(logIp, Integer.valueOf(logPort), objectName.toString()); e.printStackTrace(); } finally { try { if (inputStream != null) { inputStream.close(); } if (fileOut != null) { fileOut.close(); } } catch (IOException e) { e.printStackTrace(); } } return new RespData("200", "文件接收成功"); } }