您的位置:首页 > 编程语言 > Java开发

spring boot项目引用kaptcha

2016-06-24 17:17 435 查看
pom 文件引用依赖

<!-- 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());
}


参数配置有没有更简单的方式呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  验证码 kaptcha