Browse Source

添加图片验证的方法,验证成功返回true,失败返回false。

zzy.zhiyuan.foxmail 8 years ago
parent
commit
d334c84f03

+ 9 - 0
src/main/java/com/ekexiu/portal/service/SysService.java

@ -15,6 +15,7 @@ import org.jfw.apt.web.annotation.operate.Get;
15 15
import org.jfw.apt.web.annotation.operate.Post;
16 16
import org.jfw.apt.web.annotation.param.JdbcConn;
17 17
import org.jfw.apt.web.annotation.param.PathVar;
18
import org.jfw.apt.web.annotation.param.SessionVal;
18 19
import org.jfw.util.StringUtil;
19 20
import org.jfw.util.context.JfwAppContext;
20 21
import org.jfw.util.exception.JfwBaseException;
@ -780,6 +781,14 @@ public class SysService {
780 781
		}
781 782
		return sc.getValue().equals(vc);
782 783
	}
784
	
785
	@Post
786
	@Path("/checkPicture")
787
	public boolean checkPictureVC(@SessionVal(value = "verification",defaultvalue="null",remove=true) String verification, 
788
			String submitVerification) throws JfwBaseException {
789
		if(null==verification){throw new JfwBaseException("Picture is expire !");}
790
		return verification.equals(submitVerification);
791
	}
783 792
784 793
	@Post
785 794
	@Path("/resetPasswordWithMobilePhone")

+ 5 - 16
src/main/java/com/ekexiu/portal/util/PictureVC.java

@ -4,10 +4,10 @@ import java.io.IOException;
4 4

5 5
import javax.servlet.Servlet;
6 6
import javax.servlet.ServletException;  
7
import javax.servlet.http.Cookie;
8 7
import javax.servlet.http.HttpServlet;
9 8
import javax.servlet.http.HttpServletRequest;  
10 9
import javax.servlet.http.HttpServletResponse;  
10
import javax.servlet.http.HttpSession;
11 11

12 12
public class PictureVC extends HttpServlet implements Servlet {  
13 13
  
@ -18,22 +18,11 @@ public class PictureVC extends HttpServlet implements Servlet {
18 18
        response.setHeader("Cache-Control", "no-cache");  
19 19
        response.setDateHeader("Expires", 0);  
20 20
        response.setContentType("image/jpeg");  
21
          
22
        //生成随机字串  
21
        HttpSession session = request.getSession();
22
        //生成随机字
23 23
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
24
        //获取上次请求发送的rand的cookie 设置生命为0.
25
        Cookie[] cookies = request.getCookies();
26
        for (Cookie cookie : cookies) {
27
			if("rand".equalsIgnoreCase(cookie.getName())){
28
				cookie.setMaxAge(0);
29
			}
30
		}
31
        //为随机数创建cookie
32
        Cookie rand = new Cookie("rand", verifyCode.toLowerCase());
33
        //设置cookie中随机数的过期时间为1小时
34
        rand.setMaxAge(60 * 60);
35
        //在响应头中添加cookie
36
        response.addCookie(rand);
24
        //将验证码设置到session里 方便验证
25
        session.setAttribute("verification", verifyCode);
37 26
        //生成图片  
38 27
        int w = 100, h = 40;  
39 28
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);