redis(保存邮件激活码)

官网下载: http://redis.io/download

使用对应位数操作系统文件夹下面命令启动 redis

  redis-server.exe 服务启动程序
  redis-cli.exe 客户端命令行工具
  redis.conf 服务配置文件

通过 redis-server.exe 启动服务,默认端口 6379

通过 redis-cli.exe 启动客户端工具

使用Jedis和图形界面工具操作redis

网址: https://github.com/xetorthio/jedis

maven坐标

安转redis-desktop-manager图形化界面

添加链接

spring data redis的使用

官网: http://projects.spring.io/spring-data-redis/

引入spring data redis

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>

引入redis的配置

    <!-- 引入redis配置 -->
    <import resource="applicationContext-cache.xml"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd">
    
    <!-- jedis 连接池配置 -->
     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="300" />        
        <property name="maxWaitMillis" value="3000" />  
        <property name="testOnBorrow" value="true" />  
    </bean>  
    
    <!-- jedis 连接工厂 -->
    <bean id="redisConnectionFactory"  
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
        p:host-name="localhost" p:port="6379" p:pool-config-ref="poolConfig"  
        p:database="0" />  
        
    <!-- spring data 提供 redis模板  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
        <property name="connectionFactory" ref="redisConnectionFactory" /> 
        <!-- 如果不指定 Serializer   -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"> 
            </bean>
        </property> 
    </bean>  
    
    
</beans>

测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class RedisTemplateTest {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void testRedis() {
        // 保存key value
        // 设置30秒失效
        redisTemplate.opsForValue().set("city", "重庆", 30, TimeUnit.SECONDS);

        System.out.println(redisTemplate.opsForValue().get("city"));
    }
}

保存邮件激活码

在用户点击注册时,生成激活码并发送一封激活邮件,

// 发送一封激活邮件
        // 生成激活码
        String activecode = RandomStringUtils.randomNumeric(32);

        // 将激活码保存到redis,设置24小时失效
        redisTemplate.opsForValue().set(model.getTelephone(), activecode, 24, TimeUnit.HOURS);

        // 调用MailUtils发送激活邮件
        String content = "尊敬的客户您好,请于24小时内,进行邮箱账户的绑定,点击下面地址完成绑定:<br/><a href='" + MailUtils.activeUrl + "?telephone="
                + model.getTelephone() + "&activecode=" + activecode + "'>绑定地址</a>";
        MailUtils.sendMail("激活邮件", content, model.getEmail());

激活邮件和发送邮件的工具类

package cn.itcast.bos.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtils {
    private static String smtp_host = "smtp.163.com"; // 网易
    private static String username = "XXXXXX"; // 邮箱账户
    private static String password = "XXXXXX"; // 邮箱授权码

    private static String from = "15923157990@163.com"; // 使用当前账户
    public static String activeUrl = "http://localhost:9003/bos_fore/customer_activeMail";

    public static void sendMail(String subject, String content, String to) {
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", smtp_host);
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.auth", "true");
        Session session = Session.getInstance(props);
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from));
            message.setRecipient(RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setContent(content, "text/html;charset=utf-8");
            Transport transport = session.getTransport();
            transport.connect(smtp_host, username, password);
            transport.sendMessage(message, message.getAllRecipients());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("邮件发送失败...");
        }
    }
}
原文地址:https://www.cnblogs.com/learnjfm/p/7413740.html