MagicWolf

Spring MVC系列(二) 忘记密码功能实现

最近为认证系统添加了忘记密码功能,使用了Spring提供的邮件API和Ehcache缓存验证码。整个过程还是挺有趣,值得写一下。

Spring邮件API

Sprin提供了一个强大方便的邮件API,简化了发送邮件的工作。可以发送富文本邮件,添加附件,使用模板渲染邮件内容。推荐看Spring实战(第三版),这里只简单讲一下如何发送富文本邮件,其他的就不细讲了。

配置邮件发送器

Spring邮件API的核心是MailSender接口,Spring自带JavaMailSenderImpl实现了MailSender接口,所以需要将JavaMailSenderImpl装配到容器中。

Mail.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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"
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">
<!--读取外部属性文件-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:Mail.properties</value>
</list>
</property>
</bean>
<!--配置了mailSender-->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}"></property>
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="defaultEncoding" value="UTF-8"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
</props>
</property>
</bean>
</beans>

Mail.properties

使用QQ的邮件服务器,需要在QQ邮箱设置中开启STMP服务

1
2
3
4
5
6
7
mail.from=dai.dongliang@foxmail.com
mail.host=smtp.qq.com
mail.port=25
mail.username=675742730
mail.password=*********
mail.smtp.auth=true
mail.smtp.timeout=25000

Main.Java

这只是简单的构造了一个带有链接的邮件,其他更复杂的用法就不介绍了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "Mail.xml");
JavaMailSenderImpl sender = (JavaMailSenderImpl)context.getBean("mailSender");
//构建邮件
MimeMessage message=sender.createMimeMessage();
try {
//使用MimeMessageHelper构建Mime类型邮件
MimeMessageHelper helper= new MimeMessageHelper(message,true);
helper.setFrom("dai.dongliang@foxmail.com");
helper.setTo("xxxxxxx@foxmail.com");
message.setSubject("Spring Mail Test");
//第二个参数true表明信息类型是multipart类型
helper.setText("<a href=\"http://www.magicwolf.xyz\">你好</a>",true);
sender.send(message);
} catch (MessagingException e) {
throw new RuntimeException("邮件构造失败");
}
}
}

至此一封邮件就发送出去了,可以坐等收件人查看了。

Ehcache缓存

Spring中内置了对Ehcache的支持,封装了EhCacheCacheManager,可以很方便的使用Ehcache。

配置CacheManager

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--配置cacheManager-->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<!-- EhCache library setup -->
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" />
</beans>

ehcache.xml

Ehcache的配置文件,这里配置一个名为CodeCache的chahe用于保存验证码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<!-- 配置自定义缓存
name:Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。
maxElementsInMemory:内存中保持的对象数量。
maxElementsOnDisk:DiskStore中保持的对象数量,默认值为0,表示不限制。
eternal:是否是永恒数据,如果是,则它的超时设置会被忽略。
overflowToDisk:如果内存中数据数量超过maxElementsInMemory限制,是否要缓存到磁盘上。
timeToIdleSeconds:对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。
timeToLiveSeconds:对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。
diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
diskExpiryThreadIntervalSeconds:对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
diskSpoolBufferSizeMB:DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
memoryStoreEvictionPolicy:如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
-->
<cache name="CodeCache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LFU" />
</ehcache>

Main.java

Ehcache的基本用法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "auth-cache.xml");
//得到缓存管理器
EhCacheCacheManager cacheManager = (EhCacheCacheManager)context.getBean("cacheManager");
//得到Cache
Cache cache = cacheManager.getCache("CodeCache");
//存入缓存,这里是验证码对应用户名
cache.put("123","magicwolf");
//取出缓存
System.out.println(cache.get("123",String.class));
//删除缓存
cache.evict("123");
}
}

忘记密码功能实现

上面已经把关键点介绍了,剩余的就是如何组织代码,设计密码找回流程。代码很简单就不贴出了。

1. 显示忘记密码页面

一个简单的表单页面,输入用户名和一个60秒刷新一次的验证码。

  • 需要验证用户名是否存在,邮箱是否已填写。
  • 60秒刷新的验证码防止恶意重置密码。
  • 60秒刷新的验证码实现方式有很多,可以把时间信息存在session或cookie或Ehcache中。

2. 发送邮件,缓存重置密码令牌。

  • 生成一个5分钟内有效的令牌,将令牌和用户id映射保存在Ehcache中。
  • 用令牌值组成重置邮件链接。
  • 从数据库取出邮件地址并发送邮件。

3. 重置密码

  • 用户点击链接进入重置密码界面。
  • 验证令牌值,并得到用户Id,定位到具体用户。
  • 用户修改密码。

总结

忘记密码功能实现起来比较简单,但是如何设计一个严密的密码找回功能很麻烦。现在这个流程就很薄弱,容易受到攻击。等后面有时间了再来仔细研究一下,添加一些验证条件,比如密保问题,手机号验证这些。