二维码验证

master
guochaojie 5 months ago
parent 968e290a94
commit 35602b9bf8

@ -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.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping; import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -49,6 +48,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
"/webjars/**", "/webjars/**",
"/static/**", "/static/**",
"/upload/**", "/upload/**",
"/qrcode/**.txt",
"/doc.html" "/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 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("/**").addResourceLocations("classpath:/META-INF/resources/","classpath:/META-INF/resources/webjars/");
// 定义资源访问路径 // 定义资源访问路径
registry.addResourceHandler("/upload/**").addResourceLocations("file:" + UPLOAD_PATH); registry.addResourceHandler("/upload/**").addResourceLocations("file:" + UPLOAD_PATH);
// 定义二维码相关的资源访问路径
registry.addResourceHandler("/api/qrcode/**.txt").addResourceLocations("file:" + QR_CODE_PATH);
} }

@ -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<String, String> 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<String> 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();
}
}

@ -0,0 +1 @@
175babf4957dd6090b5b36b78cd51c78
Loading…
Cancel
Save