技术经验分享:braum的使用

简介: 技术经验分享:braum的使用

添加依赖


me.zhyd.braum.spring.boot


braum-spring-boot-starter


1.0.0-alpha


相关配置


braum.limit.access.threshold=20# 连续访问最高阀值,超过该值则认定为恶意操作的IP。单位:次 默认为20


braum.limit.access.interval=5000# 间隔时间,在该时间内如果访问次数大于阀值,则记录为恶意IP,否则视为正常访问。单位:毫秒(ms),默认为 5秒


braum.limit.access.limitedTime=60000# 当检测到恶意访问时,对恶意访问的ip进行限制的时间。单位:毫秒(ms),默认为 1分钟


braum.limit.access.blacklistTime=2592000000# 黑名单存在的时间,在单位时间内用户访问受限的次数累加。单位:毫秒(ms),默认为 1个月


braum.limit.access.type=map# 缓存类型,默认为map存储,可选值(map、redis)


开启Braum


在启动类上添加@EnableBraumConfiguration注解


import me.zhyd.braum.spring.boot.annotation.EnableBraumConfiguration;


@SpringBootApplication


@EnableBraumConfiguration


public class BraumApplication {


public static void main(String【】 args) {


SpringApplication.run(BraumApplication.class, args);


}


}


在controller中处理


@RestController


public class BraumController {


@Autowired


BraumProcessor processor;


@Autowired


HttpServletRequest request;


@RequestMapping("/")


public Object index() {


BraumResponse r = processor.process(request);


if (r.getCode() == CommonConst.ERROR) {


return "你已涉嫌恶意访问被临时禁止,请文明上网";


}


return "Hello world!";


}


}


在拦截器中使用


拦截器


@Component


public class BraumIntercepter implements HandlerInterceptor {


private static final Logger log = LoggerFactory.getLogger(BraumIntercepter.class);


private static final int SUCCESS = 1;


private static List msgList = new ArrayList();


static {


msgList.add("Wow...您太冲动了,先喝杯咖啡冷静下。");


msgList.add("Wow...一杯不够?那再来一杯。");


msgList.add("还不够?再来一杯!");


msgList.add("你就不怕被撑死么?");


msgList.add("古恩吧,不接你这种客了");


msgList.add("古恩!");


}


@Autowired


private BraumProcessor processor;


@Override


public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


BraumResponse r = processor.process(request);


if (r.getCode() == SUCCESS) {


return true;


}


String msg = r.getLimitCount() > msgList.size() ? msgList.get(msgList.size() - 1) : msgList.get(r.getLimitCount() - 1);


log.info(msg);


response.setCharacterEncoding("UTF-8");


response.setContentType("text/html;charset=utf-8");


PrintWriter writer = response.getWriter();


writer.write(msg);


writer.flush();


writer.close();


return false;


}


}

//代码效果参考: http://www.lyjsj.net.cn/wz/art_23040.html


返回说明


字段释义


code


响应码(1:正常,0:受限制)


msg


返回内容


expire


当请求被限制时该值不为0,表示为被限制的剩余时间,单位毫秒


limitCount


当请求被限制时该值不为0,表示为被限制的次数


accessInfo


本次发起请求的内容


accessInfo字段


字段释义


ip


当前访问IP


ua


当前用户的UA


referer


请求来源


requestUrl


当前请求的地址


params


当前请求的参数

相关文章
|
3天前
|
Web App开发 人工智能 Java
技术经验分享:affineCipherandafineHacker
技术经验分享:affineCipherandafineHacker
10 2
|
Java 程序员
收藏!阿里毕玄16篇文章,深度讲解Java开发、系统设计、职业发展
阿里毕玄结合自己的经历深度讲解Java开发、系统设计、职业发展等问题,快来一键收藏吧。
34677 1
|
4天前
技术经验分享:comparisonmethodviolates必现
技术经验分享:comparisonmethodviolates必现
技术经验分享:comparisonmethodviolates必现
|
4天前
|
应用服务中间件 PHP nginx
技术经验分享:fastcgimain
技术经验分享:fastcgimain
|
4天前
|
JavaScript
技术经验分享:javascriptvar的理解
技术经验分享:javascriptvar的理解
|
4天前
|
前端开发 JavaScript 容器
技术经验分享:CSSFIXEDpornjavhd
技术经验分享:CSSFIXEDpornjavhd
|
4天前
|
移动开发 HTML5
技术经验分享:CQUOJ9766ChillyWilly
技术经验分享:CQUOJ9766ChillyWilly
|
4天前
技术经验分享:Jacobi
程序技术好文:爬取梨视频网站详细过程
|
4天前
|
C++
技术经验分享:C++程序设计的技巧
技术经验分享:C++程序设计的技巧
|
4天前
技术经验分享:HLG1314火影忍者之~纲手
技术经验分享:HLG1314火影忍者之~纲手