shop--6.店铺注册--使用kaptcha实现验证码

1.引入jar包

https://mvnrepository.com/中搜索com.github.penggle

找到kaptcha,将其dependency拷贝到pom.xml下

2.在web.xml中编写servlet,用于生成验证码


  
    Kaptcha
    com.google.code.kaptcha.servlet.KaptchaServlet
    

    
    
      kaptcha.border
      no
    

    
    
      kaptcha.textproducer.font.color
      red
    
    
    
      kaptcha.textproducer.font.size
      40
    

    
    
      kaptcha.image.width
      135
    
    
    
      kaptcha.image.height
      50
    

    
    
      kaptcha.textproducer.char.string
      abcdefghijklmn123654789
    
    
    
      kaptcha.textproducer.char.length
      4
    
    
    
      kaptcha.textproducer.font.names
      Arial
    

    
    
      kaptcha.noise.color
      black
    
  
  
  
    Kaptcha
    /Kaptcha
  

  

  

3.然后在页面中生成验证码

 


  • 验证码
    点击更换
  •   

    其中  点击更换

    changeVerifyCode(this)是更换验证码的函数,可以写在js中,方便调用

    src="../Kaptcha" 找到web.xml中名为Kaptcha的servlet

     

    changeVerifyCode方法

    function changeVerifyCode(img){
        img.src = "../Kaptcha?" + Math.floor(Math.random() * 100);
    }
    

      

    然后在js中将验证码接收传回后台

    var verifyCodeActual = $('#j_kaptcha').val();
    if(!verifyCodeActual){
         $.toast('请输入验证码!');
         return;
    }
    formData.append('verifyCodeActual', verifyCodeActual);
    

      

    写一个判断验证码是否正确的工具类

    CodeUtil

    /**
         * 判断验证码是否正确
         */
        public static boolean checkVerifyCode(HttpServletRequest request){
            //实际的验证码
            String verifyCodeExpected = (String) request.getSession()
                    .getAttribute( Constants.KAPTCHA_SESSION_KEY);
            String verifyCodeActual = HttpServletRequestUtil
                    .getString( request, "verifyCodeActual" );
    
            if(verifyCodeActual == null || !verifyCodeExpected.equals(verifyCodeActual)){
                return false;
            }
            return true;
        }
    

      

    然后在注册店铺时,将验证码的验证添加上

    //注册店铺
        @RequestMapping(value="/registershop", method= RequestMethod.POST )
        @ResponseBody
        public Map registerShop(HttpServletRequest request){
            Map modelMap = new HashMap<>();
            //判断验证码是否正确
            if(!CodeUtil.checkVerifyCode(request)){
                modelMap.put( "success", false );
                modelMap.put( "errMsg", "验证码错误" );
                return modelMap;
            }
    
    
            //1.接收并转化相应的参数,包括店铺信息和图片信息
            String shopStr = HttpServletRequestUtil.getString( request, "shopStr" );
            ObjectMapper mapper = new ObjectMapper(); // create once, reuse
            Shop shop = null;
            try {
                shop = mapper.readValue(shopStr, Shop.class);
            } catch (IOException e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
                return modelMap;
            }
            CommonsMultipartFile shopImg = null;
            //在本次会话的上下文获取上传的文件
            CommonsMultipartResolver commonsMultipartResolver =
                    new CommonsMultipartResolver(request.getSession().getServletContext());
            //如果文件上传的有值
            if(commonsMultipartResolver.isMultipart( request )){
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
                shopImg = (CommonsMultipartFile)multipartHttpServletRequest.getFile("shopImg");
            } else{
                modelMap.put( "success", false );
                modelMap.put("errMsg", "上传图片不能为空");
                return modelMap;
            }
            //2.注册店铺
            if(shop != null && shopImg != null){
                PersonInfo user = (PersonInfo) request.getSession().getAttribute("user");
                shop.setOwner( user );
                ShopExecution shopExecution = shopService.addShop(shop, shopImg);
                if(shopExecution.getState() == ShopStateEnum.CHECK.getState()){
                    modelMap.put( "success", true );
    
                }else{
                    modelMap.put( "success", false );
                    modelMap.put( "errMsg",  shopExecution.getStateInfo());
                }
            }else{
                modelMap.put("success", false);
                modelMap.put("errMsg", "请输入店铺信息");
            }
            //3.返回结果
            return modelMap;
        }
    

      

    但是单单这样,后台是读取不到

    针对form中有文件流的,要用multipartResolver的文件上传解析器

    要在spring-web.xml中

    
        
            
            
            
            
        
    

      

    还在pom.xml中添加

    
        
        
          commons-fileupload
          commons-fileupload
          1.3.2
        
    

      

    转载于:https://www.cnblogs.com/SkyeAngel/p/8886125.html

    你可能感兴趣的:(java,web.xml)