zoukankan      html  css  js  c++  java
  • 极验验证(滑动验证)的使用

    一、样例

    在这里插入图片描述

    二、注册账号

    极验验证官网
    在这里插入图片描述

    在表单里面填写真实信息,客服会在 24h 联系你进行审核,审核通过后,会发送这样的邮件到你的邮箱。
    在这里插入图片描述
    点击邮箱里面提供的注册地址即可完成注册。 注册成功后,进行登录:
    在这里插入图片描述

    三、获取ID

    在这里插入图片描述
    在这里插入图片描述
    填写基本信息:
    在这里插入图片描述
    创建成功后:
    在这里插入图片描述
    点击 ID:
    在这里插入图片描述
    点击查看部署指引:
    在这里插入图片描述
    都勾选成功后,点击确认:
    在这里插入图片描述
    获取到了 ID 和 KEY:
    在这里插入图片描述
    记录 ID 和 KEY:

    ID 				上面获得到的id
    KEY 			上面获取到的value
    
    • 1
    • 2

    四、极验官方文档(参考)

    文档地址:
    https://docs.geetest.com/sensebot/start/
    服务端:
    https://docs.geetest.com/sensebot/deploy/server/java#%E9%85%8D%E7%BD %AE%E5%AF%86%E9%92%A5
    Web 端:
    https://docs.geetest.com/sensebot/deploy/client/web

    五、SpringBoot集成极验

    5.1、maven依赖(可能有些需要自己去导,个人的包依赖太多不好全部放上来,核心就这两个)

       <!--json的依赖-->
    	<dependency>
    		<groupId>org.json</groupId>
    		<artifactId>json</artifactId>
    		<version>20171018</version>
    	</dependency>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-data-redis</artifactId>
    	</dependency>
    	<dependency>
    		<groupId>org.projectlombok</groupId>
    		<artifactId>lombok</artifactId>
    		<version>1.18.12</version>
    	</dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    5.2、yml配置文件

    spring:
    	 redis:
            host: redis-server		#自己redis的ip,此处我做了ip映射
            port: 6380
            password: 123456
    geetest:
        geetest-id: 上面获得到的id
        geetest-key: 上面获取到的value
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5.3、util类(读本地ip)

    package com.zhz.util;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * 获取本地ip地址
     */
    public class IpUtil {
    
        public static String getIpAddr(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("Proxy-Client-IP");
    
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_CLIENT_IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_X_CONNECTING_IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getRemoteAddr();
                }
            } else if (ip.length() > 15) {
                String[] ips = ip.split(",");
                for (int index = 0; index < ips.length; index++) {
                    String strIp = (String) ips[index];
                    if (!("unknown".equalsIgnoreCase(strIp))) {
                        ip = strIp;
                        break;
                    }
                }
            }
            return ip;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    /**
     * @(#)Result.JAVA, 2020年05月22日.
     * <p>
     * Copyright 2020 GEETEST, Inc. All rights reserved.
     * GEETEST PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */
    package com.zhz.geetest;
    
    import org.json.JSONObject;
    
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.InputStream;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.security.MessageDigest;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.UUID;
    
    /**
     * sdk lib包,核心逻辑。
     */
    public class GeetestLib {
    
        /**
         * 公钥
         */
        private String geetest_id;
    
        /**
         * 私钥
         */
        private String geetest_key;
    
        /**
         * 返回数据的封装对象
         */
        private GeetestLibResult libResult;
    
        /**
         * 调试开关,是否输出调试日志
         */
        private static final boolean IS_DEBUG = true;
    
        private static final String API_URL = "http://api.geetest.com";
    
        private static final String REGISTER_URL = "/register.php";
    
        private static final String VALIDATE_URL = "/validate.php";
    
        private static final String JSON_FORMAT = "1";
    
        private static final boolean NEW_CAPTCHA = true;
    
        private static final int HTTP_TIMEOUT_DEFAULT = 5000; // 单位:毫秒
    
        public static final String VERSION = "jave-servlet:3.1.0";
    
        /**
         * 极验二次验证表单传参字段 chllenge
         */
        public static final String GEETEST_CHALLENGE = "geetest_challenge";
    
        /**
         * 极验二次验证表单传参字段 validate
         */
        public static final String GEETEST_VALIDATE = "geetest_validate";
    
        /**
         * 极验二次验证表单传参字段 seccode
         */
        public static final String GEETEST_SECCODE = "geetest_seccode";
    
        /**
         * 极验验证API服务状态Session Key
         */
        public static final String GEETEST_SERVER_STATUS_SESSION_KEY = "gt_server_status";
    
        /**
         * 极验证里面的user
         */
        public static final String GEETEST_SERVER_USER_KEY = "gt_server_user";
    
        public GeetestLib(String geetest_id, String geetest_key) {
            this.geetest_id = geetest_id;
            this.geetest_key = geetest_key;
            this.libResult = new GeetestLibResult();
        }
    
        public void gtlog(String message) {
            if (this.IS_DEBUG) {
                System.out.println("gtlog: " + message);
            }
        }
    
        /**
         * 验证初始化
         */
        public GeetestLibResult register(String digestmod, Map<String, String> paramMap) {
            this.gtlog(String.format("register(): 开始验证初始化, digestmod=%s.", digestmod));
            String origin_challenge = this.requestRegister(paramMap);
            this.buildRegisterResult(origin_challenge, digestmod);
            this.gtlog(String.format("register(): 验证初始化, lib包返回信息=%s.", this.libResult));
            return this.libResult;
        }
    
        /**
         * 向极验发送验证初始化的请求,GET方式
         */
        private String requestRegister(Map<String, String> paramMap) {
            paramMap.put("gt", this.geetest_id);
            paramMap.put("json_format", this.JSON_FORMAT);
            paramMap.put("sdk", this.VERSION);
            String register_url = this.API_URL + this.REGISTER_URL;
            this.gtlog(String.format("requestRegister(): 验证初始化, 向极验发送请求, url=%s, params=%s.", register_url, paramMap));
            String origin_challenge = null;
            try {
                String resBody = this.httpGet(register_url, paramMap);
                this.gtlog(String.format("requestRegister(): 验证初始化, 与极验网络交互正常, 返回body=%s.", resBody));
                JSONObject jsonObject = new JSONObject(resBody);
                origin_challenge = jsonObject.getString("challenge");
            } catch (Exception e) {
                this.gtlog("requestRegister(): 验证初始化, 请求异常,后续流程走宕机模式, " + e.toString());
                origin_challenge = "";
            }
            return origin_challenge;
        }
    
        /**
         * 构建验证初始化接口返回数据
         */
        private void buildRegisterResult(String origin_challenge, String digestmod) {
            // origin_challenge为空或者值为0代表失败
            if (origin_challenge == null || origin_challenge.isEmpty() || "0".equals(origin_challenge)) {
                // 本地随机生成32位字符串
                String challenge = UUID.randomUUID().toString().replaceAll("-", "");
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("success", 0);
                jsonObject.put("gt", this.geetest_id);
                jsonObject.put("challenge", challenge);
                jsonObject.put("new_captcha", this.NEW_CAPTCHA);
                this.libResult.setAll(0, jsonObject.toString(), "请求极验register接口失败,后续流程走宕机模式");
            } else {
                String challenge = null;
                if ("md5".equals(digestmod)) {
                    challenge = this.md5_encode(origin_challenge + this.geetest_key);
                } else if ("sha256".equals(digestmod)) {
                    challenge = this.sha256_encode(origin_challenge + this.geetest_key);
                } else if ("hmac-sha256".equals(digestmod)) {
                    challenge = this.hmac_sha256_encode(origin_challenge, this.geetest_key);
                } else {
                    challenge = this.md5_encode(origin_challenge + this.geetest_key);
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("success", 1);
                jsonObject.put("gt", this.geetest_id);
                jsonObject.put("challenge", challenge);
                jsonObject.put("new_captcha", this.NEW_CAPTCHA);
                this.libResult.setAll(1, jsonObject.toString(), "");
            }
        }
    
        /**
         * 正常流程下(即验证初始化成功),二次验证
         */
        public GeetestLibResult successValidate(String challenge, String validate, String seccode, Map<String, String> paramMap) {
            this.gtlog(String.format("successValidate(): 开始二次验证 正常模式, challenge=%s, validate=%s, seccode=%s.", challenge, validate, seccode));
            if (!this.checkParam(challenge, validate, seccode)) {
                this.libResult.setAll(0, "", "正常模式,本地校验,参数challenge、validate、seccode不可为空");
            } else {
                String response_seccode = this.requestValidate(challenge, validate, seccode, paramMap);
                if (response_seccode == null || response_seccode.isEmpty()) {
                    this.libResult.setAll(0, "", "请求极验validate接口失败");
                } else if ("false".equals(response_seccode)) {
                    this.libResult.setAll(0, "", "极验二次验证不通过");
                } else {
                    this.libResult.setAll(1, "", "");
                }
            }
            this.gtlog(String.format("successValidate(): 二次验证 正常模式, lib包返回信息=%s.", this.libResult));
            return this.libResult;
        }
    
        /**
         * 异常流程下(即验证初始化失败,宕机模式),二次验证
         * 注意:由于是宕机模式,初衷是保证验证业务不会中断正常业务,所以此处只作简单的参数校验,可自行设计逻辑。
         */
        public GeetestLibResult failValidate(String challenge, String validate, String seccode) {
            this.gtlog(String.format("failValidate(): 开始二次验证 宕机模式, challenge=%s, validate=%s, seccode=%s.", challenge, validate, seccode));
            if (!this.checkParam(challenge, validate, seccode)) {
                this.libResult.setAll(0, "", "宕机模式,本地校验,参数challenge、validate、seccode不可为空.");
            } else {
                this.libResult.setAll(1, "", "");
            }
            this.gtlog(String.format("failValidate(): 二次验证 宕机模式, lib包返回信息=%s.", this.libResult));
            return this.libResult;
        }
    
        /**
         * 向极验发送二次验证的请求,POST方式
         */
        private String requestValidate(String challenge, String validate, String seccode, Map<String, String> paramMap) {
            paramMap.put("seccode", seccode);
            paramMap.put("json_format", this.JSON_FORMAT);
            paramMap.put("challenge", challenge);
            paramMap.put("sdk", this.VERSION);
            paramMap.put("captchaid", this.geetest_id);
            String validate_url = this.API_URL + this.VALIDATE_URL;
            this.gtlog(String.format("requestValidate(): 二次验证 正常模式, 向极验发送请求, url=%s, params=%s.", validate_url, paramMap));
            String response_seccode = null;
            try {
                String resBody = this.httpPost(validate_url, paramMap);
                this.gtlog(String.format("requestValidate(): 二次验证 正常模式, 与极验网络交互正常, 返回body=%s.", resBody));
                JSONObject jsonObject = new JSONObject(resBody);
                response_seccode = jsonObject.getString("seccode");
            } catch (Exception e) {
                this.gtlog("requestValidate(): 二次验证 正常模式, 请求异常, " + e.toString());
                response_seccode = "";
            }
            return response_seccode;
        }
    
        /**
         * 校验二次验证的三个参数,校验通过返回true,校验失败返回false
         */
        private boolean checkParam(String challenge, String validate, String seccode) {
            return !(challenge == null || challenge.trim().isEmpty() || validate == null || validate.trim().isEmpty() || seccode == null || seccode.trim().isEmpty());
        }
    
        /**
         * 发送GET请求,获取服务器返回结果
         */
        private String httpGet(String url, Map<String, String> paramMap) throws Exception {
            HttpURLConnection connection = null;
            InputStream inStream = null;
            try {
                Iterator<String> it = paramMap.keySet().iterator();
                StringBuilder paramStr = new StringBuilder();
                while (it.hasNext()) {
                    String key = it.next();
                    if (key == null || key.isEmpty() || paramMap.get(key) == null || paramMap.get(key).isEmpty()) {
                        continue;
                    }
                    paramStr.append("&").append(URLEncoder.encode(key, "utf-8")).append("=").append(URLEncoder.encode(paramMap.get(key), "utf-8"));
                }
                if (paramStr.length() != 0) {
                    paramStr.replace(0, 1, "?");
                }
                url += paramStr.toString();
                URL getUrl = new URL(url);
                connection = (HttpURLConnection) getUrl.openConnection();
                connection.setConnectTimeout(this.HTTP_TIMEOUT_DEFAULT); // 设置连接主机超时(单位:毫秒)
                connection.setReadTimeout(this.HTTP_TIMEOUT_DEFAULT); // 设置从主机读取数据超时(单位:毫秒)
                connection.connect();
                if (connection.getResponseCode() == 200) {
                    StringBuilder sb = new StringBuilder();
                    byte[] buf = new byte[1024];
                    inStream = connection.getInputStream();
                    for (int n; (n = inStream.read(buf)) != -1; ) {
                        sb.append(new String(buf, 0, n, "UTF-8"));
                    }
                    return sb.toString();
                }
                return "";
            } catch (Exception e) {
                throw e;
            } finally {
                if (inStream != null) {
                    inStream.close();
                }
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
    
        /**
         * 发送POST请求,获取服务器返回结果
         */
        private String httpPost(String url, Map<String, String> paramMap) throws Exception {
            HttpURLConnection connection = null;
            InputStream inStream = null;
            try {
                Iterator<String> it = paramMap.keySet().iterator();
                StringBuilder paramStr = new StringBuilder();
                while (it.hasNext()) {
                    String key = it.next();
                    if (key == null || key.isEmpty() || paramMap.get(key) == null || paramMap.get(key).isEmpty()) {
                        continue;
                    }
                    paramStr.append("&").append(URLEncoder.encode(key, "utf-8")).append("=").append(URLEncoder.encode(paramMap.get(key), "utf-8"));
                }
                if (paramStr.length() != 0) {
                    paramStr.replace(0, 1, "");
                }
                URL postUrl = new URL(url);
                connection = (HttpURLConnection) postUrl.openConnection();
                connection.setConnectTimeout(this.HTTP_TIMEOUT_DEFAULT);// 设置连接主机超时(单位:毫秒)
                connection.setReadTimeout(this.HTTP_TIMEOUT_DEFAULT);// 设置从主机读取数据超时(单位:毫秒)
                connection.setRequestMethod("POST");
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.connect();
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
                outputStreamWriter.write(paramStr.toString());
                outputStreamWriter.flush();
                outputStreamWriter.close();
                if (connection.getResponseCode() == 200) {
                    StringBuilder sb = new StringBuilder();
                    byte[] buf = new byte[1024];
                    inStream = connection.getInputStream();
                    for (int n; (n = inStream.read(buf)) != -1; ) {
                        sb.append(new String(buf, 0, n, "UTF-8"));
                    }
                    inStream.close();
                    connection.disconnect();
                    return sb.toString();
                }
                return "";
            } catch (Exception e) {
                throw e;
            } finally {
                if (inStream != null) {
                    inStream.close();
                }
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
    
        /**
         * md5 加密
         */
        private String md5_encode(String value) {
            String re_md5 = new String();
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(value.getBytes());
                byte b[] = md.digest();
                int i;
                StringBuilder sb = new StringBuilder("");
                for (int offset = 0; offset < b.length; offset++) {
                    i = b[offset];
                    if (i < 0)
                        i += 256;
                    if (i < 16)
                        sb.append("0");
                    sb.append(Integer.toHexString(i));
                }
                re_md5 = sb.toString();
            } catch (Exception e) {
                this.gtlog("md5_encode(): 发生异常, " + e.toString());
            }
            return re_md5;
        }
    
        /**
         * sha256加密
         */
        public String sha256_encode(String value) {
            MessageDigest messageDigest;
            String encodeStr = new String();
            try {
                messageDigest = MessageDigest.getInstance("SHA-256");
                messageDigest.update(value.getBytes("UTF-8"));
                encodeStr = byte2Hex(messageDigest.digest());
            } catch (Exception e) {
                this.gtlog("sha256_encode(): 发生异常, " + e.toString());
            }
            return encodeStr;
        }
    
        /**
         * 将byte转为16进制
         */
        private static String byte2Hex(byte[] bytes) {
            StringBuilder sb = new StringBuilder();
            String temp = null;
            for (int i = 0; i < bytes.length; i++) {
                temp = Integer.toHexString(bytes[i] & 0xFF);
                if (temp.length() == 1) {
                    // 得到一位的进行补0操作
                    sb.append("0");
                }
                sb.append(temp);
            }
            return sb.toString();
        }
    
        /**
         * hmac-sha256 加密
         */
        private String hmac_sha256_encode(String value, String key) {
            String encodeStr = new String();
            try {
                Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
                SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
                sha256_HMAC.init(secret_key);
                byte[] array = sha256_HMAC.doFinal(value.getBytes("UTF-8"));
                StringBuilder sb = new StringBuilder();
                for (byte item : array) {
                    sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
                }
                encodeStr = sb.toString();
            } catch (Exception e) {
                this.gtlog("hmac_sha256_encode(): 发生异常, " + e.toString());
            }
            return encodeStr;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417

    5.4、读取配置类

    package com.zhz.config;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    /**
     * @author :zhz
     * @date :Created in 2021/01/02
     * @version: V1.0
     * @slogan: 天下风云出我辈,一入代码岁月催
     * @description: 极验验证的ID和Key配置
     **/
    @Data
    @ConfigurationProperties(prefix = "geetest")
    public class GeetestProperties {
        /**
         * 极验的 ID
         */
        private String geetestId;
        /**
         * 极验的 key
         */
        private String geetestKey;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    package com.zhz.config;
    
    import com.zhz.geetest.GeetestLib;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author :zhz
     * @date :Created in 2021/01/02
     * @version: V1.0
     * @slogan: 天下风云出我辈,一入代码岁月催
     * @description: 极验验证的自动配置
     **/
    @Configuration
    @EnableConfigurationProperties(GeetestProperties.class)
    public class GeetestAutoConfiguration {
    
        private GeetestProperties geetestProperties ;
        public GeetestAutoConfiguration(GeetestProperties geetestProperties){
            this.geetestProperties = geetestProperties ;
        }
        @Bean
        public GeetestLib geetestLib(){
            GeetestLib geetestLib = new GeetestLib(geetestProperties.getGeetestId(),
                    geetestProperties.getGeetestKey());
            return geetestLib ;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    /**
     * @(#)Result.JAVA, 2020年04月02日.
     * <p>
     * Copyright 2020 GEETEST, Inc. All rights reserved.
     * GEETEST PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */
    package com.zhz.geetest;
    
    /**
     * sdk lib包的返回结果信息。
     */
    public class GeetestLibResult {
        /**
         * 成功失败的标识码,1表示成功,0表示失败
         */
        private int status = 0;
    
        /**
         * 返回数据,json格式
         */
        private String data = "";
    
        /**
         * 备注信息,如异常信息等
         */
        private String msg = "";
    
        public void setStatus(int status) {
            this.status = status;
        }
    
        public int getStatus() {
            return status;
        }
    
        public void setData(String data) {
            this.data = data;
        }
    
        public String getData() {
            return data;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setAll(int status, String data, String msg) {
            this.setStatus(status);
            this.setData(data);
            this.setMsg(msg);
        }
    
        @Override
        public String toString() {
            return String.format("GeetestLibResult{status=%s, data=%s, msg=%s}", this.status, this.data, this.msg);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    5.5、获取极验的第一次数据包

    5.5.1、在资源服务器里面放行路径

    我用的jwt+auth2.0,你们自己去放行路径--》"/gt/register"
    
    • 1

    5.5.2、添加控制器 GeetestController

    package com.zhz.controller;
    
    import com.zhz.geetest.GeetestLib;
    import com.zhz.geetest.GeetestLibResult;
    import com.zhz.model.R;
    import com.zhz.util.IpUtil;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author :zhz
     * @date :Created in 2021/01/02
     * @version: V1.0
     * @slogan: 天下风云出我辈,一入代码岁月催
     * @description:
     **/
    @RestController
    @RequestMapping("/gt")
    public class GeetestController {
        @Autowired
        private GeetestLib geetestLib;
    
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
    
        @GetMapping("/register")
        public R<String> register(String uuid) {
            String digestmod = "md5";
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("digestmod", digestmod);
            paramMap.put("user_id", uuid);
            paramMap.put("client_type", "web");
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            paramMap.put("ip_address", IpUtil.getIpAddr(servletRequestAttributes.getRequest()));//ip地址
    
            GeetestLibResult result = geetestLib.register(digestmod, paramMap); // 极验服务器交互
    
            // 将结果状态写到session中,此处register接口存入session,后续validate接口会取出使用
            // 注意,此demo应用的session是单机模式,格外注意分布式环境下session的应用
            redisTemplate.opsForValue().set(GeetestLib.GEETEST_SERVER_STATUS_SESSION_KEY, result.getStatus(), 180, TimeUnit.SECONDS);
    //        request.getSession().setAttribute( result.getStatus());
            redisTemplate.opsForValue().set(GeetestLib.GEETEST_SERVER_USER_KEY + ":" + uuid, uuid, 180, TimeUnit.SECONDS);
    //        request.getSession().setAttribute("userId", userId);
            // 注意,不要更改返回的结构和值类型
            return R.ok(result.getData());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    5.5.3、在gateway网关里面添加 CorsConfig

    package com.zhz.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.reactive.CorsWebFilter;
    import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
    
    /**
     * @author :zhz
     * @date :Created in 2020/12/20
     * @version: V1.0
     * @slogan: 天下风云出我辈,一入代码岁月催
     * @description: 解决跨域问题
     **/
    @Configuration
    public class CorsConfig {
        @Bean
        public CorsWebFilter corsWebFilter(){
            CorsConfiguration corsConfiguration=new CorsConfiguration();
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.addAllowedMethod("*");
            UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource=new UrlBasedCorsConfigurationSource();
            urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
            return new CorsWebFilter(urlBasedCorsConfigurationSource);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    具体代码:看本人gitee上:https://gitee.com/zhouzhz/coin-exchange/tree/master/coin-member/member-services 上

    原文章:https://blog.csdn.net/zhouhengzhe/article/details/112121677

  • 相关阅读:
    python的paramiko模块的安装与使用
    python的paramiko模块的安装与使用
    python的paramiko模块的安装与使用
    Python中的getattr()函数详解
    Python中的getattr()函数详解
    Python模块学习——optparse
    Python模块学习——optparse
    Python模块学习——optparse
    pkg_resources----Entry Points为程序提供扩展点
    pkg_resources----Entry Points为程序提供扩展点
  • 原文地址:https://www.cnblogs.com/tfil/p/14228429.html
Copyright © 2011-2022 走看看