长春南关网站建设,jsp写的网站,网页制作基础教程期末考试,wordpress_ joomla_ drupal背景
在实际开发过程中#xff0c;防重复提交的操作很常见。有细分配置针对某一些路径进行拦截#xff0c;也有基于注解去实现的指定方法拦截的。
分析
实现原理
实现防重复提交#xff0c;我们很容易想到就是用过滤器或者拦截器来实现。
使用拦截器就是继承HandlerInt…背景
在实际开发过程中防重复提交的操作很常见。有细分配置针对某一些路径进行拦截也有基于注解去实现的指定方法拦截的。
分析
实现原理
实现防重复提交我们很容易想到就是用过滤器或者拦截器来实现。
使用拦截器就是继承HandlerInterceptorAdapter类实现preHandle()方法
使用过滤器就是实现OncePerRequestFilter接口在doFilterInternal()完成对应的防重复提交操作。 OncePerRequestFilter接口详解 在Spring Web应用程序中过滤器(Filter)也是一种拦截HTTP请求和响应的机制可以对它们进行处理或修改从而增强或限制应用程序的功能。OncePerRequestFilter类是Spring提供的一个抽象类继承自javax.servlet.Filter类并实现了Spring自己的过滤器接口OncePerRequestFilter它的目的是确保过滤器只会在每个请求中被执行一次从而避免重复执行过滤器逻辑所带来的问题如重复添加响应头信息等。 OncePerRequestFilter类中有一个doFilterInternal()方法用于实现过滤器的逻辑该方法只会在第一次请求时被调用之后不再执行确保了过滤器只会在每个请求中被执行一次。 实际场景考虑
使用过滤器的话会对所有的请求都进行防重复提交。但对于一些查询接口来说并不需要防重复提交。那么怎样在指定的接口需要使用防重复提交拦截呢答案就是用注解。
实现步骤
1.定义注解DuplicateSubmission
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
public interface DuplicateSubmission {
}
2.DuplicateSubmissionFilter 实现防重复提交
方法一基于过滤器与session
public class DuplicateSubmissionFilter extends OncePerRequestFilter {private final Logger LOGGER LoggerFactory.getLogger(DuplicateSubmissionFilter.class);Value(app.duplicateSubmission.time)/** 两次访问间隔时间 单位毫秒 */private long intervalTime;Autowiredprivate HttpSession session;Autowiredprivate HandlerMapping handlerMapping;Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {HandlerMethod handlerMethod getHandlerMethod(request);if (handlerMethod ! null handlerMethod.getMethodAnnotation(DuplicateSubmission.class) ! null) {// 这里的token不一定是要用户标识如果是设备之类也行能有唯一性就好String token request.getHeader(token);// 这里存到session中也可以用redis改造if (token null) {LOGGER.warn(token为空);}String key token request.getRequestURI();long nowTime System.currentTimeMillis();Object sessionObj session.getAttribute(key);if (sessionObj ! null) {long lastTime (long) sessionObj;session.setAttribute(key, nowTime);// 两次访问的时间小于规定的间隔时间if (intervalTime (nowTime - lastTime)) {LOGGER.warn(重复提交);return;}}}filterChain.doFilter(request, response);}private HandlerMethod getHandlerMethod(HttpServletRequest request) throws NoSuchMethodException {HandlerExecutionChain handlerChain null;try {handlerChain handlerMapping.getHandler(request);} catch (Exception e) {LOGGER.error(Failed to get HandlerExecutionChain., e);}if (handlerChain null) {return null;}Object handler handlerChain.getHandler();if (!(handler instanceof HandlerMethod)) {return null;}return (HandlerMethod) handler;}
}
但其实这个方案还需要考虑一个场景如果设置的防重复提交时间间隔小用户体验不会有什么奇怪。如果设置了1分钟以上那我们要考虑完善这个方案防重复提交还有一个重要的判断依据就是参数相同。**当时间小于间隔时间且参数相同时认定为重复提交。**这一步也没什么复杂就只是建一个map把请求时间和参数放进map再保存到 session中。
方式二
基于拦截器与redis实现使用拦截器记得要在你的WebMvcConfigurer实现类上注册
Component
Slf4j
public class DuplicateSubmissionInterceptor implements HandlerInterceptor {Value(app.duplicateSubmission.time)/** 两次访问间隔时间 单位毫秒 */private long intervalTime;Autowiredprivate StringRedisTemplate redisTemplate;Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {if (handler instanceof HandlerMethod) {HandlerMethod handlerMethod (HandlerMethod) handler;Method method handlerMethod.getMethod();DuplicateSubmission annotation method.getAnnotation(DuplicateSubmission.class);// 使用了注解if (annotation ! null) {// 获取key值String key token request.getRequestURI();// 直接用redis的setIfAbsentboolean firstRequest redisTemplate.opsForValue().setIfAbsent(key, flag, intervalTime, TimeUnit.SECONDS);// 如果设置不成功那就是重复提交if (!firstRequest) {log.warn(重复提交);// 通常来说这里还有抛个全局处理异常return;}}}return true;}}
同样的如果设置的重复提交过长则需要把请求参数放到redis的value值中上面只是用了flag作为一个假的值对比请求参数是否一致。
使用方式
使用方法很简单只需要在需要进行防重复提交的方法上加上一个注解即可
PostMapping(/submit)
DuplicateSubmission
public String submitForm() {// 处理表单提交请求// ...return result;
}