源码:https://github.com/HannahLihui/StudentManager-SSM/tree/master/SSM-git/StudentManager-SSM-master

1.前端有一个img引入,这里this.src=this.src+'?'就会调用映射到后台的checkCode

<el-form-item prop="code">

                                 <img src="checkCode" alt="" width="100" height="32" class="passcode" style="height:43px;cursor:pointer; float:left;"onclick="this.src=this.src+'?'">>

                              <el-input v-model="login.code" placeholder="请输入验证码" style="width: 100px; float:center"  auto-complete="off"></el-input>

</el-form-item>

2.后台返回一个图片

 @RequestMapping(value="/checkCode")

 public void checkCode(HttpServletRequest request, HttpServletResponse response)

             throws ServletException, IOException {

 //设置相应类型,告诉浏览器输出的内容为图片

         response.setContentType("image/jpeg");

         HttpSession session = request.getSession();

         //设置响应头信息,告诉浏览器不要缓存此内容

         response.setHeader("pragma", "no-cache");

         response.setHeader("Cache-Control", "no-cache");

         response.setDateHeader("Expire", 0);

         RandomValidateCode randomValidateCode = new RandomValidateCode();

         try {

             randomValidateCode.getRandcode(request, response);

         } catch (Exception e) {

             e.printStackTrace();

         }

 }

3.是通过RandomValidateCode生成随机字符串以及图片。下面这个代码可以直接用一下。来自博客:

https://blog.csdn.net/weidong_y/article/details/80630383

public class RandomValidateCode {

public static final String RANDOMCODEKEY = "randomcode_key";//放到session中的key

    private Random random = new Random();

    private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产生的字符串

    private int width = 80;//图片宽

    private int height = 26;//图片高

    private int lineSize = 40;//干扰线数量

    private int stringNum = 4;//随机产生字符数量

    /**

    * 生成随机图片

    */

    public void getRandcode(HttpServletRequest request,

            HttpServletResponse response) {

        HttpSession session = request.getSession();

        //BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类

        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);

        //产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作

        Graphics g = image.getGraphics();

        g.fillRect(0, 0, width, height);

        g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));

        g.setColor(getRandColor(160, 200));

        //绘制干扰线

        for(int i=0;i<=lineSize;i++){

            drowLine(g);

        }

        //绘制随机字符

        String randomString = "";

        for(int i=1;i<=stringNum;i++){

            randomString=drowString(g,randomString,i);

        }

        session.removeAttribute(RANDOMCODEKEY);

        session.setAttribute(RANDOMCODEKEY, randomString);

        g.dispose();

        try {

        //将内存中的图片通过流动形式输出到客户端

            ImageIO.write(image, "JPEG", response.getOutputStream());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    /*

    * 获得字体

    */

    private Font getFont(){

        return new Font("Fixedsys",Font.CENTER_BASELINE,18);

    }

    /*

    * 获得颜色

    */

    private Color getRandColor(int fc,int bc){

        if(fc > 255)

            fc = 255;

        if(bc > 255)

            bc = 255;

        int r = fc + random.nextInt(bc-fc-16);

        int g = fc + random.nextInt(bc-fc-14);

        int b = fc + random.nextInt(bc-fc-18);

        return new Color(r,g,b);

    }

    /*

    * 绘制字符串

    */

    private String drowString(Graphics g,String randomString,int i){

        g.setFont(getFont());

        g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));

        String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));

        randomString +=rand;

        g.translate(random.nextInt(3), random.nextInt(3));

        g.drawString(rand, 13*i, 16);

        return randomString;

    }

    /*

    * 绘制干扰线

    */

    private void drowLine(Graphics g){

        int x = random.nextInt(width);

        int y = random.nextInt(height);

        int xl = random.nextInt(13);

        int yl = random.nextInt(15);

        g.drawLine(x, y, x+xl, y+yl);

    }

    /*

    * 获取随机的字符

    */

    public String getRandomString(int num){

        return String.valueOf(randString.charAt(num));

    }

}

4.至于验证验证码就是我弄了半天的东西。。。

因为我不太会vue 然后写前端研究了会会才知道它是怎么用的。然后我开始是想从前端拿到后端的session,但是vue这个是html页面,不能<%@ %>引入java代码,然后我又试了一下js的ajax引入,但是报错,vue框架我也不太懂。。然后还是交给后端验证嘛。

然后就很简单了,从login那里验证,提交的时候多了一个验证码,但是我觉得这样做其实是不太好的,因为验证码跟登录一起验证,有点耗时,分开比较好。

submitForm(login) {

            this.$refs[login].validate((valid) => {

                if (valid) {

                    this.loadings(); //加载动画

                // window.alert(this.login.code);

                    this.$http.post('/login', {

                        username: this.login.username,

                        password: this.login.password,

                        remember: this.login.remember,

                        code:this.login.code

                    }).then(result => {

                    //window.alert(result);

                        // 判断用户是否登录成功,后端返回JSON格式数据,不然娶不到数据

                        if (result.body.success) {

                        alert("success");

                            window.location.href = "/listStudentInfo";

                            this.loading.close(); //关闭动画加载

                        } else {

                            // 弹出错误信息框

                            this.$emit(

                                'submit-form',

                                this.$message({

                                    message:result.body.message,

                                    type: 'warning',

                                    duration: 6000

                                }),

                            );

                            // 清空表单状态

                            this.$refs[login].resetFields();

                        }

                    });

          }

                else {

                    this.$emit(

                        'submit-form',

                        this.$message({

                            message: '输入信息有误!',

                            type: 'warning',

                            duration: 6000

                        }),

                    );

                    return false;

                }

            });

        },
@RequestMapping("/login")

public Result Login( @RequestParam(value = "username", required = false) String username,

            @RequestParam(value = "password", required = false) String password,

            @RequestParam(value = "remember", required = false) String remember,

            @RequestParam(value = "code", required = false) String code,

            HttpServletRequest request

            ) {

String error = null;

HttpSession session = request.getSession();

  System.out.println(code);

  //System.out.println(session.getAttribute( RandomValidateCode.RANDOMCODEKEY));

if(username==null||session.getAttribute( RandomValidateCode.RANDOMCODEKEY).equals(code)) {

//System.out.println("code 有问题");

return new Result(false, error);

}

//System.out.println(password);

//System.out.println("调试");

Subject subject=SecurityUtils.getSubject();

UsernamePasswordToken token=new UsernamePasswordToken(username,password);

if (remember != null) {

            if (remember.equals("true")) {

                //说明选择了记住我

                token.setRememberMe(true);

            } else {

                token.setRememberMe(false);

            }

        } else {

            token.setRememberMe(false);

        }

System.out.println(token.isRememberMe());

try {

subject.login(token);

Result re=new Result(true, "success");

return new Result(true,error);

} catch (UnknownAccountException e) {

      System.out.println( "登陆出错");

        error = "用户账户不存在,错误信息:" + e.getMessage();

    }catch (IncorrectCredentialsException ex) {

    System.out.println( "用户名和密码不匹配");

    error = "用户名或密码错误,错误信息:" + ex.getMessage();

    }catch (AuthenticationException e) {

    System.out.println( "其他的登陆错误");

    error = "错误信息:" + e.getMessage();

    }

return new Result(false, error);

}

5.session

简单说一下我理解的session和cookie的区别吧,session是保存在服务端的,cookie是保存在客户端的,就是本地会有一个文件夹专门保存cookie。cookie主要是为了保存用户状态嘛,因为http是无状态的连接,每次连接完就不会知道下一次是不是同一个用户。但是保存用户信息在很多应用场景中都是必要的。而session比cookie更加安全,因为session信息保存在服务端的,不容易被盗用。所以重要登陆信息还是应该保存在session上。而且服务端能够保存的session比较大,而单个cookie一般不超过20k.

session是怎么保存用户信息的呢?就是一个用户有一个sessionId,通过sessionId保存用户信息。

session的使用:

session.setAttribute("key","value");

session.getAttribute("key");

6.登陆界面

第一次用博客园写技术博客,以前都是在简书,感觉格式功能比较强,编辑起来看着舒服,有可能以后就在这里写博客了,毕竟程序员的地方嘛

vue+SSM验证码实现的更多相关文章

  1. vue实现验证码倒计时60秒的具体代码

    vue实现验证码倒计时60秒的具体代码 <span v-show="show" @click="getCode">获取验证码</span> ...

  2. vue 登录验证码

    vue 登录验证码 最近在开发pc端项目,配合elementui使用 createCode() { var code = ""; var codeLength = 4; //验证码 ...

  3. VUE 图片验证码

    1. 概述 1.1 说明 在开发过程中,有时候需要使用图片验证码进行增加安全强度,在点击图片时更新新的图片验证码,记录此功能,以便后期使用. 2. 示例 2.1 vue示例代码 <templat ...

  4. vue生成图片验证码

    最近做项目接触Vue,前端生成验证码.原理其实很简单,首先是生成随机数,然后用canvas绘制. 网上有一些现成的资料,没必要重复造轮子,我是在他们基础上完善了父组件,简化了子组件的调用: ident ...

  5. Vue 获取验证码倒计时组件

    子组件 <template> <a class="getvalidate":class="{gray: (!stop)}"@click='cl ...

  6. Vue图片验证码-自定义组件高级版

    最近项目中要用到图片验证码,网上一查有很多,基本都是千篇一律的4位纯数字验证码.首先得感谢那位一代目兄台提供的模板,由于不能满足需求,所以对其进行了改造升级. 经改造的图片验证码能满足一下情形使用:① ...

  7. 学生会管理系统(SSM)vue+ssm+shiro

    数据库 drop database StudentUnionManagementSystem ; create database StudentUnionManagementSystem CHARAC ...

  8. 【SSM 验证码】登录验证码

    LoginController /** * 登陆方法 */ @ResponseBody @RequestMapping("login2") public Map<String ...

  9. vue获取验证码倒计时

    <template> <div> <el-button :disabled="disabled" @click="sendcode" ...

随机推荐

  1. celery_消息队列

    http://www.cnblogs.com/wupeiqi/articles/8796552.html 一. celery 简介 Celery 是一个专注于实时处理和任务调度的分布式任务队列, 同时 ...

  2. 【2019年OCP新题】OCP题库更新出现大量新题-11

    11.Your database is in archivelog mode. You want to disable archiving for the database. Examine thes ...

  3. Python 读取大文件的方式

    对于读取容量小的文件,可以使用下面的方法: with open("path", "r") as f: f.read() 但是如果文件容量很大,高达几个G或者十几 ...

  4. hdoj2604 Queuing(矩阵快速幂)

    此题如果直接利用递推关系,处理不好会超内存的. 首先找出递推关系式,先给出递推关系式:( L )=( L - 1 ) + ( L - 3 ) + ( L - 4 ):可以先尝试推导一下,推不出来再看下 ...

  5. Mybatis的cache

    相关类:org.apache.ibatis.executor.CachingExecutor 相关代码: public <E> List<E> query(MappedStat ...

  6. IntelliJ IDEA 2016 破解旗舰版

    JRebel 授权服务器http://idea.lanyus.com/ilanyulanyu19950316@gmail.com

  7. 【codeforces 24D】损坏的机器人

    题目大意:有一只坏了的机器人站在一个n∗m的网格里,初始位置在(x,y).现在每个单位时间内它会随机选左右下三个方向走,如果它随机的方向会走出网格就不会往这个方向走.当然这个机器人也可能原地停留一个单 ...

  8. Centos7 nginx安装过程

    一.Centos版本: cat /etc/centos-release CentOS Linux release 7.1.1503 (Core) 二.nginx下载 官网:http://nginx.o ...

  9. start with connect by prior

    start with connect by prior的使用: select … from tablename start with 条件1 connect by 条件2 where 条件3; sta ...

  10. Names and Identifiers

    JLS:https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.2 Not all identifiers in a pr ...