|
|
@@ -0,0 +1,108 @@
|
|
|
+package com.inspur.smsb.gateway.filter;
|
|
|
+
|
|
|
+import com.alibaba.cola.dto.Response;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.inspur.smsb.gateway.dto.license.LicenseDto;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
+import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
+import org.springframework.core.Ordered;
|
|
|
+import org.springframework.core.io.buffer.DataBufferFactory;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.*;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author zengweijie
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2022/7/27 11:20
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class LicenseFilter implements GlobalFilter, Ordered {
|
|
|
+ @Resource
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+ private final AtomicBoolean sysLocked = new AtomicBoolean(false);
|
|
|
+ private final AtomicBoolean timeRollBack = new AtomicBoolean(false);
|
|
|
+ private static final int DEFAULT_DURATION = 60;
|
|
|
+ private static final int STEP = 5;
|
|
|
+ @Value("${license-dir}")
|
|
|
+ private String licenseDir = "D:\\workStation\\工作文件\\测试";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
+ if (sysLocked.get() || timeRollBack.get()) {
|
|
|
+ ServerHttpResponse response = exchange.getResponse();
|
|
|
+ response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
|
+ response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ String msg = sysLocked.get() ? "系统已锁定" : "发现时间回退,系统已锁定";
|
|
|
+ return response.writeWith(Mono.fromSupplier(() -> {
|
|
|
+ DataBufferFactory bufferFactory = response.bufferFactory();
|
|
|
+ try {
|
|
|
+ return bufferFactory.wrap(objectMapper.writeValueAsBytes(Response.buildFailure("401", msg)));
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ log.error("Error writing response", e);
|
|
|
+ return bufferFactory.wrap(new byte[0]);
|
|
|
+ }
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ return chain.filter(exchange);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES)
|
|
|
+ public void checkSysLocked() throws IOException {
|
|
|
+ if (sysLocked.get() || timeRollBack.get()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File directory = new File(licenseDir);
|
|
|
+ if (!directory.exists() && !directory.mkdirs()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File licenseFile = new File(directory.getAbsolutePath() + File.separator + "license.json");
|
|
|
+ if (!licenseFile.exists()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LicenseDto licenseDto = new LicenseDto();
|
|
|
+ try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(licenseFile))) {
|
|
|
+ if (inputStream.available() > 0) {
|
|
|
+ licenseDto = objectMapper.readValue(inputStream.readAllBytes(), LicenseDto.class);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ if (Objects.nonNull(licenseDto.getLastUpdate()) && licenseDto.getLastUpdate().isAfter(now)) {
|
|
|
+ timeRollBack.set(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int duration = Optional.ofNullable(licenseDto.getDuration()).orElse(DEFAULT_DURATION);
|
|
|
+ int account = Optional.ofNullable(licenseDto.getAccount()).orElse(0);
|
|
|
+ if (duration <= account) {
|
|
|
+ sysLocked.set(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ licenseDto.setDuration(duration);
|
|
|
+ licenseDto.setAccount(account + STEP);
|
|
|
+ licenseDto.setLastUpdate(now);
|
|
|
+ try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(licenseFile))) {
|
|
|
+ outputStream.write(objectMapper.writeValueAsBytes(licenseDto));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getOrder() {
|
|
|
+ return HIGHEST_PRECEDENCE;
|
|
|
+ }
|
|
|
+}
|