推薦一個超級簡單 Java 圖形驗證碼模塊

簡介

Java 圖形驗證碼,支持 Gif、中文、算術等類型,可用於 Java Web、JavaSE 等項目。

效果展示

集成項目

maven 方式引入:

   <dependency>
      <groupId>com.github.whvcse</groupId>
      <artifactId>easy-captcha</artifactId>
      <version>1.6.2</version>
   </dependency>

在 SpringBoot 中使用

@Controller
public class CaptchaController {

    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }
}

前端 html 代碼:

<img src="/captcha" width="130px" height="48px" onClick="this.src=this.src+'?'" />

判斷驗證碼是否正確:

@Controller
public class LoginController {

    @PostMapping("/login")
    public JsonResult login(String username,String password,String verCode){
        if (!CaptchaUtil.ver(verCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的驗證碼
            return JsonResult.error("驗證碼不正確");
        }
    }   
}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章