【Day 6】学习笔记Springboot仿牛客网论坛实现 -开发社区登录模块-注册(包括激活码发送校验)

体会从产品的角度功能怎么实现

1 发送邮件功能

邮箱设置

邮箱默认没法从程序调用 启动客户端SMTP 服务 : 通过自己邮箱里的设置开启SMTP 服务

spring email

导入jar包

在mvn repository 网站搜索包 复制maven配置

Spring Boot Starter Mail

邮箱参数配置

在appalication.property 文件

spring.mail.host=smtp.qq.com
spring.mail.port=465
spring.mail.username=
spring.mail.password=
spring.mail.protocol=smtps
spring.mail.properties.mail.smtp.ssl.enable=true

注意这里所填的密码不是邮箱密码是邮箱开启服务之后给的授权码

使用JavaMailSender发送邮件

写一个工具类封装他让他可以复用 新建包util包

注入JavaMailSender

逻辑: 通过服务器发邮件 发送人是固定的 是配置文件里的username

因此注入username

@Value
注解可以注入一些字段的普通属性,并且会自动进行类型转换

@Value("${}")
来注入配置文件里面的信息

@Value(“#{}”)

其实是SpEL表达式的值,可以表示常量的值,或者获取bean中的属性

@Value("#{1}")
	private int number; //获取数字 1
	
	@Value("#{'Spring Expression Language'}") //获取字符串常量
	private String str;
	
	@Value("#{dataSource.url}") //获取bean的属性
	private String jdbcUrl;

util包下的发送邮件工具类 代码

MimeMessage 通过Helper 来帮助发送

邮件的发送发即:(from) 自己 自己的邮箱已经通过@value 注入

邮件的接收方(to): 通过方法传入

package com.nowcoder.community.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailClient {
   
    private  static final Logger logger= LoggerFactory.getLogger(MailClient.class);
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private  String from;
    public void sendMail(String to, String subject, String content){
   
       try {
   
           MimeMessage message = mailSender.createMimeMessage();
           MimeMessageHelper helper = new MimeMessageHelper(message);
           helper.setFrom(from);
           helper.setTo(to);
           helper.setSubject(subject);
           helper.setText(content,true);
           mailSender.send(helper.getMimeMessage());
       } catch (MessagingException e){
   
           logger.error("发送邮件失败"+e.getMessage());
       }

    }
}

模板引擎

使用thymeleaf 发送一个html邮件 可以携带更为丰富的信息

做模板:

放在template 的mail目录下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.com">
<head>
    <meta charset="UTF-8">
    <title>邮件实例</title>
</head>
<body>
<p>
  欢迎你,<span style="color:red;"
 th:text="${username}"></span>!</p>

</body>
</html>

在这里1 因为用的是thymeleaf 所以要加上xmlns:th=“http://www.thymeleaf.com”>

2 欢迎你某某某 中间是变量用一个span 来书写

测试:

回忆:在普通的spring mvc 中如果想要使用模板引擎在controller中传入模板引擎的路径 dispachservlet 会帮我们自动调取

在这次我们要主动调取模板引擎

thymeleaf 模板引擎有一个核心的类 : TemplateEngine 这个类被spring 容器所管理 所以我们直接注入就可以使用。

package com.nowcoder.community;

import com.nowcoder.community.util.MailClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MailTests {
   

    @Autowired
    private MailClient mailClient;

    @Autowired
    private TemplateEngine templateEngine;

    @Test
    public void testTextMail() {
   
        mailClient.sendMail("xliu1998@139.com", "TEST", "Welcome.");
    }

    @Test
    public void testHtmlMail() {
   
        Context context = new Context();
        context.setVariable("username", "judy");

        String content = templateEngine.process("/mail/demo", context);
        System.out.println(content);

        mailClient.sendMail("xxxx@xxx.com", "HTML", content);
    }

}

二 : 开发注册功能

相对复杂的功能的拆解可以按照请求来

对于注册功能: 1 首页上注册的链接 -打开页面 (访问服务器就有一次请求)

2 在表单里填写数据 (访问服务器保存数据)

3 服务端验证账号是否存在,邮箱是否已注册

服务端发送激活邮件

激活注册账号 :点击邮件中的链接 访问服务端的激活服务

1 首页上的注册链接

分析: 点开链接所以没有什么业务 没有访问数据库 只需对视图层进行更改

对于视图层 由controller -调用模板 ,对浏览器进行响应

返回模板路径

@Controller
public class LoginController {
   
    @RequestMapping(path="/register",method = RequestMethod.GET)
    public  String getRegister(){
   
        return  "/site.register";
    }
}

页面的一些样式需要通过thymeleaf 进行管理

<!doctypehtml>
<htmllang="en"xmlns:th=:"http://www.thymeleaf.com">
<head>
   <metacharset="utf-8">
   <metaname="viewport"content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <linkrel="icon"href="https://static.nowcoder.com/images/logo_87_87.png"/>
   <linkrel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"crossorigin="anonymous">
   <linkrel="stylesheet"href="../css/global.css"/>
   <linkrel="stylesheet"href="../css/login.css"/>
   <title>牛客网-注册</title>

其中css,js 改成thymeleaf 进行管理

<linkrel="stylesheet"th:href="@{../css/global.css}"/>
<linkrel="stylesheet"th:href="@{../css/login.css}"/>
<scriptth:src=:"@{../js/global.js"}></script>
<scriptth:src="@{../js/register.js}"></script>

首页的一些链接需要改变

在index。html文件中修改

此外: 由于头部都是一样的想要复用: th: fragment="header“

<body>
   <divclass="nk-container">
<!--头部-->
<headerclass="bg-dark sticky-top"th:fragment="header">
         <divclass="container">
<!--导航-->
<navclass="navbar navbar-expand-lg navbar-dark">
<!-- logo -->
<aclass="navbar-brand"href="#"></a>
               <buttonclass="navbar-toggler"type="button"data-toggle="collapse"data-target="#navbarSupportedContent"aria-controls="navbarSupportedContent"aria-expanded="false"aria-label="Toggle navigation">
                  <spanclass="navbar-toggler-icon"></span>
               </button>

register.html

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="icon" href="https://static.nowcoder.com/images/logo_87_87.png"/>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" crossorigin="anonymous">
	<link rel="stylesheet" th:href="@{/css/global.css}" />
	<link rel="stylesheet" th:href="@{/css/login.css}" />
	<title>牛客网-注册</ti
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值