├── README.md
├── CodeVerify
├── LoginController.java
├── GetCodeController.java
├── CheckLoginController.java
└── login.jsp
├── SlidingVerify
├── GeetestConfig.java
├── StartCaptchaServlet.java
├── VerifyLoginServlet.java
├── login2.jsp
└── GeetestLib.java
└── web.xml
/README.md:
--------------------------------------------------------------------------------
1 | # Verify
2 | 利用java代码实现滑动验证和普通验证码验证
3 |
--------------------------------------------------------------------------------
/CodeVerify/LoginController.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxy1035/Verify/HEAD/CodeVerify/LoginController.java
--------------------------------------------------------------------------------
/CodeVerify/GetCodeController.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxy1035/Verify/HEAD/CodeVerify/GetCodeController.java
--------------------------------------------------------------------------------
/CodeVerify/CheckLoginController.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangxy1035/Verify/HEAD/CodeVerify/CheckLoginController.java
--------------------------------------------------------------------------------
/SlidingVerify/GeetestConfig.java:
--------------------------------------------------------------------------------
1 | package com.ow.appmg.controller.login;
2 |
3 | /**
4 | * GeetestWeb配置文件
5 | *
6 | *
7 | */
8 | public class GeetestConfig {
9 |
10 | // 填入自己的captcha_id和private_key
11 | private static final String geetest_id = "b46d1900d0a894591916ea94ea91bd2c";
12 | private static final String geetest_key = "36fc3fe98530eea08dfc6ce76e3d24c4";
13 |
14 | public static final String getGeetest_id() {
15 | return geetest_id;
16 | }
17 |
18 | public static final String getGeetest_key() {
19 | return geetest_key;
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/SlidingVerify/StartCaptchaServlet.java:
--------------------------------------------------------------------------------
1 | package com.ow.appmg.controller.login;
2 |
3 | import java.io.IOException;
4 | import java.io.PrintWriter;
5 |
6 | import javax.servlet.ServletException;
7 | import javax.servlet.http.HttpServlet;
8 | import javax.servlet.http.HttpServletRequest;
9 | import javax.servlet.http.HttpServletResponse;
10 |
11 |
12 |
13 | /**
14 | * 使用Get的方式返回challenge和capthca_id,此方式以实现前后端完全分离的开发模式
15 | *
16 | */
17 | public class StartCaptchaServlet extends HttpServlet {
18 |
19 | private static final long serialVersionUID = 1L;
20 |
21 | protected void doGet(HttpServletRequest request,
22 | HttpServletResponse response) throws ServletException, IOException {
23 |
24 | GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key());
25 |
26 | String resStr = "{}";
27 |
28 | //自定义userid
29 | String userid = "test";
30 |
31 | //进行验证预处理
32 | int gtServerStatus = gtSdk.preProcess(userid);
33 |
34 | //将服务器状态设置到session中
35 | request.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus);
36 | //将userid设置到session中
37 | request.getSession().setAttribute("userid", userid);
38 |
39 | resStr = gtSdk.getResponseStr();
40 |
41 | PrintWriter out = response.getWriter();
42 | out.println(resStr);
43 |
44 | }
45 | }
--------------------------------------------------------------------------------
/web.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 | springmvc
9 |
10 | org.springframework.web.servlet.DispatcherServlet
11 |
12 |
13 | contextConfigLocation
14 | classpath:com/ow/appmg/config/applicationContext.xml
15 |
16 | 1
17 |
18 |
19 | StartCaptchaServlet
20 | com.ow.appmg.controller.login.StartCaptchaServlet
21 |
22 |
23 |
24 | StartCaptchaServlet
25 | /login/pc-geetest/register
26 |
27 |
28 | springmvc
29 | *.from
30 |
31 |
32 |
33 |
34 |
35 | encodingfilter
36 |
37 | org.springframework.web.filter.CharacterEncodingFilter
38 |
39 |
40 | encoding
41 | UTF-8
42 |
43 |
44 |
45 | encodingfilter
46 | /*
47 |
48 |
49 |
50 | index.jsp
51 |
52 |
--------------------------------------------------------------------------------
/SlidingVerify/VerifyLoginServlet.java:
--------------------------------------------------------------------------------
1 | package com.ow.appmg.controller.login;
2 |
3 | import java.io.IOException;
4 | import java.io.PrintWriter;
5 | import javax.servlet.ServletException;
6 | import javax.servlet.http.HttpServlet;
7 | import javax.servlet.http.HttpServletRequest;
8 | import javax.servlet.http.HttpServletResponse;
9 |
10 |
11 | import org.json.JSONException;
12 | import org.json.JSONObject;
13 |
14 |
15 | /**
16 | * 使用post方式,返回验证结果, request表单中必须包含challenge, validate, seccode
17 | */
18 | public class VerifyLoginServlet extends HttpServlet {
19 |
20 | private static final long serialVersionUID = 244554953219893949L;
21 |
22 | protected void doPost(HttpServletRequest request,
23 | HttpServletResponse response) throws ServletException, IOException {
24 |
25 | GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key());
26 |
27 | String challenge = request.getParameter(GeetestLib.fn_geetest_challenge);
28 | String validate = request.getParameter(GeetestLib.fn_geetest_validate);
29 | String seccode = request.getParameter(GeetestLib.fn_geetest_seccode);
30 |
31 | //从session中获取gt-server状态
32 | int gt_server_status_code = (Integer) request.getSession().getAttribute(gtSdk.gtServerStatusSessionKey);
33 |
34 | //从session中获取userid
35 | String userid = (String)request.getSession().getAttribute("userid");
36 |
37 | int gtResult = 0;
38 |
39 | if (gt_server_status_code == 1) {
40 | //gt-server正常,向gt-server进行二次验证
41 |
42 | gtResult = gtSdk.enhencedValidateRequest(challenge, validate, seccode, userid);
43 | System.out.println(gtResult);
44 | } else {
45 | // gt-server非正常情况下,进行failback模式验证
46 |
47 | System.out.println("failback:use your own server captcha validate");
48 | gtResult = gtSdk.failbackValidateRequest(challenge, validate, seccode);
49 | System.out.println(gtResult);
50 | }
51 |
52 |
53 | if (gtResult == 1) {
54 | // 验证成功
55 | PrintWriter out = response.getWriter();
56 | JSONObject data = new JSONObject();
57 | try {
58 | data.put("status", "success");
59 | data.put("version", gtSdk.getVersionInfo());
60 | } catch (JSONException e) {
61 | e.printStackTrace();
62 | }
63 | out.println(data.toString());
64 | }
65 | else {
66 | // 验证失败
67 | JSONObject data = new JSONObject();
68 | try {
69 | data.put("status", "fail");
70 | data.put("version", gtSdk.getVersionInfo());
71 | } catch (JSONException e) {
72 | e.printStackTrace();
73 | }
74 | PrintWriter out = response.getWriter();
75 | out.println(data.toString());
76 | }
77 |
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/CodeVerify/login.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" pageEncoding="UTF-8"%>
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | APP管理平台
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
31 |
32 |
33 |
34 |
35 |
36 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/SlidingVerify/login2.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2 | <%
3 | String path = request.getContextPath();
4 | String basePath = request.getScheme() + "://"
5 | + request.getServerName() + ":" + request.getServerPort()
6 | + path + "/";
7 | %>
8 |
9 |
10 |
11 | APP管理平台
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
38 |
96 |
97 |
98 |
99 |
100 |
101 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
184 |
185 |