实现注册功能添加邮箱激活验证

版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接 https://blog.csdn.net/weixin_43863007/article/details/88698313
我们平时注册一些平台账户的时候经常会有使用邮箱激活验证或者短信验证码验证才能注册成功,这两个功能都是调用邮箱或者运营商提供的接口来完成响应的验证功能,只不过运营商需要收费这里就演示一下通过发送邮箱激活验证的邮件来完成一个注册功能
1. 首先需要使用邮箱的接口需要导入邮箱的jar包,这里直接在pom.xml中依赖:
		<!-- 邮件工具包 -->
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>
		<!-- 编码工具包 -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.10</version>
		</dependency>
2. 还需要在spring.xml也就是applicationContext.xml中配置定邮箱格式等等:
	<context:component-scan base-package="com.hxzy.service" />
	
	<!-- 邮箱的工具类,定义邮箱格式,发送邮箱账号,授权码,编码格式,对应mail.properties中 -->
	 <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}" />
        <property name="username" value="${mail.username}" />
        <property name="password" value="${mail.password}" />
        <property name="defaultEncoding" value="${mail.defaultEncoding}" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
            </props>
        </property>
    </bean>

上面引用的值放在properties文件中,方便管理和修改:

mail.host=smtp.qq.com
[email protected]  这里是要往外发送邮件的账号
mail.password=lntgrvwxnueaiabj
mail.defaultEncoding=utf-8
3. 用户信息在controller接收到之后,在serviceimpl中实现往数据库新增操作,这里是直接调用的通用mapper的sql语句执行的新增
@Service
public class UserServiceImp extends BaseServiceImpl<User> implements UserService {
	//spring整合的用来发送邮件的工具类
	@Autowired
	JavaMailSender sender;
	@Autowired
	UserMapper usermapper;
	@Override
	public void register(User user) {
		//用来生成邮箱中的激活验证码,随机生成
		String activecode = UUID.randomUUID().toString();
		//激活状态默认为0,邮箱验证激活之后把0改成1
		user.setActivationState(0);
		super.save(user);//添加操作
		
		//新增成功后开始发送邮件
		MimeMessage msg = sender.createMimeMessage();
		MimeMessageHelper msghelp = new MimeMessageHelper(msg);
		try {
			msghelp.setFrom("[email protected]");
			msghelp.setTo(user.getEmail());
			msghelp.setSubject("激活验证");
			String content = "<div style='width:300px;height:300px;background-color:gray;margin:0px auto;'>";
			content +="<h1>欢迎您注册知乎网</h1>";
			content +="<p>请点击以下链接进行激活</p>";
			//根据这里的请求网址接收提交的参数并根据参数修改数据库中的激活状态
			content +="<p><a href='http://localhost:8081/user/active?activationCode="+activecode+"&id="+user.getId()+"'>点击激活</a></p>";
			content +="</div>";
			msghelp.setText(content, true);
			//发送邮件
			sender.send(msg);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
4.根据上面代码中点击的连接所发出的请求以及携带的参数更改数据库中的激活状态完成注册:

controller

//判断注册时是否激活成功修改状态值
	@RequestMapping("active")
	public String active(User user){
		if(us.updateByIdAndCode(user)>0){
			return "redirect:/login.jsp#activateSuccess";
		}
		return "login";
	}

最终执行的是dao层的mapper

<mapper namespace="com.hxzy.mapper.UserMapper">
	<update id="updateByIdAndCode">
		update user set activation_state = 1 where user_id = #{id} and activation_code = #{activationCode}
	</update>
</mapper>

猜你喜欢

转载自blog.csdn.net/weixin_43863007/article/details/88698313