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
import org.jfw.apt.web.annotation.operate.Post;
15
import org.jfw.apt.web.annotation.operate.Post;
16
import org.jfw.apt.web.annotation.param.JdbcConn;
16
import org.jfw.apt.web.annotation.param.JdbcConn;
17
import org.jfw.apt.web.annotation.param.PathVar;
17
import org.jfw.apt.web.annotation.param.PathVar;
18
import org.jfw.apt.web.annotation.param.SessionVal;
18
import org.jfw.util.StringUtil;
19
import org.jfw.util.StringUtil;
19
import org.jfw.util.context.JfwAppContext;
20
import org.jfw.util.context.JfwAppContext;
20
import org.jfw.util.exception.JfwBaseException;
21
import org.jfw.util.exception.JfwBaseException;
780
		}
781
		}
781
		return sc.getValue().equals(vc);
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
	@Post
793
	@Post
785
	@Path("/resetPasswordWithMobilePhone")
794
	@Path("/resetPasswordWithMobilePhone")

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

4

4

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

11

12
public class PictureVC extends HttpServlet implements Servlet {  
12
public class PictureVC extends HttpServlet implements Servlet {  
13
  
13
  
18
        response.setHeader("Cache-Control", "no-cache");  
18
        response.setHeader("Cache-Control", "no-cache");  
19
        response.setDateHeader("Expires", 0);  
19
        response.setDateHeader("Expires", 0);  
20
        response.setContentType("image/jpeg");  
20
        response.setContentType("image/jpeg");  
21
          
22
        //生成随机字串  
21
        HttpSession session = request.getSession();
22
        //生成随机字
23
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
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
        int w = 100, h = 40;  
27
        int w = 100, h = 40;  
39
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);  
28
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);