Redis實(shí)現(xiàn)短信驗(yàn)證碼登錄的示例代碼
效果圖
發(fā)送驗(yàn)證碼

輸入手機(jī)號、密碼以及驗(yàn)證碼完成登錄操作

pom.xml
核心依賴
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</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-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
applicatoin.yml
server:
port: 8080
spring:
application:
name: redis-lettuce
datasource:
url: jdbc:mysql://aliyun-rds.mysql.rds.aliyuncs.com/illness?useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# useSSL : 一般通過證書或者令牌進(jìn)行安全認(rèn)證,否則通過賬號和密碼進(jìn)行連接
redis:
database: 0 #Redis索引0~15,默認(rèn)為0
host: ip
port: 6379
password: 123456 #密碼(默認(rèn)為空)
lettuce: # 這里標(biāo)明使用lettuce配置
pool:
max-active: 8 #連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
max-wait: -1ms #連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
max-idle: 5 #連接池中的最大空閑連接
min-idle: 0 #連接池中的最小空閑連接
timeout: 10000ms #連接超時時間(毫秒)
Redis配置類
/**
* Redis配置類
*
* @author issavior
*/
@Configuration
public class RedisConf {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
// 創(chuàng)建Template
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 設(shè)置連接工廠
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 設(shè)置序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// key和 hashKey采用 string序列化
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
// value和 hashValue采用 JSON序列化
redisTemplate.setValueSerializer(jsonRedisSerializer);
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
return redisTemplate;
}
}controller
/**
* @author issavior
*/
@RestController
@RequestMapping("/user")
public class UserController {
/**
* userService
*/
@Autowired
private UserService userService;
/**
* 登錄
*
* @param issa issa
* @return ResponseEntity<Issa>
*/
@PostMapping("/login")
public ResponseEntity<Object> login(@RequestBody Issa issa) {
return userService.login(issa);
}
/**
* 獲取驗(yàn)證碼
*
* @param phone phone
* @return ResponseEntity<Object>
*/
@GetMapping("/{phone}")
public ResponseEntity<Object> verificationCode(@PathVariable String phone) {
return ResponseEntity.ok(userService.verificationCode(phone));
}
/**
* 注冊
*
* @param issa issa
* @return ResponseEntity<Object>
*/
@PostMapping
public ResponseEntity<Object> register(@RequestBody Issa issa) {
issa.setId(UUID.randomUUID().toString(true));
return userService.register(issa);
}serviceImpl
/**
* @author issavior
*/
@Service
@Slf4j
public class UserServiceImpl implements UserService {
/**
* redisTemplate
*/
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* userMapper
*/
@Autowired
private UserMapper userMapper;
/**
* 登錄
*
* @param issa 登錄的參數(shù)
* @return ResponseEntity<Object>
*/
@Override
public ResponseEntity<Object> login(Issa issa) {
Issa user = userMapper.getUser(issa);
if (user == null) {
return ResponseEntity.status(400).body("手機(jī)號或密碼錯誤");
}
String phone = issa.getPhone();
String verificationCode = (String) redisTemplate.opsForValue().get("login:" + phone);
String verifyCode = issa.getVerifyCode();
if (!Objects.equals(verifyCode, verificationCode)) {
return ResponseEntity.status(400).body("請輸入正確的驗(yàn)證碼");
}
return ResponseEntity.ok("登錄成功");
}
/**
* 注冊
*
* @param issa 注冊的參數(shù)
* @return ResponseEntity<Object>
*/
@Override
public ResponseEntity<Object> register(Issa issa) {
int user = userMapper.insertUser(issa);
if (user != 1) {
return ResponseEntity.status(400).body("注冊失敗");
}
return ResponseEntity.ok("注冊成功");
}
/**
* 獲取驗(yàn)證碼
*
* @param phone 手機(jī)號
* @return Object
*/
@Override
public Object verificationCode(String phone) {
String randomCode = RandomUtil.randomNumbers(6);
redisTemplate.opsForValue().set("login:" + phone, randomCode);
log.info("驗(yàn)證碼已經(jīng)存入進(jìn)redis服務(wù)器中:" + randomCode);
String code = (String) redisTemplate.opsForValue().get("login:" + phone);
if (code == null) {
return "驗(yàn)證碼獲取失敗";
}
return "驗(yàn)證碼獲取成功【 " + code + " 】";
}
}
mapper
/**
* @author issavior
*/
public interface UserMapper {
/**
* 新增用戶&注冊
*
* @param issa 參數(shù)
* @return int:新增成功返回1,否則返回0
*/
@Insert("insert into issa(id,user_name,password,nick_name,sex,age,phone) values (#{id},#{userName},#{password},#{nickName},#{sex},#{age},#{phone})")
int insertUser(Issa issa);
/**
* 用于登錄時,根據(jù)手機(jī)號和密碼判斷是否有相關(guān)用戶
*
* @param issa issa
* @return int
*/
@Select("select * from issa where phone = #{phone} and password = #{password}")
Issa getUser(Issa issa);
}
以上就是Redis實(shí)現(xiàn)短信驗(yàn)證碼登錄的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Redis短信驗(yàn)證碼登錄的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Redis中的Hash和List類型常用命令及應(yīng)用分析
Redis內(nèi)存數(shù)據(jù)庫中,除了基礎(chǔ)的String類型外,Hash和List也是開發(fā)中頻繁使用的兩種數(shù)據(jù)結(jié)構(gòu),下面詳細(xì)講解Hash和List類型的特點(diǎn)、常用命令及典型應(yīng)用場景,以便處理復(fù)雜業(yè)務(wù)需求,需要的朋友跟隨小編一起看看吧2025-08-08
基于redis 7.2.3的makefile源碼解讀學(xué)習(xí)
這篇文章主要為大家介紹了基于redis 7.2.3的makefile源碼解讀學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Redis 配置文件使用建議redis.conf 從入門到實(shí)戰(zhàn)
Redis配置方式包括配置文件、命令行參數(shù)、運(yùn)行時CONFIG命令,支持動態(tài)修改參數(shù)及持久化,常用項(xiàng)涉及端口、綁定、內(nèi)存策略等,版本8.0后配置文件更完整,緩存場景建議使用LRU淘汰策略,本文給大家介紹Redis 配置文件使用建議redis.conf 從入門到實(shí)戰(zhàn),感興趣的朋友一起看看吧2025-06-06
Redis 哨兵機(jī)制及配置實(shí)現(xiàn)
本文主要介紹了Redis 哨兵機(jī)制及配置實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Spring Boot整合Redis實(shí)現(xiàn)訂單超時處理問題
這篇文章主要介紹了Spring Boot整合Redis實(shí)現(xiàn)訂單超時處理,通過這個基本的示例,你可以了解如何使用Spring Boot和Redis來處理訂單超時問題,并根據(jù)需要進(jìn)行擴(kuò)展和定制,需要的朋友可以參考下2023-11-11
Redis內(nèi)存碎片率調(diào)優(yōu)處理方式
Redis集群因內(nèi)存碎片率超過1.5觸發(fā)告警,分析發(fā)現(xiàn)內(nèi)因與外因?qū)е聝?nèi)存碎片,內(nèi)因?yàn)椴僮飨到y(tǒng)內(nèi)存分配機(jī)制,外因?yàn)镽edis操作特性,使用Redis內(nèi)置內(nèi)存碎片清理機(jī)制可有效降低碎片率,但需注意可能影響性能,建議使用MEMORY命令診斷內(nèi)存使用情況,合理配置參數(shù)以優(yōu)化性能2024-09-09

