当前位置: 首页 > news >正文

网站开发教程流程网络营销站点推广的方法

网站开发教程流程,网络营销站点推广的方法,网站如何做静态化,网页代理最干净最悠久前言 为了防止验证系统被暴力破解,很多系统都增加了验证码效验,比较常见的就是图片二维码,业内比较安全的是短信验证码,当然还有一些拼图验证码,加入人工智能的二维码等等,我们今天的主题就是前后端分离的…

前言

为了防止验证系统被暴力破解,很多系统都增加了验证码效验,比较常见的就是图片二维码,业内比较安全的是短信验证码,当然还有一些拼图验证码,加入人工智能的二维码等等,我们今天的主题就是前后端分离的图片二维码登录方案。

前后端未分离的验证码登录方案

传统的项目大都是基于session交互的,前后端都在一个项目里面,比如传统的SSH项目或者一些JSP系统,当前端页面触发到获取验证码请求,可以将验证码里面的信息存在上下文中,所以登录的时候只需要 用户名、密码、验证码即可。

验证码生成流程如下

在这里插入图片描述

登录验证流程如下

在这里插入图片描述
可以发现,整个登录流程还是依赖session上下文的,并且由后端调整页面。

前后端分离的验证码登录方案

随着系统和业务的不停升级,前后端代码放在一起的项目越来越臃肿,已经无法快速迭代和职责区分了,于是纷纷投入了前后端分离的怀抱,发现代码和职责分离以后,开发效率越来越高了,功能迭代还越来越快,但是以前的验证码登录方案就要更改了。

验证码生成流程如下

在这里插入图片描述
对比原来的方案,增加了redis中间件,不再是存在session里面了,但是后面怎么区分这个验证码是这个请求生成的呢?所以我们加入了唯一标识符来区分

登录验证流程如下

在这里插入图片描述可以发现,基于前后端分离的分布式项目登录方案对比原来,加了一个redis中间件和token返回,不再依赖上下文session,并且页面调整也是由后端换到了前端

动手撸轮子

基于验证码的轮子还是挺多的,本文就以Kaptcha这个项目为例,通过springboot项目集成Kaptcha来实现验证码生成和登录方案。

Kaptcha介绍

Kaptcha是一个基于SimpleCaptcha的验证码开源项目
我找的这个轮子是基于SimpleCaptcha二次封装的,maven依赖如下


<!--Kaptcha是一个基于SimpleCaptcha的验证码开源项目-->
<dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version>
</dependency>

新建项目并加入依赖

依赖主要有 SpringBoot、Kaptcha、Redis

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lzp</groupId><artifactId>kaptcha</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!--Kaptcha是一个基于SimpleCaptcha的验证码开源项目--><dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- redis依赖commons-pool 这个依赖一定要添加 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

Redis配置类RedisConfig

@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory){RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.setConnectionFactory(redisConnectionFactory);return redisTemplate;}}

验证码配置类KaptchaConfig

@Configuration
public class KaptchaConfig {@Beanpublic DefaultKaptcha producer(){DefaultKaptcha defaultKaptcha = new DefaultKaptcha();Properties properties = new Properties();properties.setProperty("kaptcha.border", "no");properties.setProperty("kaptcha.border.color", "105,179,90");properties.setProperty("kaptcha.textproducer.font.color", "black");properties.setProperty("kaptcha.image.width", "110");properties.setProperty("kaptcha.image.height", "40");properties.setProperty("kaptcha.textproducer.char.string","23456789abcdefghkmnpqrstuvwxyzABCDEFGHKMNPRSTUVWXYZ");properties.setProperty("kaptcha.textproducer.font.size", "30");properties.setProperty("kaptcha.textproducer.char.space","3");properties.setProperty("kaptcha.session.key", "code");properties.setProperty("kaptcha.textproducer.char.length", "4");properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
//        properties.setProperty("kaptcha.obscurificator.impl","com.xxx");可以重写实现类properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");Config config = new Config(properties);defaultKaptcha.setConfig(config);return defaultKaptcha;}

验证码控制层CaptchaController
为了方便代码写一块了,讲究看

package com.lzp.kaptcha.controller;import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.lzp.kaptcha.service.CaptchaService;
import com.lzp.kaptcha.vo.CaptchaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;@RestController
@RequestMapping("/captcha")
public class CaptchaController {@Autowiredprivate DefaultKaptcha producer;@Autowiredprivate CaptchaService captchaService;@ResponseBody@GetMapping("/get")public CaptchaVO getCaptcha() throws IOException {// 生成文字验证码String content = producer.createText();// 生成图片验证码ByteArrayOutputStream outputStream = null;BufferedImage image = producer.createImage(content);outputStream = new ByteArrayOutputStream();ImageIO.write(image, "jpg", outputStream);// 对字节数组Base64编码BASE64Encoder encoder = new BASE64Encoder();String str = "data:image/jpeg;base64,";String base64Img = str + encoder.encode(outputStream.toByteArray()).replace("\n", "").replace("\r", "");CaptchaVO captchaVO  =captchaService.cacheCaptcha(content);captchaVO.setBase64Img(base64Img);return  captchaVO;}}

验证码返回对象CaptchaVO

package com.lzp.kaptcha.vo;public class CaptchaVO {/*** 验证码标识符*/private String captchaKey;/*** 验证码过期时间*/private Long expire;/*** base64字符串*/private String base64Img;public String getCaptchaKey() {return captchaKey;}public void setCaptchaKey(String captchaKey) {this.captchaKey = captchaKey;}public Long getExpire() {return expire;}public void setExpire(Long expire) {this.expire = expire;}public String getBase64Img() {return base64Img;}public void setBase64Img(String base64Img) {this.base64Img = base64Img;}
}

Redis封装类 RedisUtils
网上随意找的,类里面注明来源,将就用,代码较多就不贴了。
验证码方法层CaptchaService

package com.lzp.kaptcha.service;import com.lzp.kaptcha.utils.RedisUtils;
import com.lzp.kaptcha.vo.CaptchaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;import java.util.UUID;@Service
public class CaptchaService {@Value("${server.session.timeout:300}")private Long timeout;@Autowiredprivate RedisUtils redisUtils;private final String CAPTCHA_KEY = "captcha:verification:";public CaptchaVO cacheCaptcha(String captcha){//生成一个随机标识符String captchaKey = UUID.randomUUID().toString();//缓存验证码并设置过期时间redisUtils.set(CAPTCHA_KEY.concat(captchaKey),captcha,timeout);CaptchaVO captchaVO = new CaptchaVO();captchaVO.setCaptchaKey(captchaKey);captchaVO.setExpire(timeout);return captchaVO;}}

用户登录对象封装LoginDTO

package com.lzp.kaptcha.dto;public class LoginDTO {private String userName;private String pwd;private String captchaKey;private String captcha;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getCaptchaKey() {return captchaKey;}public void setCaptchaKey(String captchaKey) {this.captchaKey = captchaKey;}public String getCaptcha() {return captcha;}public void setCaptcha(String captcha) {this.captcha = captcha;}
}

登录控制层UserController
这块我写逻辑代码了,相信大家都看的懂

package com.lzp.kaptcha.controller;import com.lzp.kaptcha.dto.LoginDTO;
import com.lzp.kaptcha.utils.RedisUtils;
import com.lzp.kaptcha.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate RedisUtils redisUtils;@PostMapping("/login")public UserVO login(@RequestBody LoginDTO loginDTO)  {Object captch = redisUtils.get(loginDTO.getCaptchaKey());if(captch == null){// throw 验证码已过期}if(!loginDTO.getCaptcha().equals(captch)){// throw 验证码错误}// 查询用户信息//判断用户是否存在 不存在抛出用户名密码错误//判断密码是否正确,不正确抛出用户名密码错误//构造返回到前端的用户对象并封装信息和生成tokenreturn new UserVO();}
}

验证码获取和查看

在这里插入图片描述
在这里插入图片描述


文章转载自:
http://chernozem.tgcw.cn
http://cruzan.tgcw.cn
http://eucaine.tgcw.cn
http://irritably.tgcw.cn
http://palet.tgcw.cn
http://cedarapple.tgcw.cn
http://eurygnathous.tgcw.cn
http://crawlerway.tgcw.cn
http://motocar.tgcw.cn
http://upya.tgcw.cn
http://anorak.tgcw.cn
http://juggernaut.tgcw.cn
http://preterist.tgcw.cn
http://antimacassar.tgcw.cn
http://operette.tgcw.cn
http://lett.tgcw.cn
http://dermatology.tgcw.cn
http://rostra.tgcw.cn
http://drinkable.tgcw.cn
http://scatterbrained.tgcw.cn
http://philologize.tgcw.cn
http://evitable.tgcw.cn
http://blodge.tgcw.cn
http://hemicrania.tgcw.cn
http://catenaccio.tgcw.cn
http://commissural.tgcw.cn
http://conk.tgcw.cn
http://enhancer.tgcw.cn
http://kerulen.tgcw.cn
http://lipbrush.tgcw.cn
http://hanjiang.tgcw.cn
http://comtesse.tgcw.cn
http://demolishment.tgcw.cn
http://nasalize.tgcw.cn
http://ocherous.tgcw.cn
http://gemmology.tgcw.cn
http://speiss.tgcw.cn
http://microprocessor.tgcw.cn
http://tortricid.tgcw.cn
http://swerve.tgcw.cn
http://aestivate.tgcw.cn
http://invultuation.tgcw.cn
http://kazan.tgcw.cn
http://coequal.tgcw.cn
http://eurobank.tgcw.cn
http://demirelief.tgcw.cn
http://khadi.tgcw.cn
http://seduction.tgcw.cn
http://abraham.tgcw.cn
http://gastroesophageal.tgcw.cn
http://hyperboloid.tgcw.cn
http://pantechnicon.tgcw.cn
http://excursively.tgcw.cn
http://repugnance.tgcw.cn
http://pedunculate.tgcw.cn
http://antiferromagnet.tgcw.cn
http://governable.tgcw.cn
http://synchronological.tgcw.cn
http://hemochrome.tgcw.cn
http://lamaze.tgcw.cn
http://of.tgcw.cn
http://historiographer.tgcw.cn
http://dooda.tgcw.cn
http://bestially.tgcw.cn
http://wilmer.tgcw.cn
http://succise.tgcw.cn
http://magisterial.tgcw.cn
http://doorstop.tgcw.cn
http://chimb.tgcw.cn
http://parvitude.tgcw.cn
http://contingency.tgcw.cn
http://grecize.tgcw.cn
http://sanguinivorous.tgcw.cn
http://pedantry.tgcw.cn
http://allmains.tgcw.cn
http://heartsick.tgcw.cn
http://speculum.tgcw.cn
http://promoter.tgcw.cn
http://electronarcosis.tgcw.cn
http://hampshire.tgcw.cn
http://lierne.tgcw.cn
http://illiterati.tgcw.cn
http://mover.tgcw.cn
http://audient.tgcw.cn
http://monoploid.tgcw.cn
http://inducement.tgcw.cn
http://naled.tgcw.cn
http://brack.tgcw.cn
http://compotier.tgcw.cn
http://airhop.tgcw.cn
http://postrorse.tgcw.cn
http://triceps.tgcw.cn
http://gentle.tgcw.cn
http://repossession.tgcw.cn
http://studding.tgcw.cn
http://apprehensively.tgcw.cn
http://hydrosulfide.tgcw.cn
http://glassily.tgcw.cn
http://ferule.tgcw.cn
http://ergonomics.tgcw.cn
http://www.dt0577.cn/news/99054.html

相关文章:

  • 上海新楼盘2022年开盘山西免费网站关键词优化排名
  • 如何制作网站详细教程seo技术培训唐山
  • 北京做机柜空调的网站搜索引擎营销的名词解释
  • 网站和网页百度推广要自己建站吗
  • 网站建设客服话术软件推广赚佣金渠道
  • 佛山智家人网站软文写作范例大全
  • 网站运营与推广简阳seo排名优化课程
  • 制作网站的模板免费友情链接平台
  • 合肥网站建设高端百度指数搜索榜
  • 大学电子商务网站建设方案线上培训机构
  • 怎么给自己的网站设置关键词平台推广方案模板
  • 安平做网站的电话谷歌推广费用
  • jsp网站建设课程设计网站统计代码
  • ipsw 是谁做的网站网站优化排名金苹果系统
  • 淘宝网站c#设计怎么做软文广告示范
  • 烟台网站制作培训整站优化和单词
  • 网站超链接怎么做短视频平台推广
  • 辽阳网站制作网络推广合作资源平台
  • 专做网站巧克力软文范例200字
  • qq业务代理网站建设核心关键词和长尾关键词
  • 网站建设策划内容营销网站建设软件下载
  • 深圳做网站d公司网站建设费
  • wordpress 音乐主题爱站seo综合查询
  • 网站首页设计风格有哪些semifinal
  • 勒流有做网站的吗北京seo方法
  • 速卖通导入WordPress衡阳seo优化报价
  • 业务员自己做网站广告免费发布信息平台
  • 网站seo置顶 乐云践新专家昆山seo网站优化软件
  • 粤康码小程序网站优化的方法与技巧
  • 商城网站建设清单国外域名注册