Simple application of redis

Time:2021-9-14

Spring boot integrates redis

//Import dependency
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

//File configuration
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: root

//Write your own redistemplate
package com.shuaikb.config;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //The enabledefaulttyping method in Jackson objectmapper is marked as expired from 2.10.0 due to security reasons. It is recommended to use the activatedefaulttyping method instead
        //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //String serialization method adopted by key
        template.setKeySerializer(stringRedisSerializer);
        //Hashde key also adopts string serialization
        template.setHashKeySerializer(stringRedisSerializer);
        //Value is serialized by Jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //The value serialization method of hash is Jackson
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        //Initialize configuration
        template.afterPropertiesSet();
        return template;
    }


}

Mail sending

Open the mailbox service and obtain the authorization code

Simple application of redis

//Import dependency
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

//Configuration file
spring:
  mail:
    #Configure SMTP server address
    host: smtp.qq.com
    #Sender mailbox
    username: ***********@qq.com
    #Configure the password. Note that it is not the real password, but the authorization code just applied for
    password: ******************
    #Port number 465 or 587
    port: 465
    #The default mail code is UTF-8
    default-encoding: UTF-8
    #Configure SSL encryption factory
    properties:
      mail:
        smtp:
          ssl:
            enable: true
        #Indicates that the debug mode is enabled. In this way, the log of mail sending process will be printed on the console to facilitate troubleshooting
        debug: true


//Sending process
@Autowired
JavaMailSender javaMailSender;

public void mali(){
        //Build a mail object
        SimpleMailMessage message = new SimpleMailMessage();
        //Set message subject
        Message. Setsubject ("this is a test message");
        //Set the mail sender, which should be consistent with that set in application.yml
        message.setFrom("********@qq.com");
        //Set mail recipients. There can be multiple recipients separated by commas, similar to the following
        message.setTo("********@qq.com");
        //Set the body of the message
        Message.settext ("message body");
        //Send mail
        javaMailSender.send(message);
        System.out.println ("sending succeeded");
    }

Set time expiration verification code

@Component
public class EmailSend {
    @Autowired
    JavaMailSender javaMailSender;
    String[] codees={"0","1","2","3","4","5","6","7","8","9"};

    @Autowired
    RedisUtils redisUtils;


    public void send(String toEmail,String subject){
        //Build a mail object
        SimpleMailMessage message = new SimpleMailMessage();
        //Set message subject
        message.setSubject(subject);
        //Set the mail sender, which should be consistent with that set in application.yml
        message.setFrom("********@qq.com");
        //Set mail recipients. There can be multiple recipients separated by commas, similar to the following
        message.setTo(toEmail);
        //Set the body of the message
        String code="";
        for(int i=0;i<6;i++){
            int j=(int)(Math.random()*10);
            code+=codees[j];
        }
        message.setText(code);

        //Send mail
        javaMailSender.send(message);

        redisUtils.set(toEmail,code,300);
        System.out.println ("sending succeeded");

    }
}