本文主要讲解mall整合Redis的过程,以短信验证码的存储验证为例。html
Redis是用C语言开发的一个高性能键值对数据库,可用于数据缓存,主要用于处理大量数据的高访问负载。java
下载Redis,下载地址:https://github.com/MicrosoftArchive/redis/releasesgit
下载完后解压到指定目录github
在当前地址栏输入cmd后,执行redis的启动命令:redis-server.exe redis.windows.confweb
在pom.xml中新增Redis相关依赖redis
<!--redis依赖配置--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
在application.yml中添加Redis的配置及Redis中自定义key的配置。spring
在spring节点下添加Redis的配置
redis: host: localhost # Redis服务器地址 database: 0 # Redis数据库索引(默认为0) port: 6379 # Redis服务器链接端口 password: # Redis服务器链接密码(默认为空) jedis: pool: max-active: 8 # 链接池最大链接数(使用负值表示没有限制) max-wait: -1ms # 链接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 8 # 链接池中的最大空闲链接 min-idle: 0 # 链接池中的最小空闲链接 timeout: 3000ms # 链接超时时间(毫秒)
# 自定义redis keyredis: key: prefix: authCode: "portal:authCode:" expire: authCode: 120 # 验证码超期时间
package com.macro.mall.tiny.service;
数据库
/**
json
* redis操做Service,
windows
* 对象和数组都以json形式进行存储
* Created by macro on 2018/8/7.
*/
public interface RedisService {
/**
* 存储数据
*/
void set(String key, String value);
/**
* 获取数据
*/
String get(String key);
/**
* 设置超期时间
*/
boolean expire(String key, long expire);
/**
* 删除数据
*/
void remove(String key);
/**
* 自增操做
* @param delta 自增步长
*/
Long increment(String key, long delta);
}
package com.macro.mall.tiny.service.impl;
import com.macro.mall.tiny.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的实现类
* Created by macro on 2018/8/7.
*/
@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);
}
}
添加根据电话号码获取验证码的接口和校验验证码的接口
package com.macro.mall.tiny.controller;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 会员登陆注册管理Controller
* Created by macro on 2018/8/3.
*/
@Controller
@Api(tags = "UmsMemberController", description = "会员登陆注册管理")
@RequestMapping("/sso")
public class UmsMemberController {
@Autowired
private UmsMemberService memberService;
@ApiOperation("获取验证码")
@RequestMapping(value = "/getAuthCode", method = RequestMethod.GET)
@ResponseBody
public CommonResult getAuthCode(@RequestParam String telephone) {
return memberService.generateAuthCode(telephone);
}
@ApiOperation("判断验证码是否正确")
@RequestMapping(value = "/verifyAuthCode", method = RequestMethod.POST)
@ResponseBody
public CommonResult updatePassword(@RequestParam String telephone,
@RequestParam String authCode) {
return memberService.verifyAuthCode(telephone,authCode);
}
}
package com.macro.mall.tiny.service;
import com.macro.mall.tiny.common.api.CommonResult;
/**
* 会员管理Service
* Created by macro on 2018/8/3.
*/
public interface UmsMemberService {
/**
* 生成验证码
*/
CommonResult generateAuthCode(String telephone);
/**
* 判断验证码和手机号码是否匹配
*/
CommonResult verifyAuthCode(String telephone, String authCode);
}
生成验证码时,将自定义的Redis键值加上手机号生成一个Redis的key,以验证码为value存入到Redis中,并设置过时时间为本身配置的时间(这里为120s)。校验验证码时根据手机号码来获取Redis里面存储的验证码,并与传入的验证码进行比对。
package com.macro.mall.tiny.service.impl;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.service.RedisService;
import com.macro.mall.tiny.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Random;
/**
* 会员管理Service实现类
* Created by macro on 2018/8/3.
*/
@Service
public class UmsMemberServiceImpl implements UmsMemberService {
@Autowired
private RedisService redisService;
@Value("${redis.key.prefix.authCode}")
private String REDIS_KEY_PREFIX_AUTH_CODE;
@Value("${redis.key.expire.authCode}")
private Long AUTH_CODE_EXPIRE_SECONDS;
@Override
public CommonResult generateAuthCode(String telephone) {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 6; i++) {
sb.append(random.nextInt(10));
}
//验证码绑定手机号并存储到redis
redisService.set(REDIS_KEY_PREFIX_AUTH_CODE + telephone, sb.toString());
redisService.expire(REDIS_KEY_PREFIX_AUTH_CODE + telephone, AUTH_CODE_EXPIRE_SECONDS);
return CommonResult.success(sb.toString(), "获取验证码成功");
}
//对输入的验证码进行校验
@Override
public CommonResult verifyAuthCode(String telephone, String authCode) {
if (StringUtils.isEmpty(authCode)) {
return CommonResult.failed("请输入验证码");
}
String realAuthCode = redisService.get(REDIS_KEY_PREFIX_AUTH_CODE + telephone);
boolean result = authCode.equals(realAuthCode);
if (result) {
return CommonResult.success(null, "验证码校验成功");
} else {
return CommonResult.failed("验证码不正确");
}
}
}
访问Swagger的API文档地址http://localhost:8080/swagger-ui.html ,对接口进行测试。