diff --git a/nxhs-service/src/main/java/cc/yunxi/config/WebMvcConfig.java b/nxhs-service/src/main/java/cc/yunxi/config/WebMvcConfig.java index aaefa3c..4e2b618 100644 --- a/nxhs-service/src/main/java/cc/yunxi/config/WebMvcConfig.java +++ b/nxhs-service/src/main/java/cc/yunxi/config/WebMvcConfig.java @@ -16,7 +16,6 @@ import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoi import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier; import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @@ -49,6 +48,7 @@ public class WebMvcConfig implements WebMvcConfigurer { "/webjars/**", "/static/**", "/upload/**", + "/qrcode/**.txt", "/doc.html" ); @@ -77,6 +77,7 @@ public class WebMvcConfig implements WebMvcConfigurer { private static final String UPLOAD_PATH = System.getProperty("user.dir") + "/upload/"; + private static final String QR_CODE_PATH = System.getProperty("user.dir") + "/qrcode/"; /** * 配置静态资源访问路径 @@ -89,6 +90,8 @@ public class WebMvcConfig implements WebMvcConfigurer { registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/","classpath:/META-INF/resources/webjars/"); // 定义资源访问路径 registry.addResourceHandler("/upload/**").addResourceLocations("file:" + UPLOAD_PATH); + // 定义二维码相关的资源访问路径 + registry.addResourceHandler("/api/qrcode/**.txt").addResourceLocations("file:" + QR_CODE_PATH); } diff --git a/nxhs-service/src/main/java/cc/yunxi/controller/QRCodeController.java b/nxhs-service/src/main/java/cc/yunxi/controller/QRCodeController.java deleted file mode 100644 index d8c9962..0000000 --- a/nxhs-service/src/main/java/cc/yunxi/controller/QRCodeController.java +++ /dev/null @@ -1,85 +0,0 @@ -package cc.yunxi.controller; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiOperation; -import lombok.extern.slf4j.Slf4j; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.http.ResponseEntity; -import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -@ApiModel("机柜扫码登录") -@Slf4j -@RestController("/qrcode") -public class QRCodeController { - - private static final Map FILE_CONTEXT = new ConcurrentHashMap<>(); - -// @ApiOperation("二维码校验文件") -// @GetMapping("/qrcode/{filename}") -// public void getQRCode(HttpServletResponse response, @PathVariable String filename) throws IOException { -// Resource resource = new ClassPathResource("static/" + filename); -// -// if (resource.exists() || resource.isReadable()) { -// // 设置响应头 -// String contentType = null; -// try (InputStream inputStream = resource.getInputStream()) { -// contentType = "application/octet-stream"; -// } catch (IOException e) { -// // 如果无法获取输入流,则回退到默认内容类型 -// contentType = "application/octet-stream"; -// } -// -// response.setContentType(contentType); -// response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\""); -// -// try (InputStream inputStream = resource.getInputStream(); -// OutputStream outputStream = response.getOutputStream()) { -// -// byte[] buffer = new byte[4096]; -// int bytesRead = -1; -// while ((bytesRead = inputStream.read(buffer)) != -1) { -// outputStream.write(buffer, 0, bytesRead); -// } -// outputStream.flush(); -// } -// } else { -// response.setStatus(HttpServletResponse.SC_NOT_FOUND); -// } -// } - - @ApiOperation("二维码校验文件") - @GetMapping("/qrcode/{filename:.+}") - public ResponseEntity getFile(@PathVariable String filename) { - if (StringUtils.hasText(filename)) { - String context = FILE_CONTEXT.get(filename); - if (StringUtils.hasText(context)) return ResponseEntity.ok(context); - } - Resource resource = new ClassPathResource("static/" + filename); - if (resource.exists() || resource.isReadable()) { - - try { - Path path = resource.getFile().toPath(); - String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); - FILE_CONTEXT.put(filename, content); - return ResponseEntity.ok(content); - } catch (IOException e) { - e.printStackTrace(); - log.error("读取校验文件失败: {}", e.getMessage()); - } - } - return ResponseEntity.notFound().build(); - } - - -} diff --git a/qrcode/7Hmf2EbbCc.txt b/qrcode/7Hmf2EbbCc.txt new file mode 100644 index 0000000..a8afb47 --- /dev/null +++ b/qrcode/7Hmf2EbbCc.txt @@ -0,0 +1 @@ +175babf4957dd6090b5b36b78cd51c78 \ No newline at end of file