目录
1:注册登录需求分析
1.1:业务说明
1.2:需求分析
1.3:数据库表
1.4:实体类
2:发送短信验证码
2.1:登录验证码
2.2:接口说明
2.3:流程分析
2.4:代码实现
2.4.1:mock接口
2.4.2:UserController
2.4.3:UserService
2.4.4:配置文件
2.4.5:启动类
1:注册登录需求分析
1.1:业务说明
业务:
-
用户通过手机验证码进行登录
2.如果是第一次登录则需要完善个人信息,在上传图片时,需要对上传的图片做人像的校验,防止用户上传非人像的图片作为头像。流程完成后,则登录成功。
-
已注册用户:
-
输入手机号发送验证码
-
输入验证码,进行比对完成登录
-
-
未注册用户:
-
输入手机号发送验证码
-
输入验证码,进行比对,自动注册(保存用户)
-
完善用户信息
-
1.2:需求分析
服务端接受客户端请求
Java代码调用第三方服务实现短信返送(发送短信需要运营资质,只能借助第三方实现)
1.3:数据库表
数据库使用的mysql:
CREATE TABLE `tb_user` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`mobile` varchar(11) DEFAULT NULL COMMENT '手机号',`password` varchar(32) DEFAULT NULL COMMENT '密码,需要加密',`created` datetime DEFAULT NULL,`updated` datetime DEFAULT NULL,`hx_user` datetime DEFAULT NULL,`hx_password` datetime DEFAULT NULL, PRIMARY KEY (`id`),KEY `mobile` (`mobile`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='用户表';
1.4:实体类
package com.czxy.tanhua.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;import java.util.Date;/*** <p>* 用户表* </p>** @author 爱吃豆的土豆* @since 2023-03-29*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("tb_user")
public class User extends BasePojo {private static final long serialVersionUID = 1L;@TableId(value = "id", type = IdType.AUTO)private Long id;/*** 手机号*/@TableField("mobile")private String mobile;/*** 密码,需要加密*/@TableField("password")private String password;@TableField("created")private Date created;@TableField("updated")private Date updated;@TableField("hx_user")private String hxUser;@TableField("hx_password")private String hxPassword;}
2:发送短信验证码
2.1:登录验证码
服务端接收客户端请求
Java代码调用第三方服务实现短信返送(发送短信需要运营资质,只能借助第三方实现)
2.2:接口说明
参见YAPI接口地址:http://192.168.136.160:3000/project/19/interface/api/94
2.3:流程分析
客户端发送请求
服务端调用第三方组件发送验证码
验证码发送成功,存入redis
响应客户端,客户端跳转到输入验证码页面
2.4:代码实现
2.4.1:mock接口
地址:http://192.168.136.160:3000/project/19/interface/api/94
2.4.2:UserController
tanhua-app-server
工程编写com.tanhua.server.controller.UserController#login
@RestController
@RequestMapping("/user")
public class LoginController {@Resourceprivate UserService userService;@PostMapping("/login")public ResponseEntity loginUser(@RequestBody Map map){String phone = (String) map.get("phone");userService.loginUser(phone);return ResponseEntity.ok(null);}
}
2.4.3:UserService
tanhua-app-server
工程编写 com.tanhua.server.service.UserService#sendCode
package com.czxy.tanhua.service;import com.czxy.tanhua.autoconfig.template.SmsTemplate;
import com.czxy.tanhua.commons.utils.JwtUtils;
import com.czxy.tanhua.dubbo.api.UserApi;
import com.czxy.tanhua.entity.User;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;/*** @Author 爱吃豆的土豆、* @Date 2023/3/30 9:55*/
@Service
public class UserService {@Resourceprivate SmsTemplate smsTemplate;@Resourceprivate StringRedisTemplate stringRedisTemplate;@DubboReferenceprivate UserApi userApi;/*** 登录用户的验证码* @param phone*/public void loginUser(String phone) {//随机生成验证码String code = RandomStringUtils.randomNumeric(6);//调用短信工具类smsTemplate.sendSms(phone,"",code);String key = "CHECK_CODE"+phone;//存放到redis中stringRedisTemplate.opsForValue().set(key,code,Duration.ofMinutes(5));}}
2.4.4:配置文件
tanhua-app-server
端添加配置文件application.yml
配置文件中包含短信一些配置信息,服务启动进行加载
#服务端口
server:port: 18080
spring:servlet:multipart:max-file-size: 30MBmax-request-size: 30MBapplication:name: tanhua-app-serverredis: #redis配置host: localhostport: 6379rabbitmq:host: 192.168.136.160port: 5672username: guestpassword: guestcloud: #nacos配置nacos:discovery:# server-addr: localhost:8848server-addr: 192.168.136.160:8848config: # TODO 配置中心?server-addr: 192.168.136.160:8848
dubbo: #dubbo配置protocol:name: dubboport: 20880registry:address: spring-cloud://localhostconsumer:check: falseretries: 0application:logger: slf4jcloud:subscribed-services: tanhua-dubbo-db, tanhua-dubbo-mongo
tanhua:sms:signName: 桐叔老司机templateCode: SMS_130929218accessKey: LTAI5tR6UTNzPK1F6CQoqF3Xsecret: REp7cA2pjU30skfqxGym4CkKDbtatJoss:pathProtocol: httpsendpoint: oss-cn-shanghai.aliyuncs.comkeyId: LTAI5t6JCKtfe8wXcXZjpbcVkeySecret: jnbVNSsW50CJnAWfBkXDiUorLFnRajbucketName: tudou-01dirName: avataraip:appId: 26974300apiKey: LgQEvULCyS5nkMWMfp7OfbzlsecretKey: zVNfzu9GZYwX8lfASYEaumYU7dTqVKKihuanxin:appkey: 1181230208170312#tanhuaclientId: YXA6cg-t55-3QPeMhKZWPtKXMAclientSecret: YXA6nMnWCIb3X9ANZXQQ0CoWPUtlaLEdefault:recommend:users: 2,3,8,10,18,20,24,29,27,32,36,37,56,64,75,88green:enable: trueaccessKeyID: LTAI4GKgob9vZ53k2SZdyAC7accessKeySecret: LHLBvXmILRoyw0niRSBuXBZewQ30lascenes: porn,terrorism #色情,暴力
# 分布式文件系统FDFS配置
fdfs:so-timeout: 1500connect-timeout: 600#缩略图生成参数thumb-image:width: 150height: 150#TrackerList参数,支持多个tracker-list: 192.168.136.160:22122web-server-url: http://192.168.136.160:8888/
2.4.5:启动类
/*** @Author 爱吃豆的土豆、* @Date 2023/3/29 11:42*/
@SpringBootApplication
@ServletComponentScan // 开启servlet组件扫描(过滤器扫描)
public class AppServiceApplication {public static void main(String[] args) {SpringApplication.run(AppServiceApplication.class,args);}
}