生成验证码

验证码

登录的时候,需要生成验证码,下面是个简单的示例

具体实现

service类

import com.octo.captcha.engine.CaptchaEngine;
import com.octo.captcha.service.captchastore.CaptchaStore;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;

/**
 * 验证码图片生成的业务类
 */
public class CaptchaService extends DefaultManageableImageCaptchaService{
    public CaptchaService() {
        super();
    }

    public CaptchaService(int minSeconds, int axStoreSize,int loadBefore) {
        super(minSeconds, maxStoreSize, loadBefore);
    }

    public CaptchaService(CaptchaStore captchaStore,        CaptchaEngine captchaEngine, int minSeconds, int maxStoreSize,
            int loadBefore) {
        super(captchaStore, captchaEngine, minSeconds, maxStoreSize,
                loadBefore);
    }

    /**
     * 验证用户输入的值与产生的验证码值是否匹配,区分大小写
     * 
     * @param sessionId
     * @param inputStr
     * @return
     */
    public boolean check(String sessionId, String inputStr) {
        boolean flag = false;
        try {
            flag = this.validateResponseForID(sessionId, inputStr);
        } catch (Exception e) {
            // e.printStackTrace();
        }
        return flag;
    }
}

controller类

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CaptchaController {

    @Autowired
    private CaptchaService captchaService;

    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //response header
        response.setDateHeader("Expires", 0L);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        //session id
        String id = request.getSession().getId();

        BufferedImage bi = this.captchaService.getImageChallengeForID(id);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}

login


@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
        @Valid
        @ResponseBody
        public String login(                     @NotEmpty(message = "用户名不能为空!") @RequestParam("lname") String lname,                        @NotEmpty(message = "密码不能为空!") @RequestParam("pwd") String pwd,
                        @NotEmpty(message = "验证码不能为空!") @RequestParam("cc") String cc,                        HttpSession session) throws Exception {
              //...
                if (captchaService.check(session.getId(), cc)) {
                //再判断name和pwd对应
        }else{
        //验证码错误
        }
                        }

  
                <dependency>
                        <groupId>com.octo.captchagroupId>
                        <artifactId>jcaptcha-allartifactId>
                        <version>1.0version>
                dependency>
                <dependency>
                        <groupId>commons-collectionsgroupId>
                        <artifactId>commons-collectionsartifactId>
                        <version>3.2.2version>
                dependency>

你可能感兴趣的:(java)