微信域名被封检测_Java_SpringBoot

/**
微信域名检测
	方法一:使用 微信公众号中的短链接生成的Api 进行短链接生成,以Java为例子,
	使用okhttp,httpclient等工具访问生成的短链接,然后查看内部的html等或跳转信息即可得知是否被封
	
	方法二:以代码的方式模拟微信浏览器,链接使用微信浏览器访问会被显示被封,
	但使用外部浏览器访问并没有,即是微信内部的行为,模拟微信浏览器即可得知是否被封


	下述提供了核心方法 简陋版,只提供思路,后续可自行根据需求改造.
	
*/
package com.ichaomeng.wechat.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ichaomeng.config.properties.ConfigProperties;
import com.ichaomeng.utils.CacheManager;
import com.ichaomeng.utils.JacksonUtil;
import com.ichaomeng.wechat.api.WeChatApi;
import com.ichaomeng.wechat.pojo.AccessTokenResult;
import com.ichaomeng.wechat.unit.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/*
 * @author: Liao
 */
@Slf4j
@Service
public class WeChatService {

    @Resource
    ConfigProperties configProperties;

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 微信域名检测
     *  使用微信 Api 生成短链接 进行
     */
    public Boolean checkDomainByWeChatApi(String domain) throws IOException {

        //获取微信短连接
        Map<String,Object> shortResult = getShortUrl(domain);

        //判断短链接是否为空
        if (!Objects.isNull(shortResult.get("short_url"))) {

            //判断短链接中的 errcode 是否为0
            if (shortResult.get("errcode").equals(0)) {

                String shortUrl = (String) shortResult.get("short_url");

                String htmlDoc = OkHttpUtil.ping(shortUrl).body().string();

                if (htmlDoc.contains("cgiData")) {

                    String json = htmlDoc.substring(htmlDoc.indexOf("{"), htmlDoc.indexOf(";"));
                    Map result = JacksonUtil.readValue(json, Map.class);

                    if (result.get("retcode").equals("0")) {
                        return false;
                    } else {
                        return false;
                    }

                } else {
                    return true;
                }

            } else {
                log.error("shortResult.getString(errmsg): " + shortResult.get("errmsg"));
            }

        } else {
            log.error("请求错误");
        }

        return false;
    }

    /**
     * 获取AccessToken
     *  2h / 获取一次token
     */
    public AccessTokenResult getAccessToken() throws IOException {

        String access_token = CacheManager.get("access_token");
        String expires_in = CacheManager.get("expires_in");

        if (StringUtils.isNotBlank(CacheManager.get("access_token"))) {
            AccessTokenResult accessTokenResult = new AccessTokenResult();
            accessTokenResult.setAccess_token(access_token)
                                .setExpires_in(expires_in);
            log.info("当前使用的是保存下来的token");
            return accessTokenResult;
        } else {
            Map<String, Object> paramMaps = new HashMap<String, Object>();
            paramMaps.put("grant_type", "client_credential");
            paramMaps.put("appid", configProperties.getWeChat().getAppId());
            paramMaps.put("secret", configProperties.getWeChat().getAppSecret());

            ResponseBody body = OkHttpUtil.sendGetRequest(WeChatApi.GET_ACCESS_TOEKN, paramMaps).body();

            AccessTokenResult accessTokenResult = JacksonUtil.readValue(
                    Objects.requireNonNull(body).string(),
                    AccessTokenResult.class);

            String set_access_token = accessTokenResult.getAccess_token();
            String set_expires_in = accessTokenResult.getExpires_in();

            log.info("当前使用的是重新创建的token");

            CacheManager.set("access_token",set_access_token, 360000 * 2);
            CacheManager.set("expires_in",set_expires_in, 360000 * 2);

            return accessTokenResult;
        }

    }

    /**
     * 获取短链接
     */
    public Map getShortUrl(String domain) throws IOException {
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("action", "long2short");
        maps.put("long_url", domain);
        return JacksonUtil.readValue(
                OkHttpUtil.sendJsonRequest(WeChatApi.GET_SHORT_URL + getAccessToken().getAccess_token(), maps).body().string(), Map.class);
    }

}

你可能感兴趣的:(微信域名被封检测_Java_SpringBoot)