此次跟随教程完成项目,主要目的是学习一些新的技术,掌握一些开发工具的使用。因此,并不会对事务逻辑进行细致记录。此篇博客仅用于博主本人日后搭建项目时的参考,并非教程
学习redis:
Redis是当前比较流行的一个NoSQL库,这里我们使用它来做短信验证码的缓存。
先来安装,我们把redis装在Linux。首先上传安装包到/home/leyou下,解压:
tar -xvf redis-4.0.9.tar.gz
删除压缩包:
rm -rf redis-4.0.9.tar.gz
对安装目录进行重命名(个人习惯):
mv redis-4.0.9 redis
进入目录并编译安装:
cd redis
make && make install
在安装目录下,进入redis.conf文件并进行相关配置:
vim redis.conf
修改以下配置:
#bind 127.0.0.1 # 将这行代码注释,监听所有的ip地址,外网可以访问
protected-mode no # 把yes改成no,允许外网访问
daemonize yes # 把no改成yes,后台运行
设置开机启动,新建文件:
vim /etc/init.d/redis
进入后输入以下内容:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
#这里需根据自己的安装路径进行配置
CONF="/home/leyou/redis/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
保存退出。
输入命令修改权限:
chmod 755 /etc/init.d/redis
设置开机启动:
chkconfig --add /etc/init.d/redis
chkconfig redis on
手动开启命令:
redis-server redis.conf
关闭可以使用命令:
killall redis-server
到这里,redis的安装和基础配置就完成了,接下来学习一下和redis相关的基础知识。
- @generic:通用指令
- @string:字符串类型指令
- @list:队列结构指令
- @set:set结构指令
- @sorted_set:可排序的set结构指令
- @hash:hash结构指令
其中,除了@generic以外的,对应了redis中常用的5种数据类型:
- String:等同于java中的Map<String,String>
- list:等同于java中的Map<String,List<String,String>>
- set:等同于java中的Map<String,Set<String,String>>
- sort_set:可排序的set
- hash:等同于java中的Map<String,Map<String,String>>
通用指令:
- keys * :查询所有的键
- exists key:查询key是否存在,存在就返回1,否则返回0
- del key:删除key,可以一次性删除多个,例:del k1 k2 k3
- select index: index为一个整数,用来改变库。默认有16个库(0~15)
- expire key seconds:设置key的过期时间,单位为秒,超时后key将被自动删除,设置过期时间后,可以使用命令“ttl key”来查看key的过期时间,过期后ttl key命令的返回值为-2,否则为对应的秒数,未设置过期时间的默认为永久存活,ttl 返回值为-1
- persist key:将key重新设置为永不过期
字符串指令(类似java的Map<String,String>):
- set key value:设置key的值为value
- get key:获取key的值
- incr key:将key中存储的数字自增1
- decr key:将key中存储的数字自减1
- mset k1 v1 k2 v2 k3 v3:同时添加多个key的值
- mget k1 k2 k3:同时获取多个key的值
hash结构命令(Map<String(键),Map<String(字段),String(值)>>):
这里我们称键为key,字段为hKey,字段值为hValue。
- hset key hKey hValue:添加
- hget key hKey:获得相应的hValue
- hgetall key:获得key中所有键值对
- hkeys key:获得key中所有的hKey
- hvalues key:获得key中所有的hValue
- hdel key hKey:删除key中的hKey
java代码访问redis:
我们使用spring为我们提供的SpringDataRedis,在注入template后调用相应方法即可。
创建短信微服务:
我们使用阿里云的短信服务,注册账号申请短信签名的步骤就不在这里记录了,反正我也申请不到。但是如何调用其api还是要记录下来的。我们创建一个短信微服务并引入相关依赖:
- 阿里云短信服务的相关依赖
- 消息队列相关依赖(因短信服务需采用异步调用避免线程阻塞)
- 需要使用配置类
- 自己写的common工程
- springboot的测试依赖
- 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">
<parent>
<artifactId>leyou</artifactId>
<groupId>com.leyou.parent</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.service</groupId>
<artifactId>ly-sms</artifactId>
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.leyou.common</groupId>
<artifactId>ly-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
启动类:
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LySmsApplication {
public static void main(String[] args) {
SpringApplication.run(LySmsApplication.class);
}
}
配置文件,ly.sms为自定义的配置:
server:
port: 8086
spring:
application:
name: sms-service
rabbitmq:
host: 192.168.114.129
username: leyou
password: leyou
virtual-host: /leyou
redis:
host: 192.168.114.129
ly:
sms:
accessKeyId: #自己的accessKeyId
accessKeySecret: #自己的aks
signName: #签名名称
verifyCodeTemplate: #模板名称
添加配置类,成员变量名称应与配置文件中的名称完全相同,并且要在类上添加@ConfigurationProperties注解,括号中填写前缀如下:
package com.leyou.sms.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "ly.sms")
public class SmsProperties {
String accessKeyId;
String accessKeySecret;
String signName;
String verifyCodeTemplate;
}
编写发送短信工具类,其中大部分方法在阿里云官网的说明文档中已经给出,我们直接复制下来再进行小的改动即可。首先我们需要在此类上添加@Component注解便于其他类注入。添加@EnableConfigurationProperties(SmsProperties.class)注解(括号中传入配置类的字节码),只有添加这个注解我们才能使用相关的注解类,并在工具类中注入配置类。另外我们在发送短信前需要判断相同手机号发送短信频率是否过高,并且在成功发送短信后将记录写入redis并设置存活时间为1分钟(发送短信频率这里我们设置为一分钟最多一条)。因此我们还需要在类中注入StringRedisTemplate,工具类如下:
package com.leyou.sms.utils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.leyou.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
@EnableConfigurationProperties(SmsProperties.class)
@Slf4j
public class SmsUtil {
@Autowired
private SmsProperties prop;
@Autowired
private StringRedisTemplate redisTemplate;
//产品名称:云通信短信API产品,开发者无需替换
static final String product = "Dysmsapi";
//产品域名,开发者无需替换
static final String domain = "dysmsapi.aliyuncs.com";
// 此前缀可自定义添加
private static final String KEY_PREFIX="sms:phone";
private static final long SMS_MIN_INTERVAL_IN_MILLIS = 60000;
public SendSmsResponse sendSms(String phoneNumber,String signName,String templateCode,String templateParam) {
String lastTime = redisTemplate.opsForValue().get(KEY_PREFIX + phoneNumber);
if(StringUtils.isNotBlank(lastTime)) {
Long last = Long.valueOf(lastTime);
if(System.currentTimeMillis() - last < SMS_MIN_INTERVAL_IN_MILLIS) {
log.info("[短信服务] 发送短信频率过高,被拦截");
return null;
}
}
try {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", prop.getAccessKeyId(), prop.getAccessKeySecret());
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
request.setMethod(MethodType.POST);
//必填:待发送手机号
request.setPhoneNumbers(phoneNumber);
//必填:短信签名-可在短信控制台中找到
request.setSignName(signName);
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateCode);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(templateParam);
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if (!"OK".equals(sendSmsResponse.getCode())) {
log.info("[短信服务] 发送短信失败,phoneNumber:{},原因:{}", phoneNumber, sendSmsResponse.getMessage());
}
// 发送短信成功后,写入redis,指定生存时间为一分钟
redisTemplate.opsForValue().set(KEY_PREFIX + phoneNumber,String.valueOf(System.currentTimeMillis()),
1,TimeUnit.MINUTES);
return sendSmsResponse;
}catch (Exception e) {
log.error("[短信服务] 发送短信异常",e);
return null;
}
}
}
编写消息监听类:
package com.leyou.sms.mq;
import com.aliyuncs.exceptions.ClientException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.sms.config.SmsProperties;
import com.leyou.sms.utils.SmsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Map;
@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsListener {
@Autowired
private SmsProperties prop;
@Autowired
private SmsUtil smsUtil;
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "sms.verify.code.queue",durable = "true"),
exchange = @Exchange(name = "ly.sms.exchange",type = ExchangeTypes.TOPIC),
key = {"sms.verify.code"}
))
public void list (Map<String,String> msg) {
if(CollectionUtils.isEmpty(msg)) {
return;
}
String phone = msg.remove("phone");
if(StringUtils.isBlank(phone)) {
return;
}
smsUtil.sendSms(phone,prop.getSignName(),prop.getVerifyCodeTemplate(), JsonUtils.serialize(msg));
}
}
短信微服务就完成了。
创建用户中心:
用户微服务同样需要像商品微服务一样包含两个子工程,interface工程和service工程,父工程需在pom.xml更改打包方式为pom。
interface工程需要提供接口供其他服务接口继承后添加feign接口调用,因此需要有webmvc依赖,user实体类需要添加和数据库相关的注解,并使用hibernate-validator对前端传来的数据进行校验,返回实体类时,我们不希望springmvc将用户密码一并返回给前端,所以需要添加注解@JsonIgnore,使用这个注解同样需要引入相关依赖。因此,interface工程的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">
<parent>
<artifactId>ly-user</artifactId>
<groupId>com.leyou.service</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.service</groupId>
<artifactId>ly-user-interface</artifactId>
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-core</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
</project>
对外提供的api接口:
package com.leyou.user.api;
import com.leyou.user.pojo.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
public interface UserApi {
@GetMapping("/query")
User queryUserByUsernameAndPassword(
@RequestParam("username") String username,
@RequestParam("password") String password
);
}
user实体类:
package com.leyou.user.pojo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.util.Date;
@Data
@Table(name = "tb_user")
public class User {
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
@NotEmpty
@Length(min = 4, max = 30, message = "用户名只能在4~30位之间")
private String username;// 用户名
@JsonIgnore
@Length(min = 4, max = 30, message = "用户名只能在4~30位之间")
private String password;// 密码
@Pattern(regexp = "^1[35678]\\d{9}$", message = "手机号格式不正确")
private String phone;// 电话
private Date created;// 创建时间
@JsonIgnore
private String salt;// 密码的盐值
}
这样我们的interface工程就暂时搭建到这里。接下来搭建service工程。
service工程中需要用到md5加密,因此引入依赖commons-codec,其他没有新出现的依赖,就不再详细记录,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">
<parent>
<artifactId>ly-user</artifactId>
<groupId>com.leyou.service</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.service</groupId>
<artifactId>ly-user-service</artifactId>
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.leyou.common</groupId>
<artifactId>ly-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.leyou.service</groupId>
<artifactId>ly-user-interface</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
</dependencies>
</project>
启动类:
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.leyou.user.mapper")
public class LyUserApplication {
public static void main(String[] args) {
SpringApplication.run(LyUserApplication.class);
}
}
配置文件:
server:
port: 8085
spring:
application:
name: user-service
datasource:
url: jdbc:mysql://127.0.0.1:3306/yun6
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
rabbitmq:
host: 192.168.114.129
username: leyou
password: leyou
virtual-host: /leyou
redis:
host: 192.168.114.129
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
mybatis:
type-aliases-package: com.leyou.user.pojo
创建后记得去网关的配置文件中增添相应的路由信息。
首先我们添加一个加密工具类:
package com.leyou.user.utils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.UUID;
/**
* @author: HuYi.Zhang
* @create: 2018-04-30 15:56
**/
public class CodecUtils {
public static String md5Hex(String data,String salt) {
if (StringUtils.isBlank(salt)) {
salt = data.hashCode() + "";
}
return DigestUtils.md5Hex(salt + DigestUtils.md5Hex(data));
}
public static String shaHex(String data, String salt) {
if (StringUtils.isBlank(salt)) {
salt = data.hashCode() + "";
}
return DigestUtils.sha512Hex(salt + DigestUtils.sha512Hex(data));
}
public static String generateSalt(){
return StringUtils.replace(UUID.randomUUID().toString(), "-", "");
}
}
userMapper:
package com.leyou.user.mapper;
import com.leyou.user.pojo.User;
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User>{
}
userService需要注意的是我们在通过rabbitmq进行广播时,消息中仅能传入一个参数,但是我们需要告知短信微服务手机号码和验证码是多少,所以我们将多个参数放入一个map中传递过去,发送后,我们将验证码存入redis中并设置存活时长为5分钟,如下:
package com.leyou.user.service;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.NumberUtils;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.pojo.User;
import com.leyou.user.utils.CodecUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private AmqpTemplate amqpTemplate;
@Autowired
private StringRedisTemplate redisTemplate;
private static final String KEY_PREFIX = "user:verify:phone";
public Boolean checkData(String data, Integer type) {
User record = new User();
switch (type) {
case 1:
record.setUsername(data);
break;
case 2:
record.setPhone(data);
break;
default:
throw new LyException(ExceptionEnum.INVALID_USER_DATA_TYPE);
}
return userMapper.selectCount(record) == 0;
}
public void sendCode(String phone) {
// 生成key
String key = KEY_PREFIX + phone;
// 生成验证码
String code = NumberUtils.generateCode(6);
HashMap<String, String> msg = new HashMap<>();
msg.put("phone",phone);
msg.put("code",code);
// 发送验证码
amqpTemplate.convertAndSend("ly.sms.exchange","sms.verify.code",msg);
// 保存验证码
redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
}
public void register(User user, String code) {
// 校验验证码
String cacheCode = redisTemplate.opsForValue().get(KEY_PREFIX + user.getPhone());
if(!StringUtils.equals(code,cacheCode)) {
throw new LyException(ExceptionEnum.INVALID_VERIFY_CODE);
}
// 生成加密密码,引入依赖后直接使用工具类即可
// 生成盐
String salt = CodecUtils.generateSalt();
user.setSalt(salt);
// 对密码加密
user.setPassword(CodecUtils.md5Hex(user.getPassword(),salt));
// 数据写入数据库
user.setCreated(new Date());
userMapper.insert(user);
}
public User queryUserByUsernameAndPassword(String username, String password) {
// 查询用户
User record = new User();
record.setUsername(username);
User user = userMapper.selectOne(record);
if(user == null) {
throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
}
String word = CodecUtils.md5Hex(password, user.getSalt());
if(!StringUtils.equals(word,user.getPassword())) {
throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
}
return user;
}
}
web层,需要注意的是,在注册时,前端会传来一个user对象,user实体类我们使用hibernate validator进行了校验,所以在接收user时我们需要在前面添加@Valid注解,只有添加了此注解,才会自动进行校验:
package com.leyou.user.web;
import com.leyou.user.pojo.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/check/{data}/{type}")
public ResponseEntity<Boolean> checkData(
@PathVariable("data") String data,@PathVariable("type") Integer type) {
return ResponseEntity.ok(userService.checkData(data,type));
}
@PostMapping("/code")
public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone) {
userService.sendCode(phone);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
@PostMapping("/register")
public ResponseEntity<Void> register(@Valid User user , @RequestParam("code") String code) {
userService.register(user,code);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@GetMapping("/query")
public ResponseEntity<User> queryUserByUsernameAndPassword(
@RequestParam("username") String username,
@RequestParam("password") String password
) {
return ResponseEntity.ok(userService.queryUserByUsernameAndPassword(username,password));
}
}
第五部分到此为止