SpringBoot使用Kaptcha验证码

Kaptcha 是一个可高度配置的实用验证码生成工具。个人认为还是挺好用的,虽然现有更加安全的验证方式。

此次是使用springboot+ssm,使用maven管理。直接上步骤:

  1. pom文件添加jar包依赖:
    		
    		
       	
                com.github.penggle
                kaptcha
                2.3.2
            

     

  2. 配置Kaptcha验证码的样式:在src/main/resources编写一个kaptchaXML.xml(文件名自创)
    
    
    
        
            
                
                    
                                                
                              
                            150  
                              
                            40  
                              
                            0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ  
                              
                            5  
                              
                            no  
                              
                            105,179,90  
                              
                            1  
                              
                            black  
                              
                            30  
                              
                            微软雅黑  
                              
                            black  
                              
                            5  
                              
                            com.google.code.kaptcha.impl.ShadowGimpy 
                            
                        
                    
                
            
        
    
    
    

     

  3. 在启动类用@ImportResource来导入xml配置文件
    @SpringBootApplication
    @ImportResource(locations = { "classpath:KaptchaXML.xml" })
    @MapperScan("com.p.mapper")
    public class MyPApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(MyPApplication.class, args);
    	}
    
    }

     

  4. 生成验证码
    package com.p.controller;
    
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import javax.imageio.ImageIO;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    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.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.google.code.kaptcha.impl.DefaultKaptcha;
    
    @Controller
    @RequestMapping("/code")
    public class Codecontroller {
    	
    	
    	
    	@Autowired
    	private DefaultKaptcha captchaProducer;
    	/**  
    	 * @Title: defaultKaptcha  
    	 * @Description: 生成验证码,并存到session中  
    	 * @param @param httpServletRequest
    	 * @param @param httpServletResponse
    	 * @param @throws Exception    参数  
    	 * @return void    返回类型  
    	 * @throws  
    	 */  
    	 
    	@RequestMapping("/defaultKaptcha") 
        public void defaultKaptcha(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception{  
                byte[] captchaChallengeAsJpeg = null;    
                 ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();    
                 try {    
                 //生产验证码字符串并保存到session中  
                 String createText = captchaProducer.createText();  
                 //打印验证码
                 System.out.println(createText);
                 httpServletRequest.getSession().setAttribute("code", createText);  
                 //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中  
                 BufferedImage challenge = captchaProducer.createImage(createText);  
                 ImageIO.write(challenge, "jpg", jpegOutputStream);  
                 } catch (IllegalArgumentException e) {    
                     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);    
                     return;    
                 }   
                 
                 //定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组  
                 captchaChallengeAsJpeg = jpegOutputStream.toByteArray();    
                 httpServletResponse.setHeader("Cache-Control", "no-store");    
                 httpServletResponse.setHeader("Pragma", "no-cache");    
                 httpServletResponse.setDateHeader("Expires", 0);    
                 httpServletResponse.setContentType("image/jpeg");    
                 ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();    
                 responseOutputStream.write(captchaChallengeAsJpeg);    
                 responseOutputStream.flush();    
                 responseOutputStream.close();    
        }  
    
    	
    }
    

     

  5. 在前端使用:
    	  
    验证码

     

  6. 效果图样

SpringBoot使用Kaptcha验证码_第1张图片

最后就此完成Kaptcha生成验证码的整个过程了。

你可能感兴趣的:(ssm,Spring,Boot)