spring boot项目引用kaptcha
2016-06-24 17:17
435 查看
pom 文件引用依赖
kaptcha.jar在maven库里没找到,自己去官网下载的
mvn install:install-file -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3.2 -Dfile=C:\Server\MavenRepository\maven_jar\com\google\code\kaptcha\2.3.2\kaptcha-2.3.2.jar -Dpackaging=jar -DgeneratePom=true
更新:
使用com.github.axet.kaptcha会报错java.lang.IllegalStateException: Cannot create a session after the response has been committed
所以要么使用com.google.code.kaptcha,要么请勿参照这个方法
//kaptcha已不由谷歌维护,新的依赖是:
配置参数,如果不需要配置的话,简单两行就搞定
添加一些初始化参数
都是配置在配置文件里面的:
[颜色color不可以这样写的,暂时写成black]
附上代码
前面这堆配置代码,实际上就是实现普通web项目这些配置(spring-applicationcontext)
前台页面,加在用户名密码框下面
刷新方法
参数配置有没有更简单的方式呢?
<!-- kaptcha --> <dependency> <groupId>com.google.code</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
kaptcha.jar在maven库里没找到,自己去官网下载的
mvn install:install-file -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3.2 -Dfile=C:\Server\MavenRepository\maven_jar\com\google\code\kaptcha\2.3.2\kaptcha-2.3.2.jar -Dpackaging=jar -DgeneratePom=true
更新:
使用com.github.axet.kaptcha会报错java.lang.IllegalStateException: Cannot create a session after the response has been committed
所以要么使用com.google.code.kaptcha,要么请勿参照这个方法
//kaptcha已不由谷歌维护,新的依赖是:
//<dependency> // <groupId>com.github.axet</groupId> // <artifactId>kaptcha</artifactId> // <version>0.0.9</version> // </dependency>
配置参数,如果不需要配置的话,简单两行就搞定
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Bean public ServletRegistrationBean servletRegistrationBean() throws ServletException{ return new ServletRegistrationBean(new KaptchaServlet(),"/images/kaptcha.jpg"); } }
添加一些初始化参数
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { /*@Value("${kaptcha.border}") private String kborder;*/ @Value("${kaptcha.session.key}") private String skey; @Value("${kaptcha.textproducer.font.color}") private String fcolor; @Value("${kaptcha.textproducer.font.size}") private String fsize; @Value("${kaptcha.obscurificator.impl}") private String obscurificator; @Value("${kaptcha.noise.impl}") private String noise; @Value("${kaptcha.image.width}") private String width; @Value("${kaptcha.image.height}") private String height; @Value("${kaptcha.textproducer.char.length}") private String clength; @Value("${kaptcha.textproducer.char.space}") private String cspace; @Value("${kaptcha.background.clear.from}") private String from; @Value("${kaptcha.background.clear.to}") private String to; @Bean public ServletRegistrationBean servletRegistrationBean() throws ServletException{ ServletRegistrationBean servlet = new ServletRegistrationBean(new KaptchaServlet(),"/images/kaptcha.jpg"); servlet.addInitParameter("kaptcha.border", "no"/*kborder*/);//无边框 servlet.addInitParameter("kaptcha.session.key", skey);//session key servlet.addInitParameter("kaptcha.textproducer.font.color", fcolor); servlet.addInitParameter("kaptcha.textproducer.font.size", fsize); servlet.addInitParameter("kaptcha.obscurificator.impl", obscurificator); servlet.addInitParameter("kaptcha.noise.impl", noise); servlet.addInitParameter("kaptcha.image.width", width); servlet.addInitParameter("kaptcha.image.height", height); servlet.addInitParameter("kaptcha.textproducer.char.length", clength); servlet.addInitParameter("kaptcha.textproducer.char.space", cspace); servlet.addInitParameter("kaptcha.background.clear.from", from); //和登录框背景颜色一致 servlet.addInitParameter("kaptcha.background.clear.to", to); return servlet; } }
都是配置在配置文件里面的:
[颜色color不可以这样写的,暂时写成black]
附上代码
kaptcha: session: key: kaptcha.code #border: no #渲染效果:水纹:WaterRipple;鱼眼:FishEyeGimpy;阴影:ShadowGimpy obscurificator: impl: com.google.code.kaptcha.impl.WaterRipple #不要噪点 noise: impl: com.google.code.kaptcha.impl.NoNoise image: width: 90 height: 33 textproducer: font: size: 25 color: black char: length: 4 space: 5 #和登录框背景颜色一致 background: clear: from: 247,247,247 to: 247,247,247
前面这堆配置代码,实际上就是实现普通web项目这些配置(spring-applicationcontext)
前台页面,加在用户名密码框下面
<img style="width:47%;display:inline;" id="kaptcha" src="/images/kaptcha.jpg" title="点击更换" onclick="javascript:refreshCaptcha();"/>
刷新方法
function refreshCaptcha() { $("#kaptcha").attr("src","/images/kaptcha.jpg?t=" + Math.random()); }
参数配置有没有更简单的方式呢?
相关文章推荐
- java自动生成验证码插件-kaptcha
- 如何识别高级的验证码的技术总结第1/4页
- WinForm生成验证码图片的方法
- PHP 验证码不显示只有一个小红叉的解决方法
- 一个简单安全的PHP验证码类 附调用方法
- asp汉字中文图片验证码
- asp.net生成字母和数字混合图形验证码
- PHP实现简单汉字验证码
- php生成动态验证码gif图片
- php生成验证码函数
- 基于JavaScript短信验证码如何实现
- php 验证码制作(网树注释思想)
- 基于C#实现12306的动态验证码变成静态验证码的方法
- 理解C#生成验证码的过程
- C#验证码识别基础方法实例分析
- javascript点击才出现验证码
- node.js WEB开发中图片验证码的实现方法
- 深入学习.net验证码生成及使用方法
- jQuery Real Person验证码插件防止表单自动提交
- 验证码的三个常见漏洞和修复方法