青少年编程教育平台后台—登录注册(代码编写)

一、新建项目

二、编写配置文件
(一)pom.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.zgf</groupId><artifactId>platform_admin</artifactId><version>0.0.1-SNAPSHOT</version><name>platform_admin</name><description>platform_admin</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
<!--        thymeleaf--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>

(二)application.yml配置文件

spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8username: rootpassword: rootmail:protocol: smtp #邮件协议host: smtp.yeah.net #网易邮箱 smtp 服务器地址port: 25 #网易邮箱默认端口25username: zgf0907@yeah.net #发件人邮箱地址password: DTFAUFTCBKHCAQNN #网易邮箱授权码default-encoding: utf-8 #编码字符集properties:mail:debug: true #开启debug模式后会完整打印邮件发送过程的日志
mybatis:configuration:map-underscore-to-camel-case: true #开启驼峰标记

注: 使用的网易邮箱 smtp 服务器地址为smtp.yeah.net并非smtp.163.com,这个地方找了半天,太难了~~

三、导入静态资源
将原先写好的静态资源放在static目录下,html放在templates目录下,此时需要注意:在html中读取css、js、img等资源时路径填写方式:

<link th:href="@{/font/css/font-awesome.css}" rel="stylesheet"/>
<link th:href="@{/css/login.css}" rel="stylesheet"/>
<img th:src="@{/img/login_img.png}" alt="">

Thymeleaf 默认的静态文件路径是 resources 下的 static文件夹,页面路径是 templates,在 templates 中的页面文件如果要使用 static 的css或js等文件,实际上使用 th:src="@{/.../...}"或th:href="@{/.../...}"
(这一点真的太重要了,改了老半天才改好!)

四、编写程序
1.SystemContellor

@Controller
public class SystemContellor {/*** 登录页面跳转*/@GetMapping("login")public String login(){return "login";}
}

测试即可
2.新建包结构,新建数据库表admin,实体类User

@Data //省略get/set/toString
@NoArgsConstructor //无参构造器
@AllArgsConstructor//有参
public class User implements Serializable { //实现序列化操作private Integer adminTd; //主键private String adminEmail; //邮箱private String adminPwd; //密码 使用 md5 + 盐 加密private String salt; //盐private String confirmCode;  //确认码private LocalDateTime activationTime; //激活失效时间private Byte isValid; //是否可用
}

3 .分析代码逻辑,业务分析
4.UserMapper

package com.zgf.mapper;
public interface UserMapper {/*** 新增账号* @param user* @return*/@Insert("INSERT INTO admin(adminPwd,adminEmail,salt,activationTime,isValid,confirmCode)" +"VALUES(#{adminPwd},#{adminEmail},#{salt},#{activationTime},#{isValid},#{confirmCode})")int insertUser(User user);/*** 根据确认码查询用户* @param confirmCode* @return*/@Select("SELECT adminEmail,activationTime FORM admin WHERE confirmCode=#{confirmCode} AND isValid=0")User selectUserByConfirmCode(@Param("confirmCode") String confirmCode);/*** 根据确认码查询用户并修改状态值为1(可用)* @param confirmCode* @return*/@Update("UPDATE admin SET isValid=1 WHERE confirmCode = #{confirmCode}")int updateUserByConfirmCode(@Param("confirmCode") String confirmCode);/*** 根据邮箱查询用户* @param adminEmail* @return*/@Select("SELECT adminEmail,adminPwd,salt FROM admin WHERE adminEmail=#{adminEmail} AND isValid=1")List<User> selectUserByEmail(@Param("adminEmail") String adminEmail);
}

5 . UserService
使用雪花算法生成确认码

<!--    雪花算法--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.20</version></dependency>
package com.zgf.service;
@Service
public class UserService {@Resourceprivate UserMapper userMapper;@Resourceprivate MailService mailService;/*** 注册账号* @param user* @return*/@Transactional(rollbackFor = Exception.class)public Map<String,Object> createAccount(User user){//雪花算法 生成确认码String confirmCode = IdUtil.getSnowflake(1,1).nextIdStr();//生成盐 用于加密String salt = RandomUtil.randomString(6);//加密密码 原始密码+盐String md5Pwd = SecureUtil.md5(user.getAdminPwd()+salt);//激活失效时间:24小时LocalDateTime ldt = LocalDateTime.now().plusDays(1);// 初始化账号信息user.setAdminEmail("3047287962@qq.com");user.setSalt(salt);user.setAdminPwd(md5Pwd);user.setConfirmCode(confirmCode);user.setActivationTime(ldt);user.setIsValid((byte) 0);//新增账号int result = userMapper.insertUser(user);Map<String,Object> resultMap = new HashMap<>();if (result > 0){// 发送邮件String activationUrl = "http://localhost:8080/user/activation?confirmCode=" + confirmCode;mailService.sendMailForActivationAccount(activationUrl, user.getAdminEmail());resultMap.put("code",200);resultMap.put("message","注册成功,请前往邮箱激活账号!");}else{resultMap.put("code",400);resultMap.put("message","注册失败");}return resultMap;}public Map<String, Object> loginAccount(User user){Map<String ,Object> resultMap = new HashMap<>();//根据邮箱查询用户List<User> userList = userMapper.selectUserByEmail(user.getAdminEmail());//查询不到结果,返回该账户不存在或者尚未激活if(userList == null || userList.isEmpty()){resultMap.put("code",400);resultMap.put("message","该用户尚未存在或未激活");return resultMap;}//查询到多个用户,返回账号异常,请联系管理员if(userList.size() > 1){resultMap.put("code",400);resultMap.put("message","账号异常,请联系管理员");return resultMap;}//查询到一个用户,进行密码比对User u = userList.get(0);//用户输入的密码与盐进行加密String md5Pwd = SecureUtil.md5(user.getAdminPwd() + u.getSalt());//密码不一致,返回用户或密码错误if (!u.getAdminPwd().equals(md5Pwd)){resultMap.put("code",400);resultMap.put("message","用户或密码错误");return resultMap;}resultMap.put("code",200);resultMap.put("message","登录成功!");return resultMap;}/*** 激活账号* @param confirmCode* @return*/@Transactionalpublic Map<String, Object> activationAccount(String confirmCode) {Map<String, Object> resultMap = new HashMap<>();//根据确认码查询用户User user = userMapper.selectUserByConfirmCode(confirmCode);//判断激活时间是否超时boolean after = LocalDateTime.now().isAfter(user.getActivationTime());if (after){resultMap.put("code",400);resultMap.put("message","链接失效,请重新注册!");return resultMap;}//根据确认码查询用户并修改状态值为1(可用)int result = userMapper.updateUserByConfirmCode(confirmCode);if (result > 0){resultMap.put("code",200);resultMap.put("message","激活成功!");}else{resultMap.put("code",400);resultMap.put("message","激活失败!");}return resultMap;}
}

6.MailService

package com.zgf.service;
@Service
public class MailService {@Value("${spring.mail.username}")private String mailUsername;@Resourceprivate JavaMailSender javaMailSender;@Resourceprivate TemplateEngine templateEngine;/*** 激活账号邮件发送* @param activationUrl 激活url链接* @param email 收件人邮箱*/public void sendMailForActivationAccount(String activationUrl,String email){//创建邮件对象MimeMessage mimeMessage = javaMailSender.createMimeMessage();try {MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true);//设置邮件主题message.setSubject("欢迎来到青少年编程教育平台 —— 管理员账号激活");//设置邮件发送者message.setFrom(mailUsername);//设置邮件接收者 可多个message.setTo(email);//设置邮件抄送人 可多个
//            message.setCc();//设置隐秘抄送人 可多个
//            message.setBcc();//设置邮件发送日期message.setSentDate(new Date());//创建上下文环境Context context = new Context();context.setVariable("activationUrl",activationUrl);String text = templateEngine.process("activation-account.html",context);//邮件发送 设置邮件正文message.setText(text,true);} catch (MessagingException e) {e.printStackTrace();}//邮件的发送javaMailSender.send(mimeMessage);}
}

7.activation-account.html

<html>
<body>
<div>Email 地址验证<br>这封邮件是由<a href="http://www.baidu.com" target="_blank">青少年编程教育平台</a>发送的。<br>您收到这封邮件,是由于在<a href="http://www.baidu.com" target="_blank">青少年编程教育平台</a>进行了新用户注册<br>如果您没有访问过<a href="http://www.baidu.com" target="_blank">青少年编程教育平台</a>或没有进行上述操作,请忽略这封邮件----------------------------------------------------------<br>账号激活说明<br>----------------------------------------------------------<br>如果您是<a href="http://www.baidu.com" target="_blank">青少年编程教育平台</a>的新用户,或在修改您的注册Email时使用了本地址<br>您只需要点击下方链接激活账号即可:<br><a th:href="@{${activationUrl}}"><span th:text="${activationUrl}"></span></a><br/>此致<br><a href="http://www.baidu.com" target="_blank">青少年编程教育平台</a>管理团队。
</div>
</body>
</html>

8.UserController

package com.zgf.comtroller;
@RestController
@RequestMapping("user")
public class UserController {@Resourceprivate UserService userService;/*** 注册账号* @param user* @return*/@PostMapping("create")public Map<String,Object> creatAccount(User user){return userService.createAccount(user);}/*** 登录账号* @param user* @return*/@GetMapping("login")public Map<String,Object> loginAccount(User user){return userService.loginAccount(user);}/*** 激活账号* @param confirmCode* @return*/@GetMapping("activation")public Map<String, Object> activationAccont(String confirmCode){return userService.activationAccount(confirmCode);}
}

9.SystemContellor

@Controller
public class SystemContellor {/*** 登录页面跳转*/@GetMapping("login")public String login(){return "login";}/*** 登录页面跳转*/@GetMapping("register")public String register(){return "register";}
}

10.PlatformAdminApplication

package com.zgf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.zgf.mapper")
@SpringBootApplication
public class PlatformAdminApplication {public static void main(String[] args) {SpringApplication.run(PlatformAdminApplication.class, args);}
}

青少年编程教育平台后台—登录注册(代码编写)相关推荐

  1. 青少年编程教育平台后台—登录注册(界面设计)

    青少年编程教育平台后台-登录注册(界面设计) 本界面使用HTML和CSS,未使用任何框架! 一.效果截图 二.HTML <!DOCTYPE html> <html><he ...

  2. 基于SpringBoot+LayUI的青少年编程教育平台的设计与实现

    青少年编程教育平台的设计与实现 青少年编程教育平台演示视频

  3. 为全面助力青少年编程教育普及,推出花瓣少儿编程

    华为开发者大会 2021(Together)将至,华为青少年编程教育平台--花瓣少儿编程(PetalKidsCode)正式上线,并将作为HDC中Codelab挑战赛指定少儿编程产品,为参赛选手提供实战 ...

  4. OPPO入股少儿编程教育平台编程猫 官网域名为纯字母域名codemao.cn

    2月1日消息,企查查APP显示,近日,编程猫经营主体深圳点猫科技有限公司发生工商变更,上海海言投资中心(有限合伙)等股东退出,新增股东OPPO广东移动通信有限公司等. 据了解,编程猫是一个少儿编程教育 ...

  5. 长沙哪里学青少年计算机编程,长沙青少年培训编程-青少年编程教育(人工智能编程)...

    一.项目背景: 根据<新一代人工智能发展规划>,为促进全民智能教育发展,配合在中小学阶段设置人工智能相关课程并逐步推广编程教育需要,培养更多适应时代需求的青少年编程教育教师,人力资源和社会 ...

  6. 在线问诊、找科室、找医生、查疾病、图文问诊、电话急诊、健康咨询、问诊平台、咨询平台、问诊服务、语音问诊、开药问诊、看病平台、在线医疗、健康平台、登录注册、信息架构图、全局说明、组件规范、需求清单、

    在线问诊.找科室.找医生.查疾病.图文问诊.电话急诊.健康咨询.问诊平台.咨询平台.问诊服务.语音问诊.开药问诊.看病平台.在线医疗.健康平台.登录注册.信息架构图.全局说明.组件规范.需求清单. A ...

  7. jsp登录注册代码(增删改查+网页+数据库)

    目录 一·登录注册代码以及效果 doregister.jsp:注册信息弹框 login.jsp:登录 dologin.jsp:与数据库相连.存放登陆的用户 index.jsp:主界面 update.j ...

  8. java毕业设计青少年公共卫生教育平台源码+lw文档+mybatis+系统+mysql数据库+调试

    java毕业设计青少年公共卫生教育平台源码+lw文档+mybatis+系统+mysql数据库+调试 java毕业设计青少年公共卫生教育平台源码+lw文档+mybatis+系统+mysql数据库+调试 ...

  9. java计算机毕业设计青少年公共卫生教育平台源代码+数据库+系统+lw文档

    java计算机毕业设计青少年公共卫生教育平台源代码+数据库+系统+lw文档 java计算机毕业设计青少年公共卫生教育平台源代码+数据库+系统+lw文档 本源码技术栈: 项目架构:B/S架构 开发语言: ...

最新文章

  1. java中的action_浅析java中action的作用
  2. 用GDB调试程序(五)
  3. java socket安全策略文件
  4. 在Visual Studio中启用对jquery等javascript框架的智能感知
  5. cuDNN兼容性问题造成的caffe/mnist,py-faster-rcnn/demo运行结果错误
  6. mysql榨包是什么意思_模块与包 Mysql与Oracle区别
  7. CentOS 5 安装免费虚拟主机管理系统Kloxo
  8. VisualSVN安装图解
  9. break和continue的区别和执行过程
  10. git如何合并指定文件内容_Git合并指定文件到另一个分支
  11. qtcreator中常用快捷键总结
  12. linux pstack命令总结
  13. word打开提示“所用加密类型不可用”
  14. Spring Cloud 微服务下的权限解决方案
  15. 什么是侧翼区(flanking region)和侧翼区单核苷酸多态性(Flanking SNPs)
  16. adb shell 获取手机分辨率
  17. win7计算机未连接网络,教你w7电脑本地连接受限制或无连接的七种解决方法
  18. mysql tp5时间倒叙_tp5(thinkPHP5框架)时间查询操作实例分析
  19. 掌握python字符串容器_Python字符串容器,python
  20. 【办公类-16-01-01】“机动班下午代班的排班表”(python 排班表系列)

热门文章

  1. NN、DN、2NN、JN
  2. 白话空间统计之:空间异质性
  3. 图像处理:直方图规定化
  4. Python零基础之自动登录12306
  5. 两台电脑互传文件你还可以这么做
  6. 大数据主要所学技术(简介)
  7. 二分查找法及二分搜索树及其C++实现
  8. day1 704.二分查找 27.移除元素
  9. 三年级计算机绘画第二课堂教案,第二课堂活动计划15篇
  10. 88 Three.js 导入FBX格式骨骼绑定模型