redis(保存郵件激活碼)
阿新 • • 發佈:2017-08-23
active 令行 bject base encoding public 下載 logs conf
官網下載: 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 = "[email protected]"; // 使用當前賬戶 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("郵件發送失敗..."); } } }
redis(保存郵件激活碼)