算数验证码

吾非媛 2020年09月06日 425次浏览

Maven坐标

<!-- 验证码 Java开源图形验证码-->
        <dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
	    <version>1.6.2</version>
        </dependency>

控制器代码

import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/admin/base")
public class CaptchaController {

    @RequestMapping("/code.htm")
    public void getCaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Captcha captcha = new ArithmeticCaptcha();
        CaptchaUtil.out(captcha,request,response);
    }
}

运行效果

360截图17001020194349.jpg

优缺点

优点:快速,漂亮
缺点:存在Session会话中