没有名字。。。 2021-08-02 20:29 采纳率: 100%
浏览 234
已结题

问一下,就是为什么我后端添加了session,前端没有收到

后端代码


@Controller
@CrossOrigin
@RequestMapping("/VerifyCode")
public class VerifyCodeController {
    final int width = 400;
    final int height = 200;
    final String imgType = "jpeg";
    @GetMapping("/GetCode")
    public void getcode(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        ServletOutputStream outputStream = response.getOutputStream();
        String url = request.getRequestURI();
        System.out.println("Hello"+url);
        String code = GraphicsHelper.create(width, height, imgType, outputStream);
        session.setAttribute(url,code);
        System.out.println("验证码为"+session.getAttribute(url));

    }
}


```Java
public class GraphicsHelper {

    public static String create(final int width, final int height, final String imgType, OutputStream output ){
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics graphic = image.getGraphics();
        graphic.fillRect(0,0,width,height);
        Color[] colors = new Color[] { Color.BLUE, Color.GRAY, Color.GREEN, Color.RED, Color.BLACK, Color.ORANGE,
                Color.CYAN };
        // 在 "画板"上生成干扰线条 ( 50 是线条个数)
        for (int i = 0; i < 50; i++) {
            graphic.setColor(colors[random.nextInt(colors.length)]);
            final int x = random.nextInt(width);
            final int y = random.nextInt(height);
            final int w = random.nextInt(20);
            final int h = random.nextInt(20);
            final int signA = random.nextBoolean() ? 1 : -1;
            final int signB = random.nextBoolean() ? 1 : -1;
            graphic.drawLine(x, y, x + w * signA, y + h * signB);
        }

        // 在 "画板"上绘制字母
        graphic.setFont(new Font("Comic Sans MS", Font.BOLD, 30));
        for (int i = 0; i < 6; i++) {
            final int temp = random.nextInt(26) + 97;
            String s = String.valueOf((char) temp);
            sb.append(s);
            graphic.setColor(colors[random.nextInt(colors.length)]);
            graphic.drawString(s, i * (width / 6), height - (height / 3));
        }
        graphic.dispose();
        try {
            ImageIO.write(image, imgType, output);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
前端代码
<template>
  <div>
    <input type="text" name="" ref="val" id="">
    <input type="button" @click="refush()">
    <img ref="code" src="http://localhost:8888/web/VerifyCode/GetCode">

  </div>
</template>

<script>
var socket;
export default {
name: "chat",
  data(){
    return{


    }
  },beforeMount() {
    if(typeof WebSocket == undefined){
      return console.log("您的浏览器不支持Websocket")
    }
    else{
      socket = new WebSocket("ws://localhost:8010");
    }

  }
  ,methods:{
    refush(){
      this.$refs.code.src="http://localhost:8888/web/VerifyCode/GetCode?time="+new Date();
      var ajax = new XMLHttpRequest();
      ajax.open("get","http://localhost:8888/web/VerifyCode/GetCode")
      ajax.send();

    },
   
  }


}
</script>

<style scoped>

</style>

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 8月11日
      • 已采纳回答 8月3日
      • 创建了问题 8月2日

      悬赏问题

      • ¥15 ABAQUS简单的带传动系统运转不了
      • ¥15 关于git actions自动部署的问题
      • ¥25 小程序 view wx:for 循环太多时,如何在循环结束执行某个事件?
      • ¥15 曲率模态差matlab程序计算
      • ¥15 大概算是比较简单的坐标系旋转问题
      • ¥15 关于#python#的问题:安装完gym环境后
      • ¥15 关于稳像云台姿态控制中相机姿态误差表征
      • ¥15 求sm16306s驱动例程
      • ¥15 本地生活媒体平台口碑选品怎么获得权限
      • ¥15 R语言 回归树/袋装树/随机森林预测