spring boot项目引用kaptcha

4 篇文章 0 订阅

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="点击更换" οnclick="javascript:refreshCaptcha();"/>


刷新方法

function refreshCaptcha() {
				$("#kaptcha").attr("src","/images/kaptcha.jpg?t=" + Math.random());  
			}

参数配置有没有更简单的方式呢?


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值