接口uu快3软件/UU快三软件【致富彩经】 验证

1.ip黑名单过滤

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 描述:黑名单过滤
 *
 * @author ZMY
 * @date 2018/11/2711:01
 */
@Component
@Order(1)
@Slf4j
public class IpFilter extends OncePerRequestFilter {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String IP_BLACK_LIST_KEY = "ip_black_list_%s";
  	
  	private static String[] IP_HEADS = new String[]{"x-forwarded-for", "proxy-client-ip",
      "wl-proxy-client-ip",
      "http_client_ip", "http_x_forwarded_for",};

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // 获取IP地址
        String ip = WebUtils.getRemoteAddr(request);

        log.info("请求的ip:{}"+ip);
        String key = String.format(IP_BLACK_LIST_KEY,ip);
        boolean flag = this.stringRedisTemplate.hasKey(key);
        if(flag){
            // 将相关的错误返回给客户端
            WebUtils.write(response,BasicResult.fail(10001,
                    "ip存在黑名单中"));
            return;
        }
        filterChain.doFilter(request,response);
    }

}

2.防止重复提交

  • 可以在请求接口的时候,要求每次都带上时间戳和uuid,请求的时候先去redis查询是否存在该uuid的key。
        if (this.stringRedisTemplate.hasKey(uuid)){

        }
  • 如果不存在再去处理后面的逻辑,最后将该uuid作为key存到redis(根据自己的业务需要设置有效时间),我这里设置的是5分钟。
  • 如果请求接口的时候,该uuid已经存在redis,那么这次请求就是重复的。
  • 服务的接到的时间戳应该拿来跟当前时间对比,如果相差大于一定的时间(5分钟,跟上面的redis的key有效时间一样),就可以拒绝这次请求。

3.带token的请求

。。。

这是工具类

import com.google.common.collect.Maps;
import com.zhuguang.lession8.model.BasicResult;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;

/**
 * @author Hikaru
 * Created  on 2018/5/27.
 */

@Slf4j
public class WebUtils {


  private static String[] IP_HEADS = new String[]{"x-forwarded-for", "proxy-client-ip",
      "wl-proxy-client-ip",
      "http_client_ip", "http_x_forwarded_for",};

  public static String getRemoteAddr(HttpServletRequest request) {
    for (String header : IP_HEADS) {
      String ip = request.getHeader(header);
      if (ip != null && ip.length() > 0 && !"unknown".equalsIgnoreCase(ip)) {
        if (ip.indexOf(",") > -1) {
          ip = ip.substring(0, ip.indexOf(","));
        }
        return ip;
      }
    }
    return request.getRemoteAddr();
  }


  public static void write(HttpServletResponse response, BasicResult basicResult)
      throws IOException {
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json; charset=utf-8");
    PrintWriter writer = null;
    OutputStreamWriter osw = null;
    try {
      osw = new OutputStreamWriter(response.getOutputStream(),
          "UTF-8");
      writer = new PrintWriter(osw, true);
      String jsonStr = GsonUtils.getGsonInstance().toJson(basicResult);
      writer.write(jsonStr);
      writer.flush();
      writer.close();

      osw.close();


    } catch (UnsupportedEncodingException e) {
      log.error("过滤器返回信息失败:" + e.getMessage(), e);
    } catch (IOException e) {
      log.error("过滤器返回信息失败:" + e.getMessage(), e);
    } finally {
      if (null != writer) {
        writer.close();
      }
      if (null != osw) {
        osw.close();
      }
    }

  }



  /**
   * 是否匹配配置路径
   */
  public static boolean match(HttpServletRequest request,List<String> paths) {
    String path = request.getRequestURI();
    boolean flag = false;
    for (String string : paths) {
      if (string.matches(path)) {
        flag = true;
        break;
      }
    }
    return flag;

  }


  /**
   * 拼接get参数和post参数列表
   */
  public static  String paramResult(HttpServletRequest request) {
    TreeMap<String, String> tree = Maps.newTreeMap();
    request.getParameterMap().forEach(
        (key, value) -> {
          if (ArrayUtils.isNotEmpty(value) && isNotBlankArray(value)) {
            tree.put(key, Arrays.toString(value));
          }
        }
    );
    StringBuilder sb = new StringBuilder();
    tree.forEach((key, value) -> {
      sb.append(key);
      sb.append("=");
      sb.append(value);
    });
    return sb.toString();
  }

  private static boolean isNotBlankArray(String[] value) {
    if (value.length == 1 && value[0] == "") {
      return false;
    }
    return true;
  }

}

展开阅读全文

没有更多推荐了,返回首页