parent
62d212a9e9
commit
2ec4ef2d67
@ -0,0 +1,24 @@
|
||||
package org.opsli.common.annotation.limiter;
|
||||
|
||||
|
||||
import org.opsli.common.enums.AlertType;
|
||||
import org.opsli.common.utils.RateLimiterUtil;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Java 限流器
|
||||
* @author Parker
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Limiter {
|
||||
|
||||
/** 标题 */
|
||||
double qps() default RateLimiterUtil.DEFAULT_QPS;
|
||||
|
||||
/** 提醒方式 */
|
||||
AlertType alertType() default AlertType.JSON;
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package org.opsli.common.enums;
|
||||
|
||||
|
||||
/**
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-17 23:40
|
||||
* @Description: 字典
|
||||
*/
|
||||
public enum AlertType {
|
||||
|
||||
/** alert 弹出 */
|
||||
ALERT(),
|
||||
|
||||
/** JSON 回推*/
|
||||
JSON,
|
||||
;
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package org.opsli.common.utils;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.opsli.common.exception.ServiceException;
|
||||
import org.opsli.common.msg.CommonMsg;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @BelongsProject: think-bboss-parent
|
||||
* @BelongsPackage: com.think.bboss.common.utils
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2021-01-05 14:26
|
||||
* @Description: 周鹏程
|
||||
*/
|
||||
@Slf4j
|
||||
public final class OutputStreamUtil {
|
||||
|
||||
/**
|
||||
* 导出文件时为Writer生成OutputStream
|
||||
*/
|
||||
public static OutputStream getOutputStream(String fileName, HttpServletResponse response)
|
||||
throws ServiceException {
|
||||
try {
|
||||
fileName = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
|
||||
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
|
||||
return response.getOutputStream();
|
||||
} catch (IOException e) {
|
||||
// 创建文件失败
|
||||
throw new ServiceException(CommonMsg.EXCEPTION_CREATE_FILE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回异常值
|
||||
*/
|
||||
public static void exceptionResponse(String msg, HttpServletResponse response){
|
||||
try {
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("text/html;charset=utf-8;");
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write(
|
||||
"<script type=\"text/javascript\">alert('"+msg+"');</script>");
|
||||
writer.flush();
|
||||
// 关闭流
|
||||
IoUtil.close(writer);
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ==========================
|
||||
|
||||
private OutputStreamUtil(){}
|
||||
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.opsli.core.aspect;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.opsli.common.annotation.limiter.Limiter;
|
||||
import org.opsli.common.enums.AlertType;
|
||||
import org.opsli.common.exception.ServiceException;
|
||||
import org.opsli.common.utils.OutputStreamUtil;
|
||||
import org.opsli.common.utils.RateLimiterUtil;
|
||||
import org.opsli.core.msg.CoreMsg;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.opsli.common.constants.OrderConstants.LIMITER_AOP_SORT;
|
||||
/**
|
||||
* 限流器
|
||||
*
|
||||
* @author 周鹏程
|
||||
* @date 2020-09-16
|
||||
*/
|
||||
@Slf4j
|
||||
@Order(LIMITER_AOP_SORT)
|
||||
@Aspect
|
||||
@Component
|
||||
public class LimiterAop {
|
||||
|
||||
|
||||
@Pointcut("@annotation(org.opsli.common.annotation.limiter.Limiter)")
|
||||
public void requestMapping() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 限流
|
||||
* @param point
|
||||
*/
|
||||
@Before("requestMapping()")
|
||||
public void limiterHandle(JoinPoint point){
|
||||
try {
|
||||
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
|
||||
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
|
||||
MethodSignature signature = (MethodSignature) point.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
if(sra != null) {
|
||||
HttpServletRequest request = sra.getRequest();
|
||||
HttpServletResponse response = sra.getResponse();
|
||||
Limiter limiter = method.getAnnotation(Limiter.class);
|
||||
if(limiter != null){
|
||||
AlertType alertType = limiter.alertType();
|
||||
double qps = limiter.qps();
|
||||
|
||||
// 限流
|
||||
boolean enterFlag = RateLimiterUtil.enter(request, qps);
|
||||
if(!enterFlag){
|
||||
// alert 弹出
|
||||
if(AlertType.ALERT == alertType){
|
||||
OutputStreamUtil.exceptionResponse(
|
||||
CoreMsg.OTHER_EXCEPTION_LIMITER.getMessage(),
|
||||
response
|
||||
);
|
||||
}else {
|
||||
// 异常返回
|
||||
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_LIMITER);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch (ServiceException e){
|
||||
throw e;
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue