時(shí)間久了 大多解釋都在注釋里面 回顧一下:
用戶登錄時(shí)需要用手機(jī)號生成驗(yàn)證碼尺借,然后用驗(yàn)證碼進(jìn)行登錄
這里先通過手機(jī)號生成驗(yàn)證碼精拟,存儲(chǔ)在redis里,登錄時(shí)我們?nèi)ミM(jìn)行判斷是否與redis中的相同
上代碼:
配置項(xiàng)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.liyanyan</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web核心依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql數(shù)據(jù)庫驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- pageHelper Springboot和Spring引用的包不一樣 這里請注意 要不然分頁不起作用 引用的是Spring包的話會(huì)一直查出所有的值 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<!--集成redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!-- fastjson json轉(zhuǎn)換器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主要用的是集成 redis 的pom文件 導(dǎo)入后進(jìn)行maven --> reimport
server.port=8080
# spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shope_springboot?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations= classpath:mapper/*.xml
mybatis.type-aliases-package= com.liyanyan.test.pojo
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
# Redis服務(wù)器地址
spring.redis.host=127.0.0.1
# Redis數(shù)據(jù)庫索引(默認(rèn)為0)
spring.redis.database=0
# Redis服務(wù)器連接端口號
spring.redis.port=6379
# 連接池中最大連接數(shù)(使用負(fù)值則表示沒有限制)
spring.redis.jedis.pool.max-active=8
# 連接池中最大阻塞等待時(shí)間(使用負(fù)值則表示沒有限制)
spring.redis.jedis.pool.max-wait=-1ms
# 連接池中的最大空閑連接
spring.redis.jedis.pool.max-idle=8
# 連接池中的最小空閑連接
spring.redis.jedis.pool.min-idle=0
# 連接超時(shí)時(shí)間
spring.redis.timeout=3000ms
上面為application.properties 配置文件 我們用到的也就只是redis相關(guān)的配置
包裝返回結(jié)果
CommonResult
package com.liyanyan.tiny3.api;
public class CommonResult<T> {
private long code;
private String name;
private T data;
public long getCode() {
return code;
}
public void setCode(long code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public CommonResult() {
}
public CommonResult(long code, String name, T data) {
this.code = code;
this.name = name;
this.data = data;
}
public CommonResult(ResultCode resultCode, T data) {
this.code = resultCode.getCode();
this.name = resultCode.getMessage();
this.data = data;
}
public CommonResult(ResultCode resultCode) {
this.code = resultCode.getCode();
this.name = resultCode.getMessage();
}
}
ResultCode
package com.liyanyan.tiny3.api;
public enum ResultCode {
SUCCESS(200, "操作成功"),
FAILED(500, "操作失敗"),
VALIDATE_FAILED(404, "參數(shù)教研失敗"),
UNAUTHORIZED(401, "暫未登錄或token已過期"),
FORBIDDEN(403, "沒有相關(guān)權(quán)限");
private long code;
private String message;
ResultCode(long code, String message) {
this.code = code;
this.message = message;
}
public long getCode() {
return code;
}
public void setCode(long code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Controller層
UmsMemberController
package com.liyanyan.tiny3.controller;
import com.liyanyan.tiny3.api.CommonResult;
import com.liyanyan.tiny3.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* 添加根據(jù)電話號碼獲取驗(yàn)證碼的接口和校驗(yàn)碼的接口
*
* 會(huì)員登錄注冊管理
*/
@RestController
@Api(tags = "UmsMemberController", description = "會(huì)員登錄注冊管理")
@RequestMapping("/sso")
public class UmsMemberController {
@Autowired
private UmsMemberService umsMemberService;
@ApiOperation("獲取驗(yàn)證碼")
@GetMapping("/getAuthCode")
public CommonResult getAuthCode(@RequestParam String telephone) {
return umsMemberService.generateAuthCode(telephone);
}
@ApiOperation("判斷驗(yàn)證碼是否正確")
@PostMapping("/verifyAuthCode")
public CommonResult verifyAuthCode(@RequestParam String telephone, @RequestParam String authCode) {
return umsMemberService.verifyAuthCode(telephone, authCode);
}
}
Service層
RedisService
package com.liyanyan.tiny3.service;
/**
* redis操作Service
* 對象和數(shù)組都以json進(jìn)行存儲(chǔ)
*/
public interface RedisService {
/**
* 存儲(chǔ)數(shù)據(jù)
*/
void set(String key, String value);
/**
* 獲取數(shù)據(jù)
*/
String get(String key);
/**
* 設(shè)置超期時(shí)間
*/
boolean expire(String key, long expire);
/**
* 刪除數(shù)據(jù)
*/
void remove(String key);
/**
* 自增操作
*/
Long increment(String key, long date);
}
UmsMemberService
package com.liyanyan.tiny3.service;
import com.liyanyan.tiny3.api.CommonResult;
public interface UmsMemberService {
/**
* 生成驗(yàn)證碼
* @param telephone
* @return
*/
CommonResult generateAuthCode(String telephone);
/**
* 判斷驗(yàn)證碼和手機(jī)號碼能否匹配的上
* @param telephone
* @param authCode
* @return
*/
CommonResult verifyAuthCode(String telephone, String authCode);
}
RedisServiceImpl
package com.liyanyan.tiny3.service.impl;
import com.liyanyan.tiny3.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* redis 操作Service 的實(shí)現(xiàn)類 注入stringRedisTemplate坛吁,實(shí)現(xiàn)RedisService接口
*/
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
@Override
public boolean expire(String key, long expire) {
return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
@Override
public void remove(String key) {
stringRedisTemplate.delete(key);
}
@Override
public Long increment(String key, long delta) {
return stringRedisTemplate.opsForValue().increment(key, delta);
}
}
UmsMemberServiceImpl
package com.liyanyan.tiny3.service.impl;
import com.liyanyan.tiny3.api.CommonResult;
import com.liyanyan.tiny3.api.ResultCode;
import com.liyanyan.tiny3.service.RedisService;
import com.liyanyan.tiny3.service.UmsMemberService;
import com.liyanyan.tiny3.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Random;
/**
* 會(huì)員管理Service實(shí)現(xiàn)類
* 生成驗(yàn)證碼時(shí)铐尚,將自定義的Redis的前綴 加上 手機(jī)號作為 一個(gè)redis的key ,生成的驗(yàn)證碼為這個(gè)key對應(yīng)的value值 并存在redis中玫膀,而且設(shè)置了過期時(shí)間
* 我們設(shè)置的是 120s
* 驗(yàn)證的時(shí)候爹脾,我們先用驗(yàn)證碼和手機(jī)號來獲取 redis里的value(真實(shí)驗(yàn)證碼) 與 傳過來的 驗(yàn)證碼 進(jìn)行比對
*/
@Service
public class UmsMemberServiceImpl implements UmsMemberService {
@Autowired
private RedisService redisService;
@Override
public CommonResult generateAuthCode(String telephone) {
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random();
for( int i=0; i<6; i++) {
stringBuilder.append(random.nextInt(10));
}
//驗(yàn)證碼綁定手機(jī)號并存儲(chǔ)到redis中
redisService.set(RedisUtils.REDIS_UMS_PREFIX+telephone, stringBuilder.toString());
redisService.expire(RedisUtils.REDIS_UMS_PREFIX+telephone, RedisUtils.REDIS_UMS_EXPIRE);
return new CommonResult(ResultCode.SUCCESS, stringBuilder.toString());
}
/**
* 對輸入的驗(yàn)證碼進(jìn)行校驗(yàn)
* @param telephone
* @param authCode
* @return
*/
@Override
public CommonResult verifyAuthCode(String telephone, String authCode) {
if(StringUtils.isEmpty(authCode)) {
return new CommonResult(ResultCode.VALIDATE_FAILED, "請輸入驗(yàn)證碼");
}
String realAuthCode = redisService.get(RedisUtils.REDIS_UMS_PREFIX+telephone);
boolean result = authCode.equals(realAuthCode);
if(result) {
return new CommonResult(ResultCode.SUCCESS, realAuthCode + "+_+" + authCode);
}else {
return new CommonResult(ResultCode.VALIDATE_FAILED, realAuthCode + "+_+" + authCode);
}
}
}
Utils層
package com.liyanyan.tiny3.utils;
public class RedisUtils {
//為了保證 redis 的key不會(huì)因?yàn)?不同的業(yè)務(wù)可能會(huì)相同 所以一般會(huì)在這里加上前綴
public static final String REDIS_UMS_PREFIX = "portal:authCode";
//redis緩存過期時(shí)間 我們設(shè)置為 120s 一般驗(yàn)證碼時(shí)間防止暴力破解 時(shí)間都很短
public static final Long REDIS_UMS_EXPIRE = 120L;
}
Application
package com.liyanyan.tiny3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TinyApplication {
public static void main(String[] args) {
SpringApplication.run(TinyApplication.class, args);
}
}
最后為程序啟動(dòng)類
啟動(dòng)前 記得先啟動(dòng)你的redis服務(wù) 以及對號你的redis的配置哦 不然redis會(huì)連不上滴。
祝好解阅!