文章目录
一、安装redis
- 在github中可以找到诸多版本
https://github.com/MicrosoftArchive/redis/releases - 选择性下载,有zip 还有msi
- 安装或者解压完之后可以先不设置密码 打开redis-server 直接启动
二、配置文件
- 在pom增加redis配置
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
ps :如果不生效的话,确定不是版本对应的关系,再增加如下配置
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
- 在applicationContext.xml中增加bean
<!-- 配置JedisPoolConfig相关参数 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100"/>
<property name="maxIdle" value="20"/>
<property name="minIdle" value="3"/>
<property name="maxWaitMillis" value="2000"/>
</bean>
<!-- 配置redis服务器信息 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="127.0.0.1" p:port="6379"
p:poolConfig-ref="poolConfig" />
<bean id="genericJackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
<bean id= "stringRedisSerializer" class= "org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id= "redisTemplate" class= "org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="genericJackson2JsonRedisSerializer" />
</bean>
三、验证码的功能的实现
新建接口VerificationCodeService
package com.swyee01.service;
/**
* @author swyee
* @Program Blog
* @Description:
* @date 2020/8/13 22:29
**/
public interface VerificationCodeService {
/**
* 获取验证码
* @param mobile
* @return
*/
public String getCode(String mobile);
/**
* 判断验证码是否输入正确
* @param mobile
* @param code
* @return
*/
public boolean codeIsRight(String mobile,String code);
}
新建实现
引入redisTemplate
@Autowired
RedisTemplate<String,Object> redisTemplate;
获取验证码的方法
@Override
public String getCode(String mobile) {
//Math.random()范围为[0,1)包括0 不包括1 所以扩大十倍也小于10
StringBuilder code = new StringBuilder();
for (int i =0;i<6;i++){
int s = (int) (Math.random() * 10);
code.append(s);
}
//向redis中set值 key是mobile value是code 过期时间是300 单位是s
redisTemplate.opsForValue().set(mobile,code,300, TimeUnit.SECONDS);
return String.valueOf(code);
}
判断验证码正确与否的方法
/**
* 判断验证码是否正确
* @param mobile 页面传过来的手机号
* @param code 前端传过来的验证码
* @return
*/
@Override
public boolean codeIsRight(String mobile, String code) {
//从redis中取值
String redisCode = (String)redisTemplate.opsForValue().get(mobile);
//如果redis中没有取到值 抛出自定义异常
if(redisCode==null){
throw new CustomException("MOBILEERROR", "手机号不匹配");
}else if (code.equals(redisCode)) {
redisTemplate.delete(mobile);
//成功返回0000
return true;
} else {
//输入错误不清楚记录 仍可再次输入
return false;
}
}
Controller
package com.swyee01.controller;
import com.swyee01.model.AllResultMessage;
import com.swyee01.service.VerificationCodeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* @author swyee
* @Program Blog
* @Description:
* @date 2020/8/13 23:00
**/
@Controller //加上注解可以被springMvc管理
@ResponseBody //返回json形式
// 上面两个注解可以简写成 @RestController
@RequestMapping(value = "code") //需要在url拼接的路径
public class VeriFCodeController {
@Resource
VerificationCodeService verificationCodeService;
@RequestMapping(value = "getCode")//同样是配置路径拼接因为我们在web的配置,我们只会处理以.do结尾的请求
public AllResultMessage<String> getCode(String mobile){
String code = verificationCodeService.getCode(mobile);
return new AllResultMessage<>("SUCCESS","验证码获取成功",code);
}
@RequestMapping(value = "codeIsRight")
public AllResultMessage<String> codeIsRight(String mobile,String code){
boolean b = verificationCodeService.codeIsRight(mobile, code);
if(b){
return new AllResultMessage<>("SUCCESS","验证码输入正确","");
}else {
return new AllResultMessage<>("CODEERROR","验证码输入错误","");
}
}
}
结果展示
- 获取验证码
运行输入:http://localhost:8880/code/getCode.do?mobile=111110000
- 验证正确与否
第一次手机号输入错误尝试
http://localhost:8880/code/codeIsRight.do?mobile=11111&code=111111
第二次 手机号正确验证码输错尝试
http://localhost:8880/code/codeIsRight.do?mobile=111110000&code=111111
第三次输入正确:
http://localhost:8880/code/codeIsRight.do?mobile=111110000&code=844151
结束
上一篇:【个人博客项目 4】自定义全局返回类型模板、自定义全局异常,并以json形式返回在页面
【个人博客项目 4】自定义全局返回类型模板、自定义全局异常,并以json形式返回在页面
下一篇:【个人博客项目 6】对blog做基本的增删改查、Mybatis生成的Example的使用、PageHelper分页插件的使用 Selective的区别 本blog为例
【个人博客项目 6】对blog做基本的增删改查、Mybatis生成的Example的使用、PageHelper分页插件的使用 Selective的区别 本blog为例