盒子里的加菲猫 2022-02-07 16:04 采纳率: 73.1%
浏览 163
已结题

一个vue+kaptcha验证码。我在浏览器中直接用地址栏获取图片和验证都正常。但是我搬运到vue当中去做,正确都给我返回成错误的。

问题遇到的现象和发生背景

我刚刚学习的kaptcha验证码。并在vue中实操了。获取图片成功获取了,但是我提交验证的时候就出问题了。

img

比如说上图,验证码结果是rcg5

img

img

img

给我返回的是false。

但是如果我拿去直接给地址栏访问

img

img

img

奇了怪了!

求解决!!

问题相关代码,请勿粘贴截图

后端

CaptchaController获取图像
package com.example.demo.kaptcha;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

@Controller
//跨域问题
@CrossOrigin
public class CaptchaController {
    @Autowired
    private Producer captchaProducer;

    @Autowired
    private static Logger logger = LoggerFactory.getLogger(CaptchaController.class);
    @RequestMapping("getimg")
    public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        logger.debug("******************验证码是: " + code + "******************");

        response.setDateHeader("Expires", 0);

        // 设置标准的HTTP/1.1无缓存头信息
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

        // 设置IE扩展的HTTP/1.1无缓存头(使用addHeader)
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");

        // 设置标准HTTP/1.0无缓存报头
        response.setHeader("Pragma", "no-cache");

        // 返回一个jpeg
        response.setContentType("image/jpeg");

        // 为图像创建文本
        String capText = captchaProducer.createText();

        // 将文本存储在会话中
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);

        // 用文本创建图像
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();

        // 导出数据
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }
}
KaptchaConfig设置图像参数
package com.example.demo.kaptcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
public class KaptchaConfig {
    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
        Properties properties = new Properties();
        // 图片边框
        properties.setProperty("kaptcha.border", "no");
        // 边框颜色
        properties.setProperty("kaptcha.border.color", "black");
        //边框厚度
        properties.setProperty("kaptcha.border.thickness", "1");
        // 图片宽
        properties.setProperty("kaptcha.image.width", "200");
        // 图片高
        properties.setProperty("kaptcha.image.height", "50");
        //图片实现类
        properties.setProperty("kaptcha.producer.impl", "com.google.code.kaptcha.impl.DefaultKaptcha");
        //文本实现类
        properties.setProperty("kaptcha.textproducer.impl", "com.google.code.kaptcha.text.impl.DefaultTextCreator");
        //文本集合,验证码值从此集合中获取
        properties.setProperty("kaptcha.textproducer.char.string", "01234567890qwertyuiopasdfghjklzxcvbnm");
        //验证码长度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        //字体
        properties.setProperty("kaptcha.textproducer.font.names", "宋体");
        //字体颜色
        properties.setProperty("kaptcha.textproducer.font.color", "black");
        //文字间隔
        properties.setProperty("kaptcha.textproducer.char.space", "5");
        //干扰实现类
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise");
        //干扰颜色
        properties.setProperty("kaptcha.noise.color", "blue");
        //干扰图片样式
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
        //背景实现类
        properties.setProperty("kaptcha.background.impl", "com.google.code.kaptcha.impl.DefaultBackground");
        //背景颜色渐变,结束颜色
        properties.setProperty("kaptcha.background.clear.to", "white");
        //文字渲染器
        properties.setProperty("kaptcha.word.impl", "com.google.code.kaptcha.text.impl.DefaultWordRenderer");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
login 获取数据并返回布尔值
package com.example.demo.kaptcha;

import com.google.code.kaptcha.Constants;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
//跨域问题
@CrossOrigin
public class login {
    @GetMapping("login")

    public boolean login(HttpServletRequest request, @RequestParam("code") String code) {
        String sessionCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        if (code.equalsIgnoreCase(sessionCode)) {
//            验证正常返回true
            return true;
        } else {
//            验证失败返回false
            return false;
        }

    }
}

前端

封装
import axios from 'axios'
export function kaptcha(config) {
    let newVar = axios.create({
        baseURL: 'http://localhost:8080',
        timeout:5000
    });
    return newVar(config);
}
网页以及方法
<template>
  <div>
    <h1>图像验证码</h1>
    <img src="http://localhost:8080/getimg" id="images">
    <br>
    <a @click="replace" href="">看不清楚,换一张!</a>
    <br>
    <input size="20" placeholder="请输入验证码" id="input" />
    <br>
    <button @click="click">验证</button>
    <br>
    <a>判断结果:{{this.$data.message}}</a>
  </div>

</template>

<script>
import {kaptcha} from "@/network/kaptcha";
export default {
  name: 'HelloWorld',
  data(){
    return{
      data:'',
      message:''
    }
  },
  methods:{
    replace(){
      document.getElementById("images").src="http://localhost:8080/getimg";
    },
    click(){
      this.$data.data=document.getElementById("input").value;
      kaptcha({
        url:'login',
        params:{
          code:this.$data.data
        },
      }).then(res=>{
        this.$data.message=res.data
      }).catch(err=>{
        this.$data.message=err.data
      })
    }
  }
}
</script>

<style scoped>

</style>

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

6条回答 默认 最新

  • CCCCCCCYYY_ 2022-02-07 16:31
    关注

    session跨域了?login里面sessioncode打个断点看看拿到了啥

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 2月15日
  • 已采纳回答 2月7日
  • 创建了问题 2月7日

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测