摘要:引入依賴添加驗(yàn)證碼生成器是否有邊框默認(rèn)為我們可以自己設(shè)置,邊框顏色默認(rèn)為邊框粗細(xì)度默認(rèn)為驗(yàn)證碼生成器默認(rèn)為驗(yàn)證碼文本生成器默認(rèn)為驗(yàn)證碼文本字符內(nèi)容范圍默認(rèn)為驗(yàn)證碼文本字符長(zhǎng)度默認(rèn)為驗(yàn)證碼文本字體樣式默認(rèn)為驗(yàn)證碼文本字符大小默
引入依賴
com.github.axet kaptcha 0.0.9
Spring-mvc.xml添加
no black 5 5 com.google.code.kaptcha.impl.ShadowGimpy
生成驗(yàn)證碼
@RequestMapping(value = "/captchaImg", method = RequestMethod.GET) public void image(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setDateHeader("Expires", 0); 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"); String capText = captchaProducer.createText(); //生成圖片驗(yàn)證碼 BufferedImage image = captchaProducer.createImage(capText); //保存到shiro session SecurityUtils.getSubject().getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); try { out.flush(); } finally { out.close(); } }
驗(yàn)證驗(yàn)證碼
@RequestMapping(value = "/captchaValid", method = RequestMethod.POST) @ResponseBody public Message captchaValid(String captcha) throws Exception { String kaptcha = SecurityUtils.getSubject().getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY).toString(); SecurityUtils.getSubject().getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY); if (captcha.equalsIgnoreCase(kaptcha)) { return Message.success("驗(yàn)證成功"); } return Message.error("驗(yàn)證碼不正確"); }
合并后為
import com.google.code.kaptcha.Constants; import com.google.code.kaptcha.Producer; import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; /** * Controller - 驗(yàn)證碼 * * @author liaoyx * @version 1.0 */ @Controller("mgmtCaptchaController") @RequestMapping("/mgmt/captcha") public class CaptchaController extends BaseController { private Producer captchaProducer = null; @Autowired public void setCaptchaProducer(Producer captchaProducer) { this.captchaProducer = captchaProducer; } @RequestMapping(value = "/captchaImg", method = RequestMethod.GET) public void image(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setDateHeader("Expires", 0); 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"); String capText = captchaProducer.createText(); //生成圖片驗(yàn)證碼 BufferedImage image = captchaProducer.createImage(capText); //保存到shiro session SecurityUtils.getSubject().getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); try { out.flush(); } finally { out.close(); } } @RequestMapping(value = "/captchaValid", method = RequestMethod.POST) @ResponseBody public Message captchaValid(String captcha) throws Exception { String kaptcha = SecurityUtils.getSubject().getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY).toString(); SecurityUtils.getSubject().getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY); if (captcha.equalsIgnoreCase(kaptcha)) { return R.success("驗(yàn)證成功"); } return R.error("驗(yàn)證碼不正確"); } }
參數(shù)詳解
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/75959.html
摘要:這個(gè)文件包含對(duì)對(duì)數(shù)據(jù)訪問(wèn)進(jìn)行封裝的所有類(lèi)。為等提供的一致的聲明式和編程式事務(wù)管理。 SSM 環(huán)境搭建 目錄創(chuàng)建 pom.xml SSM 逐層配置 一、目錄 1.1 src/main/java 目錄下的包(以下包要放在項(xiàng)目包下,如:com.imooc.項(xiàng)目名) entity: 存放實(shí)體類(lèi) web: 存放controller,相當(dāng)于Struts中的action service: 業(yè)務(wù)...
摘要:環(huán)境要求使用純來(lái)搭建環(huán)境,要求的版本必須在以上。即視圖解析器解析文件上傳等等,如果都不需要配置的話,這樣就可以了??梢詫⒁粋€(gè)字符串轉(zhuǎn)為對(duì)象,也可以將一個(gè)對(duì)象轉(zhuǎn)為字符串,實(shí)際上它的底層還是依賴于具體的庫(kù)。中,默認(rèn)提供了和的,分別是和。 在 Spring Boot 項(xiàng)目中,正常來(lái)說(shuō)是不存在 XML 配置,這是因?yàn)?Spring Boot 不推薦使用 XML ,注意,并非不支持,Spring...
摘要:模仿的輕量級(jí)框架,適合學(xué)習(xí)和搭建小型項(xiàng)目使用,持續(xù)更新項(xiàng)目地址感興趣的記得喲目錄介紹框架源碼?;诳蚣軐?xiě)的一個(gè)小。根據(jù)配置,自動(dòng)掃描包。本項(xiàng)目更大的用處是學(xué)習(xí)的思想,而不是要開(kāi)發(fā)一個(gè)全新的框架。 bfmvc 模仿springmvc的輕量級(jí)web框架,適合學(xué)習(xí)和搭建小型web項(xiàng)目使用,持續(xù)更新 項(xiàng)目地址:https://github.com/CFshuming/... 感興趣的記得st...
摘要:最近在做某在線教育平臺(tái)網(wǎng)站的開(kāi)發(fā),按師兄的建議要用來(lái)搞?,F(xiàn)在把開(kāi)發(fā)過(guò)程中的一些相關(guān)經(jīng)驗(yàn)貼出來(lái)。事先聲明,請(qǐng)確保和都已經(jīng)安裝好。對(duì)于不使用的開(kāi)發(fā)者,可以直接建一個(gè)簡(jiǎn)單的項(xiàng)目。使用的話,請(qǐng)按照?qǐng)D進(jìn)行操作。 訪問(wèn)GitHub下載最新源碼:https://github.com/gaussic/Sp... 文章已針對(duì)IDEA 2016做了一定的更新,部分更新較為重要,請(qǐng)重新閱讀文章并下載最新源碼...
閱讀 3189·2021-10-19 11:46
閱讀 1127·2021-08-03 14:03
閱讀 3141·2021-06-11 18:08
閱讀 3060·2019-08-29 13:52
閱讀 2994·2019-08-29 12:49
閱讀 661·2019-08-26 13:56
閱讀 1072·2019-08-26 13:41
閱讀 988·2019-08-26 13:35