首页 > 其他分享 >3.登录发送验证码

3.登录发送验证码

时间:2022-10-30 11:56:53浏览次数:42  
标签:登录 验证码 springframework server 发送 tanhua org import

登录发送验证码

1.接口说明

1665631729848

2.流程分析

客户端发送请求

服务端调用第三方组件发送验证码

验证码发送成功,存入redis

响应客户端,客户端跳转到输入验证码页面

1665631686151

3.代码实现

1665632031716

3.1在tanhua-app-server模块的application.yml配置文件中添加配置

server:
  port: 8080 #服务端口
spring:
  application:
    name: tanhua-app-server #服务名称
  redis: #redis配置
    port: 6379
    host: 192.168.136.160
  cloud: #nacos配置
    nacos:
      discovery:
        server-addr: 192.168.136.160:8848
dubbo:
  registry:
    address: spring-cloud://localhost
  consumer:
    check: false
  cloud:
    subscribed-services: 'tanhua-app-server'
tanhua:
  sms:
    signName: 物流云商
    templateCode: SMS_106590012
    accessKey: LTAI4GKgob9vZ53k2SZdyAC7
    secret: LHLBvXmILRoyw0niRSBuXBZewQ30la

3.2为tanhua-app-server模块新建启动类AppServerApplication

package com.tanhua.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AppServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AppServerApplication.class, args);
    }
}

3.3创建controller包创建LoginController接收请求

package com.tanhua.server.controller;

import com.tanhua.server.service.UserService;
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;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LOginController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public void login(@RequestBody Map map){
        //1.接收参数
        String  phone = (String) map.get("phone");

        //2.调用service
        String randomCode = userService.sendMsm(phone);
    }
}

3.4UserService

package com.tanhua.server.service;

import com.tanhua.autoconfig.template.SmsTemplate;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
public class UserService {

    @Autowired
    private SmsTemplate smsTemplate;  //发送手机验证码模板

    @Autowired
    private RedisTemplate<String ,String> redisTemplate;

    public void sendMsm(String phone) {

        //1.生成六位数的随机号码
        String randomCode = RandomStringUtils.randomNumeric(6);

//        //2.调用手机验证码模板发送手机验证码
//        smsTemplate.sendSms(phone, randomCode);

        //3.将验证码缓存到Redis中,方便后续比对校验
        redisTemplate.opsForValue().set("CHECK_CODE_"+phone,randomCode, Duration.ofMinutes(5));

       
    }
}

标签:登录,验证码,springframework,server,发送,tanhua,org,import
From: https://www.cnblogs.com/zhangdashuaige/p/16840870.html

相关文章

  • 2.封装短信发送服务组件
    封装短信发送服务组件企业开发中,往往将常见工具类封装抽取,以简洁便利的方式供其他工程模块使用。而SpringBoot的自动装配机制可以方便的实现组件抽取。SpringBoot执行流程......
  • JS之循环发送请求代码优化
    需求:将数组中的值作为参数,循环调用新增接口实现批量导入功能。因为接口调用有时间间隔限制,避免误操作,所以需要设置接口请求的间隔时间。原代码如下:constrecursive=(arr,......
  • Javaweb基础复习------Filter相关应用+登录验证案例的使用
    Filter(过滤器)基本步骤:1、定义类,实现Filter接口,并重写其所有方法2、配置Filter拦截资源的路径,在类上定义2WebFilter注解(WebFilter配置的路径,是拦截资源的路径)3、在d......
  • 验证码案例的实现---MyBatis+Session+Cookie
    展示验证码(jsp页面)首先,我们需要自己利用BufferedImage类去生成一张可以变换的验证码图片;之后,我们就可以利用这样一串代码去将验证码里面的内容获取到:这是一串测试代码:O......
  • JAVA-登录
    packagecom.itheima;importjavax.swing.*;publicclassJFrame05{publicstaticvoidmain(String[]args){JFramejf=newJFrame();jf.s......
  • Javaweb基础复习------Cookie+Session案例的实现(登录注册案例)
    Cookie对象的创建--Cookiecookie=newCookie("key","value");发送Cookie:resp.addCookie();获取Cookie数据:req.getCookie("","");Cookie不能直接存储中文需要进行转......
  • 52-ES8-async和await结合发送ajax请求
     ......
  • Npm 使用 Nexus 仓库的登录时候出现授权的问题
    例如,我们在npm登录Nexus的仓库的时候提示错误如下:npmERR!codeE401npmERR!Unabletoauthenticate,need:BASICrealm="SonatypeNexusRepositoryManager"npmER......
  • 利用Kafaka发送系统通知(27)
    事件驱动的方式1需求分析需求:当用户评论帖子,点赞或关注其他用户时,系统给对应的用户发送通知。思路:封装好一个通用的事件类,保存事件的发送者,接受者、事件主题(评论、点......
  • python系列:远程SSH登录并执行命令,获取结果
    需求:机器A上执行Python代码登录连接到机器B,然后在机器B上执行命令实现#!/usr/bin/envpython#coding=utf-8importparamiko,getpass,sys,tracebackclasssshDemo():def......