angular前端生成验证码(ts组件)

angular前端生成验证码(ts组件)

账号登录的时候,添加校验验证码功能可以防止暴力破解密码,增加破解成本,此为前端生成验证码的方法,主要是将随机验证码,随机颜色,随机线条,随机点,再加上随机偏转等生成一个canvas,大家按需取用。

先看效果:
验证码效果图

一、HTML

在html中需添加#标签,标签名自定

<div class="login-picture" id="loginMaskLi">
  <canvas class="picture-canvas" #verifyCode type="text"
  (click)="doInitVerifyCode($event)" disabled="disabled"></canvas>
</div>
二、css

设置需要的长宽样式

.login-picture {
  width: 140px;
  height: 48px;
  .picture-canvas {
    width: 100%;
    // height: 100%;
  }
}
三、ts
import {
  Component,
  ElementRef,
  OnInit,
  Output,
  ViewChild,
  EventEmitter,
} from '@angular/core';

//生成验证图片
@Component({
  selector: 'app-login-picture',
  templateUrl: './login-picture.component.html',
  styleUrls: ['./login-picture.component.scss'],
})
export class LoginPictureComponent implements OnInit {
  @ViewChild('verifyCode', { static: true })
  verifyCode!: ElementRef;
  @Output() private outer = new EventEmitter();
  constructor() {}
  //   public showAuthCode: string;
  public verifyCodeLine: number = 8;//线条数
  public verifyCodeNum: number = 4;//验证码字符长度
  public verifyCodeWidth: any = 140;//验证码宽度
  public verifyCodeHeight: any = 48;//验证码高度
  ngOnInit(): void {}

  // 随机生成验证码
  //生成随机整数
  randomNum(min: any, max: any) {
    return parseInt(Math.random() * (max - min + 1) + min);
  }
  //生成随机颜色
  randomCol(min: any, max: any) {
    const r = this.randomNum(min, max);
    const g = this.randomNum(min, max);
    const b = this.randomNum(min, max);
    return 'rgb(' + r + ',' + g + ',' + b + ')';
  }
  //生成验证码(纯字符串)
  randomStr() {
    let codeStr: string = '';
    const codeArr: any[] = [];
		const randomArray=[0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z'];
    const verifyCodePic = this.verifyCode.nativeElement.getContext('2d');
    for (let i = 0; i < this.verifyCodeNum; i++) {
      const verifyChar = randomArray[this.randomNum(0, randomArray.length - 1)];
      const fontSize = this.randomNum(25, 35);
      const angle = this.randomNum(-30, 30);
      verifyCodePic.font = fontSize + 'px consolas';
      verifyCodePic.textBaseline = 'top';
      verifyCodePic.save();
      verifyCodePic.fillStyle = this.randomCol(50, 150);
      verifyCodePic.translate(30 * i + 15, 15);
      verifyCodePic.rotate((angle * Math.PI) / 180);
      verifyCodePic.fillText(verifyChar, -10, -5);
      verifyCodePic.restore();
      codeArr.push(verifyChar);
    }
    //将验证码抛出给父组件
    codeStr = codeArr.join('');
    this.outer.emit(codeStr);
  }
  
  //随机线条
  randomLine() {
    const verifyCodePic = this.verifyCode.nativeElement.getContext('2d');
    for (let i = 0; i < this.verifyCodeLine; i++) {
      verifyCodePic.beginPath();
      verifyCodePic.moveTo(
        this.randomNum(0, this.verifyCodeWidth),
        this.randomNum(0, this.verifyCodeHeight)
      );
      verifyCodePic.lineTo(
        this.randomNum(0, this.verifyCodeWidth),
        this.randomNum(0, this.verifyCodeHeight)
      );
      verifyCodePic.strokeStyle = this.randomCol(50, 230);
      verifyCodePic.closePath();
      verifyCodePic.stroke();
    }
  }
  //随机噪点
  randomDot() {
    const verifyCodePic = this.verifyCode.nativeElement.getContext('2d');
    const verifyCodeDot = 100;
    for (let i = 0; i < verifyCodeDot; i++) {
      verifyCodePic.beginPath();
      verifyCodePic.arc(
        this.randomNum(0, this.verifyCodeWidth),
        this.randomNum(0, this.verifyCodeHeight),
        1,
        0,
        2 * Math.PI
      );
      verifyCodePic.closePath();
      verifyCodePic.fillStyle = this.randomCol(50, 200);
      verifyCodePic.fill();
    }
  }
  //初始化验证码
  initVerifyCode() {
    const verifyCodePic = this.verifyCode.nativeElement.getContext('2d');
    this.verifyCode.nativeElement.setAttribute('width', this.verifyCodeWidth);
    this.verifyCode.nativeElement.setAttribute('height', this.verifyCodeHeight);
    verifyCodePic.fillStyle = this.randomCol(180, 255);
    verifyCodePic.fillRect(0, 0, this.verifyCodeWidth, this.verifyCodeHeight);
    this.randomStr();
    this.randomLine();
    this.randomDot();
  }
}
四、使用

使用就用正常的angular组件使用方式就行,可以添加#标签来调用initVerifyCode()方法实现验证码刷新。

五、优缺点及解决建议

1.优点是前端直接生成验证码不需要后台参与,不用调用接口。
2.缺点是过不了渗透测试,可以跳过前端界面直接使用postman等接口调试工具直接访问接口暴力破解密码。
3.建议还是后台生成随机字符串验证码,前端拿到验证码生成验证码canvas,然后登录时前端后台共同校验。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Angular是一个流行的前端框架,用于构建Web应用程序。它由Google开发并维护,采用TypeScript语言编写。Angular提供了一套完整的工具和功能,用于开发现代化的单页应用程序(SPA)和响应式Web应用程序。 Angular的主要特点包括: 1. 组件化架构:Angular使用组件化架构来构建应用程序。每个组件都有自己的模板、样式和逻辑,并可以嵌套在其他组件中,以构建复杂的用户界面。 2. 双向数据绑定:Angular支持双向数据绑定,使得数据的变化可以自动反映在视图中,同时用户的输入也可以自动更新数据模型。 3. 依赖注入:Angular使用依赖注入来管理组件之间的依赖关系。这样可以更好地组织和测试代码,并提高代码的可维护性和可扩展性。 4. 路由和导航:Angular提供了强大的路由和导航功能,可以实现页面之间的无缝切换和导航。 5. 响应式表单:Angular提供了响应式表单功能,可以轻松地处理表单的验证、状态管理和用户输入。 6. 强大的工具集:Angular配备了丰富的工具集,包括开发者工具、测试工具和构建工具,可以提高开发效率和代码质量。 下面是一个简单的Angular组件的示例: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-hello', template: ` <h1>Hello, {{ name }}!</h1> <button (click)="changeName()">Change Name</button> `, }) export class HelloComponent { name = 'Angular'; changeName() { this.name = 'World'; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值