资讯专栏INFORMATION COLUMN

springMVC引入kaptcha

vpants / 299人阅读

摘要:引入依赖添加验证码生成器是否有边框默认为我们可以自己设置,边框颜色默认为边框粗细度默认为验证码生成器默认为验证码文本生成器默认为验证码文本字符内容范围默认为验证码文本字符长度默认为验证码文本字体样式默认为验证码文本字符大小默

引入依赖


    com.github.axet
        kaptcha
    0.0.9

Spring-mvc.xml添加


    
        
            
                
                    
                        no
                        black
                        5
                        5
                        com.google.code.kaptcha.impl.ShadowGimpy
                        
                    
                
            
        
    

生成验证码

@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();
    //生成图片验证码
    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 Message.success("验证成功");
    }
    return Message.error("验证码不正确");
}

合并后为

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 - 验证码
 *
 * @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();
        //生成图片验证码
        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("验证成功");
        }
        return R.error("验证码不正确");
    }

}

参数详解

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75959.html

相关文章

  • SSM : 环境搭建

    摘要:这个文件包含对对数据访问进行封装的所有类。为等提供的一致的声明式和编程式事务管理。 SSM 环境搭建 目录创建 pom.xml SSM 逐层配置 一、目录 1.1 src/main/java 目录下的包(以下包要放在项目包下,如:com.imooc.项目名) entity: 存放实体类 web: 存放controller,相当于Struts中的action service: 业务...

    MonoLog 评论0 收藏0
  • Spring+SpringMVC+Maven+Mybatis+MySQL+Jetty项目搭建(2)

    摘要:解决办法右击项目然后系统会在文件加下创建文件。引入在原有的依赖基础上新增两个依赖包注意和项目使用的版本对应包注意和项目使用的版本对应然后执行命令下载对应的包。访问资源到这一步,整个项目搭建完成 在上一篇文章中,我们搭建了一个Spring+Maven+Mybatis+MySQL项目,并通过单元测试验证了开发环境的可靠性。注意,这只是一个普通的 maven Java Project,而不是...

    neroneroffy 评论0 收藏0
  • 这一次,我连 web.xml 都不要了,纯 Java 搭建 SSM 环境!

    摘要:环境要求使用纯来搭建环境,要求的版本必须在以上。即视图解析器解析文件上传等等,如果都不需要配置的话,这样就可以了。可以将一个字符串转为对象,也可以将一个对象转为字符串,实际上它的底层还是依赖于具体的库。中,默认提供了和的,分别是和。 在 Spring Boot 项目中,正常来说是不存在 XML 配置,这是因为 Spring Boot 不推荐使用 XML ,注意,并非不支持,Spring...

    liaorio 评论0 收藏0
  • 自己模仿springmvc 写的一个轻量级mvc框架

    摘要:模仿的轻量级框架,适合学习和搭建小型项目使用,持续更新项目地址感兴趣的记得哟目录介绍框架源码。基于框架写的一个小。根据配置,自动扫描包。本项目更大的用处是学习的思想,而不是要开发一个全新的框架。 bfmvc 模仿springmvc的轻量级web框架,适合学习和搭建小型web项目使用,持续更新 项目地址:https://github.com/CFshuming/... 感兴趣的记得st...

    EddieChan 评论0 收藏0
  • [转载]使用IntelliJ IDEA开发SpringMVC网站(一)开发环境

    摘要:最近在做某在线教育平台网站的开发,按师兄的建议要用来搞。现在把开发过程中的一些相关经验贴出来。事先声明,请确保和都已经安装好。对于不使用的开发者,可以直接建一个简单的项目。使用的话,请按照图进行操作。 访问GitHub下载最新源码:https://github.com/gaussic/Sp... 文章已针对IDEA 2016做了一定的更新,部分更新较为重要,请重新阅读文章并下载最新源码...

    Ali_ 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<