springboot——kaptcha

springboot——kaptcha导入包: <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>配置类:p…

大家好,又见面了,我是你们的朋友全栈君。

导入包:

		<dependency>
			<groupId>com.github.penggle</groupId>
			<artifactId>kaptcha</artifactId>
			<version>2.3.2</version>
		</dependency>

配置类:

package com.now.community.community.config;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KaptchaConfig {

    @Bean
    public Producer kaptchaProducer() {
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", "100");
        properties.setProperty("kaptcha.image.height", "40");
        properties.setProperty("kaptcha.textproducer.font.size", "32");
        properties.setProperty("kaptcha.textproducer.font.color", "0,0,0");
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");

        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Config config = new Config(properties);
        kaptcha.setConfig(config);
        return kaptcha;
    }

}
Controller
    //验证码
    @RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
    public void getKaptcha(HttpServletResponse response/*, HttpSession session*/) {
        // 生成验证码
        String text = kaptchaProducer.createText();
        BufferedImage image = kaptchaProducer.createImage(text);

        //将验证码存入session
        session.setAttribute("kaptcha", text);

        // 将图片输出给浏览器
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            logger.error("响应验证码失败:" + e.getMessage());
        }
    }

html

<img th:src="@{/kaptcha}" id="kaptcha" style="width:100px;height:40px;" class="mr-2"/>
<a href="javascript:refresh_kaptcha();" class="font-size-12 align-bottom">刷新验证码</a>

js:

jq

善意欺骗,随机数,更新。

<script>
		function refresh_kaptcha() {
			var path = CONTEXT_PATH + "/kaptcha?p=" + Math.random();
			$("#kaptcha").attr("src", path);
		}
</script>

springboot——kaptcha

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150375.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 流量相关说明

    流量相关说明

    2021年10月8日
    44
  • 让电脑报废的代码(30万行代码)

    作者:小傅哥博客:https://bugstack.cn沉淀、分享、成长,让自己和他人都能有所收获!????一、前言20万行代码写完,毕业了找一份工作不是问题!刚一毕业因为找不到工作,就得报名去参加Java培训的大有人在。并不是说参加培训就不好,只不过以你现在这个毕业的时间点参加,就会显得特别匆忙。因为你的压力既来自于培训还需要花家里一笔不小的费用,也有同班同学已经找到一份不错的工作开始赚钱的比对。大学四年其实有足够的时间让你学会编程,也能从一个较长时间的学习中,知道自己适合不适合做程序员。

    2022年4月11日
    215
  • source insight激活码3.5_source insight4.0安装

    source insight激活码3.5_source insight4.0安装SourceInsight3.x官方下载地址:http://www.sourceinsight.com/distribute/Si3563Setup.exe官方网站:http://www.sourceinsight.com/注册码:SI3US-230590-09757SI3US-840598-11493SI3US-404808-04697SI3US-510811-93484…

    2022年10月3日
    3
  • .htaccess文件RewriteRule语法规则

    .htaccess文件RewriteRule语法规则.htaccess文件是运行ApacheWebServer的Web服务器的配置文件,对配置和重定向ApacheWebServer文件系统很有用。请记住.htaccess文件将采用隐藏格式。没有人可以通过URL直接看到它。.htaccess文件有很多用途。在这里,我将讨论.htaccess文件RewriteRule语法规则。RewriteRule语法规则#——位于行首时表示注释。[F]——Forbidden(禁止):命令服务器返回403Forb…

    2022年5月15日
    28
  • 大数据Lambda架构详解

    大数据Lambda架构详解Lambda架构是NathanMarz提出的一个实时大数据处理框架。NathanMarz是著名的实时大数据处理框架Storm的作者,Lambda架构就是其根据多年分布式大数据系统的经验总结提炼而成。NathanMarz在BigData:Principlesandbestpracticesofscalablereal-timedatasystems一书中提到了很多实时大数据系统的关键特性,包括容错性,健壮性,低延迟,可扩展,通用性,方便查询等,Lambda就是其根据这些特性设计的一

    2022年6月25日
    40
  • git 提交代码常用命令

    git 提交代码常用命令 一、master分支代码提交过程 gitlog 查看git合入的记录    gitpull从服务器重新拉代码,将本地代码更新为服务器上的最新代码 gitstatus查看本地代码状态,是否有待提交的代码  git add.  将本地代码全部提交  gitcommit-m"合入新的PUCCH和小区功率代码"   为本次提交添加注释 …

    2022年6月26日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号