项目理解(三):邮箱注册登录、拦截器与页面访问

目录

1、基本配置:

2、注册(邮箱验证)

3、登录:

4、页面访问显示登录信息

5、账号设置

6、保障用户数据安全性


1、基本配置:

pom文件导入包:

AOP\thymeleaf\web\devtools\test\mysql-connector-java\spring-boot-starter-mail\mybatis-spring-boot-starter\commons-lang3\kaptcha\fastjson

配置工具类:

数据库表:

2、注册(邮箱验证)

  1. 访问注册页面:点击顶部区域的链接,打开注册页面;
  2. 提交注册数据:
    通过表单提交数据;服务端验证账号是否已存在、邮箱是否已注册;服务端发送激活邮箱;
  3. 激活注册账号:
    点击邮件中的链接,访问服务端的激活服务:
    点击激活链接,处理情形:1激活成功 2激活码不对 3点击了多次重复激活;在util中定义一个常量类来表示激活状态;
     

知识扩展:邮件发送的一般流程:1、发信⼈在⽤户代理上编辑邮件,并写清楚收件⼈的邮箱地址;2、⽤户代理根据发信⼈编辑的信息,⽣成⼀封符合邮件格式的邮件;3、⽤户代理把邮件发送到发信⼈的邮件服务器上,邮件服务器上⾯有⼀个缓冲队列,发送到邮件服务器上⾯的邮件都会加⼊到缓冲队列中,等待邮件服务器上的 SMTP 客户端进⾏发送;4、发信⼈的邮件服务器使⽤ SMTP 协议把这封邮件发送到收件⼈的邮件服务器上;5、收件⼈的邮件服务器收到邮件后,把这封邮件放到收件⼈在这个服务器上的信箱中;6、收件⼈使⽤⽤户代理来收取邮件,⾸先⽤户代理使⽤ POP 3 协议来连接收件⼈所在的邮件服务器,身份验证成功后,⽤户代理就可以把邮件服务器上⾯的收件⼈邮箱⾥⾯的邮件读取出来,并展示给收件⼈


Spring邮箱设置:
启用客户端SMTP服务
1、Spring Email 导入 jar 包
2、邮箱参数配置
3、使用 JavaMailSender 发送邮件
4、模板引擎:使用 Thymeleaf 发送 HTML 邮件

#配置文件中:
# MailProperties
spring.mail.host=smtp.163.com
spring.mail.port=465
spring.mail.username=*********@163.com
spring.mail.password=*****
spring.mail.protocol=smtps
spring.mail.properties.mail.smtp.ssl.enable=true
@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());
        }
    }

}

Sever层:register:

       //==========注册的业务逻辑处理=============================
    public Map<String, Object> register(User user) {
        Map<String, Object> map = new HashMap<>();

        // 空值处理
        if (user == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }
        if (StringUtils.isBlank(user.getUsername())) {
            map.put("usernameMsg", "账号不能为空!");
            return map;
        }
        if (StringUtils.isBlank(user.getPassword())) {
            map.put("passwordMsg", "密码不能为空!");
            return map;
        }
        if (StringUtils.isBlank(user.getEmail())) {
            map.put("emailMsg", "邮箱不能为空!");
            return map;
        }

        // 验证账号
        User u = userMapper.selectByName(user.getUsername());
        if (u != null) {
            map.put("usernameMsg", "该账号已存在!");
            return map;
        }

        // 验证邮箱
        u = userMapper.selectByEmail(user.getEmail());
        if (u != null) {
            map.put("emailMsg", "该邮箱已被注册!");
            return map;
        }

        // 注册用户
        user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
        user.setPassword(CommunityUtil.md5(user.getPassword() + user.getSalt()));
        user.setType(0);
        user.setStatus(0);
        user.setActivationCode(CommunityUtil.generateUUID());
        user.setHeaderUrl(String.format("http://images.nowcoder.com/head/%dt.png", new Random().nextInt(1000)));
        user.setCreateTime(new Date());
        userMapper.insertUser(user);

        // 激活邮件
        Context context = new Context();
        context.setVariable("email", user.getEmail());
        // http://localhost:8080/community/activation/101/code
        String url = domain + contextPath + "/activation/" + user.getId() + "/" + user.getActivationCode();
        context.setVariable("url", url);
        String content = templateEngine.process("/mail/activation", context);
        mailClient.sendMail(user.getEmail(), "激活账号", content);

        return map;
    }

注意:激活邮件路径设置为:http://localhost:8080/community/activation/101/code 使用了激活码

Sever层:activation:

    //==========激活的业务逻辑处理==========
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值