Mymes project full set of learning tutorials serialized, pay attention to the public number for the first time
Now I’m going to integrate Mymes based on what I said earlier and if you haven’t seen it Redis can review the previous article
-
Springboot create mymes
-
Mymes integrates Mybatis Generator
-
Swagger mymes integration – the UI
Mymes integrates Redis for caching
This article mainly explains mymes through Redis, to SMS authentication storage authentication
Redis installation and startup
Redis (Remote Dictionary Server) is an open source, network-enabled, memory-based and persistent logging, key-value database written in ANSI C language, and provides multiple language APIS. As of March 15, 2010, the development of Redis is hosted by VMware. Development of Redis has been sponsored by Pivotal since May 2013.
-
2. Download Redis from https://redis.io/
-
2. (https://github.com/MicrosoftArchive/redis/releases)
-
- Unzip to the specified directory using 2 Download
-
- Enter CMD in the current address bar and run the redis startup command
Integrate Redis within the Mymes framework
Add Reids dependencies in the Mymes project
Add Redis dependencies to the pom.xml of Mymes
<! -- Redis dependency configuration -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Copy the code
Add the Reids configuration parameters for Mymes
Add the Redis configuration in application.yml
Add the Redis configuration under the Spring node of application.yml
redis:
host: localhost # Redis server address
database: 0 # Redis database index (default 0)
port: 6379 # Redis server connection port
password: # Redis server connection password (default null)
jedis:
pool:
max-active: 8 # maximum number of connections in the pool (use negative values to indicate no limit)
max-wait: -1ms Maximum connection pool blocking wait time (negative value indicates no limit)
max-idle: 8 The maximum number of free connections in the connection pool
min-idle: 0 Minimum free connection in connection pool
timeout: 3000ms Connection timeout (ms)
Copy the code
Add Redis configuration under application.yml
redis:
key:
prefix:
authCode: "portal:authCode:"
expire:
authCode: 120 # Verification code expiration time
Copy the code
Application. Yml parameter configuration up to Redis
server:
port: 9999
spring:
application:
name: mes-demo
datasource:
Url: JDBC: mysql: / / 192.168.199.128:3306 / mymes? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: reader
password: 123456
redis:
Host: localhost (IP address)# Redis server address
database: 0 # Redis database index (default 0)
port: 6379 # Redis server connection port
password: # Redis server connection password (default null)
jedis:
pool:
max-active: 8 # maximum number of connections in the pool (use negative values to indicate no limit)
max-wait: -1ms Maximum connection pool blocking wait time (negative value indicates no limit)
max-idle: 8 The maximum number of free connections in the connection pool
min-idle: 0 Minimum free connection in connection pool
timeout: 3000ms Connection timeout (ms)
mybatis:
mapper-locations:
- classpath:mapper/*.xml
- classpath*:com/**/mapper/*.xml
# Custom redis key
redis:
key:
prefix:
authCode: "portal:authCode:"
expire:
authCode: 120 # Verification code expiration time
Copy the code
Add Redis interface to implement common Redis operations
package com.cn.mymes.service;
/ * *
* redis operates on Service,
* Objects and arrays are stored as JSON
*
* /
public interface RedisService {
/ * *
* key - value
* /
void set(String key, String value);
/ * *
* get kye - value
* /
String get(String key);
/ * *
* Set expiration time
* /
boolean expire(String key, long expire);
/ * *
* delete key - value
* /
void remove(String key);
/ * *
* Automatic growth operation
*
* /
Long increment(String key, long delta);
}
Copy the code
Implement Redis interface
package com.cn.mymes.service.impl;
import com.cn.mymes.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;
/ * *
* Redis interface implementation class
*
* /
@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);
}
}
Copy the code
Add the MyMesMemberService interface
package com.cn.mymes.service;
import com.cn.mymes.common.CommonResult;
/ * *
* Log in to verify the management Service
*
* /
public interface MyMesMemberService {
/ * *
* Generate a verification code
* /
CommonResult generateAuthCode(String telephone);
/ * *
* Determine the verification code
* /
CommonResult verifyAuthCode(String telephone, String authCode);
}
Copy the code
Add MyMesMemberService implementation class MyMesMemberServiceImpl for the MyMesMemberService interface
package com.cn.mymes.service.impl;
import com.cn.mymes.common.CommonResult;
import com.cn.mymes.service.MyMesMemberService;
import com.cn.mymes.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import java.util.Random;
/ * *
* Login authentication management implementation class
*
* /
public class MyMesMemberServiceImpl implements MyMesMemberService {
@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));
}
// The verification code is bound to the mobile phone number and stored in 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(), "Verification code obtained successfully");
}
// Verify the entered verification code
@Override
public CommonResult verifyAuthCode(String telephone, String authCode) {
if (StringUtils.isEmpty(authCode)) {
return CommonResult.failed("Please enter the verification code.");
}
String realAuthCode = redisService.get(REDIS_KEY_PREFIX_AUTH_CODE + telephone);
boolean result = authCode.equals(realAuthCode);
if (result) {
return CommonResult.success(null, "Verification code verified successfully");
} else {
return CommonResult.failed("Verification code is not correct");
}
}
}
Copy the code
Add MyMesMemberController
package com.cn.mymes.controller;
import com.cn.mymes.common.CommonResult;
import com.cn.mymes.service.MyMesMemberService;
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;
/ * *
* Members log in to register the admin Controller
*
* /
@Controller
@Api(tags = "MyMesMemberController", description = "Login registration Management")
@RequestMapping("/sso")
public class MyMesMemberController {
@Autowired
private MyMesMemberService memberService;
@ApiOperation("Obtain captcha code")
@RequestMapping(value = "/getAuthCode", method = RequestMethod.GET)
@ResponseBody
public CommonResult getAuthCode(@RequestParam String telephone) {
return memberService.generateAuthCode(telephone);
}
@ApiOperation("Determine whether the verification code is correct.")
@RequestMapping(value = "/verifyAuthCode", method = RequestMethod.POST)
@ResponseBody
public CommonResult updatePassword(@RequestParam String telephone,
@RequestParam String authCode) {
return memberService.verifyAuthCode(telephone,authCode);
}
}
Copy the code
Run the project
Access to Swagger interface address http://localhost:9999/swagger-ui.html, testing interface.