计数器的基本使用
/** * redis计数器 * @param key 键 * @return */ public Long test(String key){
//key为key,delta为递增数目,返回值为增长后的数量。 Long increment =
redisTemplate.opsForValue().increment(key, 1); return increment; }
 

 短信发送请求次数限制
/** * 短信验证码请求次数限制 * @param mobile * @return */ public String
sendVerifyCode(String mobile){ Long increment =
redisTemplate.opsForValue().increment(mobile, 1); if (increment == 1) {
//设置有效期一分钟 redisTemplate.expire(mobile, 60, TimeUnit.SECONDS); } if (increment
> 1) { return "每分钟只能发送一次短信"; } //发送验证码 return "验证码已发送"; }
IP请求次数限制

定义消息发送类
/** * 拦截器消息发送 * @author 向振华 * @date 2018/11/28 10:52 */ public class
BaseInterceptor extends HandlerInterceptorAdapter { /** * 发送异常信息 * @param
request * @param response * @param code * @param msg * @param data */ public
void sendMsg(HttpServletRequest request, HttpServletResponse response, int
code, String msg, String data) { response.setCharacterEncoding("UTF-8");
response.setContentType("application/json"); //封装成返回给前端的固定格式 ResponseMessage
responseMessage = new ResponseMessage(code, msg, data); String jsonObject =
JSONObject.toJSONString(responseMessage); PrintWriter out = null; try { out =
response.getWriter(); out.append(jsonObject); } catch (IOException e) {
e.printStackTrace(); } finally { if (out != null) { out.close(); } } } } @Data
public class ResponseMessage<T> implements Serializable { private static final
long serialVersionUID = -2988364810346364595L; /** * 1:成功 -1:失败 */ private int
code; /** * 错误信息 */ private String errorMsg; /** * 返回数据 */ private T data;
public ResponseMessage(int code, String errorMsg, T data) { this.code = code;
this.errorMsg = errorMsg; this.data = data; } public ResponseMessage(int code,
String errorMsg) { this.code = code; this.errorMsg = errorMsg; } public
ResponseMessage() { } }
拦截器编写
/** * IP请求次数限制 * @author 向振华 * @date 2018/11/28 10:55 */ public class
CounterInterceptor extends BaseInterceptor { @Resource private RedisTemplate
redisTemplate; @Override public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception { String ip =
getRemoteHost(request); String key = ip + request.getRequestURI() +
request.getSession().getId(); Long increment =
redisTemplate.opsForValue().increment(key, 1); if (increment == 1) { //设置有效期一分钟
redisTemplate.expire(key, 60, TimeUnit.SECONDS); } //次数限制为5次 if (increment > 5)
{ sendMsg(request, response, -1, "次数限制", request.getRequestURI()); return
false; } return true; } /** * 获取请求IP地址 * @param request * @return */ public
String getRemoteHost(HttpServletRequest request) { String ip =
request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 ||
"unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); }
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip =
request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0
|| "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return
ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip; } }
拦截器配置
/** * 拦截器配置 * @author 向振华 * @date 2018/11/28 11:30 */ @Configuration
@EnableTransactionManagement public class MyWebAppConfig extends
WebMvcConfigurerAdapter { @Bean CounterInterceptor counterInterceptor() {
return new CounterInterceptor(); } /** * 拦截器配置 * @param registry */ @Override
public void addInterceptors(InterceptorRegistry registry) { //配置IP请求次数限制
registry.addInterceptor(counterInterceptor()); super.addInterceptors(registry);
} }
 

技术
©2019-2020 Toolsou All rights reserved,
hive大量小文件处理方法总结最优化方法总结:公式解、数值优化、求解思想JavaScript中的 Call 和 Apply内存溢出和内存泄漏的区别、产生原因以及解决方案创建数据mysql库流程微信小程序(uni-app)url参数传递对象蝗灾虫群上亿只很少发生碰撞 蝗虫要成自动驾驶功臣vue 监听 Treeselect 选择项的改变第十一届蓝桥杯C/C++ 大学 B 组大赛软件类省赛服务器价格有什么差异?