123
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package com.ruoyi.cai.controller;
|
package com.ruoyi.web.controller.cai.admin;
|
||||||
|
|
||||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.ruoyi.cai.controller;
|
package com.ruoyi.web.controller.cai.admin;
|
||||||
|
|
||||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
@@ -45,7 +45,7 @@ server:
|
|||||||
# 日志配置
|
# 日志配置
|
||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
com.ruoyi: @logging.level@
|
com.ruoyi: info
|
||||||
org.springframework: warn
|
org.springframework: warn
|
||||||
config: classpath:logback-plus.xml
|
config: classpath:logback-plus.xml
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ spring:
|
|||||||
# 国际化资源文件路径
|
# 国际化资源文件路径
|
||||||
basename: i18n/messages
|
basename: i18n/messages
|
||||||
profiles:
|
profiles:
|
||||||
active: @profiles.active@
|
active: dev
|
||||||
# 文件上传
|
# 文件上传
|
||||||
servlet:
|
servlet:
|
||||||
multipart:
|
multipart:
|
||||||
@@ -240,7 +240,8 @@ lock4j:
|
|||||||
acquire-timeout: 3000
|
acquire-timeout: 3000
|
||||||
# 分布式锁的超时时间,默认为 30 秒
|
# 分布式锁的超时时间,默认为 30 秒
|
||||||
expire: 30000
|
expire: 30000
|
||||||
|
cai:
|
||||||
|
enable-api-encryption: false
|
||||||
--- # Actuator 监控端点的配置项
|
--- # Actuator 监控端点的配置项
|
||||||
management:
|
management:
|
||||||
endpoints:
|
endpoints:
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ public class CaiProperties {
|
|||||||
|
|
||||||
private String homeName = "蜜瓜";
|
private String homeName = "蜜瓜";
|
||||||
private String coinName = "紫贝";
|
private String coinName = "紫贝";
|
||||||
|
private boolean enableApiEncryption = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package com.ruoyi.cai.filter;
|
||||||
|
|
||||||
|
import com.ruoyi.cai.config.CaiProperties;
|
||||||
|
import com.ruoyi.cai.util.AES;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
|
||||||
|
import javax.servlet.*;
|
||||||
|
import javax.servlet.annotation.WebFilter;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
@WebFilter(urlPatterns = "/api/**", filterName = "encryptionFilter")
|
||||||
|
public class EncryptionFilter implements Filter {
|
||||||
|
private static final Set<String> IGNORE_URL = new HashSet<>();
|
||||||
|
static {
|
||||||
|
IGNORE_URL.add("/api/ali/notify");
|
||||||
|
IGNORE_URL.add("/api/wx/notify");
|
||||||
|
}
|
||||||
|
private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();
|
||||||
|
|
||||||
|
private static final String KEY = "UCEfSKns45SWjHov";
|
||||||
|
@Autowired
|
||||||
|
private CaiProperties caiProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||||
|
if(!(servletRequest instanceof HttpServletRequest)){
|
||||||
|
filterChain.doFilter(servletRequest, servletResponse);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!caiProperties.isEnableApiEncryption()){
|
||||||
|
filterChain.doFilter(servletRequest, servletResponse);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
||||||
|
HttpServletResponse originalResponse = (HttpServletResponse) servletResponse;
|
||||||
|
String requestURI = request.getRequestURI();
|
||||||
|
for (String ignoreUrlMatch : IGNORE_URL) {
|
||||||
|
boolean match = ANT_PATH_MATCHER.match(ignoreUrlMatch, requestURI);
|
||||||
|
if(match){
|
||||||
|
filterChain.doFilter(servletRequest, servletResponse);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse) servletResponse);
|
||||||
|
filterChain.doFilter(request, responseWrapper);
|
||||||
|
byte[] resData = responseWrapper.getContent();
|
||||||
|
if(resData.length > 0){
|
||||||
|
try {
|
||||||
|
byte[] encrypt = AES.encrypt(resData, KEY);
|
||||||
|
originalResponse.setContentLength(encrypt.length);
|
||||||
|
ServletOutputStream outputStream = originalResponse.getOutputStream();
|
||||||
|
outputStream.write(encrypt);
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package com.ruoyi.cai.filter;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.ServletOutputStream;
|
||||||
|
import javax.servlet.WriteListener;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletResponseWrapper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回值输出代理类
|
||||||
|
*
|
||||||
|
* @author kokJuis
|
||||||
|
* @Title: ResponseWrapper
|
||||||
|
* @Description:
|
||||||
|
* @date 上午9:52:11
|
||||||
|
*/
|
||||||
|
public class ResponseWrapper extends HttpServletResponseWrapper {
|
||||||
|
|
||||||
|
private ByteArrayOutputStream buffer;
|
||||||
|
|
||||||
|
private ServletOutputStream out;
|
||||||
|
|
||||||
|
public ResponseWrapper(HttpServletResponse httpServletResponse) {
|
||||||
|
super(httpServletResponse);
|
||||||
|
buffer = new ByteArrayOutputStream();
|
||||||
|
out = new WrapperOutputStream(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletOutputStream getOutputStream()
|
||||||
|
throws IOException {
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flushBuffer()
|
||||||
|
throws IOException {
|
||||||
|
if (out != null) {
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getContent()
|
||||||
|
throws IOException {
|
||||||
|
flushBuffer();
|
||||||
|
return buffer.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
class WrapperOutputStream extends ServletOutputStream {
|
||||||
|
private ByteArrayOutputStream bos;
|
||||||
|
|
||||||
|
public WrapperOutputStream(ByteArrayOutputStream bos) {
|
||||||
|
this.bos = bos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(int b) {
|
||||||
|
bos.write(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isReady() {
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setWriteListener(WriteListener arg0) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,13 +8,19 @@ import java.util.Base64;
|
|||||||
public class AES {
|
public class AES {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
String jsonData ="zTmtrzE4t6jFriRIde3X+AFG9PQPSTQb5GrZcsBZySFUne3NGpRbb19LUbcKKLaRXj31TSE6nrVz3jLD7nCH+ArJzZwEgMb3hLXZPh53Xluk1Rkq/8wUMsDhAWjPRG012dhoaINUggAJwDKvKDqRgA==";
|
String jsonData ="YRdHX2hqWMGguxfvSXfHzK5kuljBah5luDUo/YcmoQVKtSSAtEY2ndScZIi2dgLvjJ6noUDcIctq653wt83OCgj3P4+jgnopMps12RY1DnxHtLUd2Rnv3TCCKf+Br5+iGi9ruuq27JimqOYOjT+K8CWVESSQsoTN6knQA69s2kctSaHkmpirf4N3gJxp84BW4xi+hyayuKFe5/jva0X/Cg==";
|
||||||
// String jsonData = "123";
|
String key = "UCEfSKns45SWjHov";
|
||||||
String key = "K2AwvosrwtoAgOEP";
|
|
||||||
String encod = encrypt(jsonData, key);
|
|
||||||
System.out.println(decrypt(jsonData,key));
|
System.out.println(decrypt(jsonData,key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] encrypt(byte[] data, String key) throws Exception {
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||||
|
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
|
||||||
|
byte[] encryptedBytes = cipher.doFinal(data);
|
||||||
|
return Base64.getEncoder().encode(encryptedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
public static String encrypt(String data, String key) throws Exception {
|
public static String encrypt(String data, String key) throws Exception {
|
||||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
|
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
|
||||||
|
|||||||
Reference in New Issue
Block a user