目录
- 一个个人邮箱网站(基于SpringBoot环境搭建)
- 效果
- 项目的概述
- 核心功能代码
- 后端
- 前端
- 项目的制作原因
- 项目总结
一个个人邮箱网站(基于SpringBoot环境搭建)
目前只能达到发送一些简单的内容,如果有啥好的建议可以发评论!!
源码地址在文章最下方
本人还在学习中
优化的地方
- 可以通过连接数据库来登录用户!
效果
登录前的主界面
登录界面
登录后的主界面
发送邮箱界面
项目的概述
1.这个项目目前只能实现固定的发送邮箱,接受者邮箱是可以改变的。
2.目前功能只有发送一些简单的标题、内容这样子后续我会慢慢加点功能上去。
3.这个项目是在网站上实现的,界面布局和效果你们都可以随意按照你们的想法去改。
4.核心的代码主要是在后台这一块,有SpringBoot+SpringSecurity、Springboot+Mybatis、Springboot+thymeleaf、异步任务、Druid。
5.可以实现发送邮件的简单日志,还有一个带附件发送的半成品的一些代码。
核心功能代码
后端
数据库的连接
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/meweb?serverTimezone=UTC&userUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource#下面这一块是一些Durid的设置不设置也是可以的#Spring Boot 默认是不注入这些属性值的,需要自己绑定#druid 数据源专有配置initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4jfilters: stat,wall,log4jmaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis配置
# 配置mybatis
# 扫描包的位置
mybatis.type-aliases-package=com.me.pojo
# 实现mapper接口配置mapper和接口的绑定
mybatis.mapper-locations=classpath:mapper/*.xml
邮箱的一些配置
# 邮箱
spring.mail.username=2336164407@qq.com
# 邮箱密码的明文
spring.mail.password=xxxxxxxxxx
# 我使用的是QQ邮箱
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties..mail.smtp.ssl.enable=true # QQ邮箱独有的加密验证
发送邮箱的业务
// 封装成了一个方法
@Service
public class SendMailService {@AutowiredJavaMailSenderImpl mailSender;@Asyncpublic void sendMail(String subject, String text, String to, String from) {SimpleMailMessage simpleMailMessage = new SimpleMailMessage();simpleMailMessage.setSubject(subject);simpleMailMessage.setText(text);simpleMailMessage.setTo(to);simpleMailMessage.setFrom(from);mailSender.send(simpleMailMessage);}}
用户数据查询代码
mapper层
@Repository
@Mapper
public interface UserInfoMapper {/*** 查找对应用户信息* @param username* @return*/UserInfo getUserInfo(String username);
}
maooer对应xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.me.mapper.UserInfoMapper"><select id="getUserInfo" resultType="UserInfo" parameterType="String">select * from meweb.user where username=#{username};</select></mapper>
Service接口
public interface UserInfoService {/*** 查找对应用户信息* @param username* @return*/UserInfo getUserInfo(String username);
}
Service接口实现类
@Service
public class UserInfoServiceImpl implements UserInfoService {@Autowiredprivate UserInfoMapper userInfoMapper;@Overridepublic UserInfo getUserInfo(String username) {return userInfoMapper.getUserInfo(username);}
}
SpringSecurity这块的功能代码
创建一个类去实现UserDetailsService接口
@Component
public class CustomUserDetailsService implements UserDetailsService {@Autowiredprivate UserInfoService userInfoService;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {UserInfo userInfo = userInfoService.getUserInfo(username);if(userInfo==null){throw new UsernameNotFoundException("用户不存在");}String role = userInfo.getRole();List<GrantedAuthority> authorities = new ArrayList<>();//角色必须以"ROLE_"做为开头,数据库中不需要!authorities.add(new SimpleGrantedAuthority("ROLE_"+role));return new User(userInfo.getUsername(),//因为数据库是明文,所以我们这里需要加密处理new BCryptPasswordEncoder().encode(userInfo.getPassword()),authorities);}
}
@EnableWebSecurity // 用来开启SpringSecurity的注解
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate CustomUserDetailsService customUserDetailsService;@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/","index").permitAll() // permitAll() 这个代表谁都可以访问.antMatchers("/mail/**").hasAnyRole("mail");// hasAnyRole() 这个代表要有指定的角色才可以访问的,比如有mail这个角色// 这一块是用于自定义的登录页面http.formLogin().usernameParameter("username") // 里面的username必须和前端表单传递的name要一致.passwordParameter("password") // 里面的password必须和前端表单传递的name要一致.loginPage("/toLogin") // 设置登录页面的地址.loginProcessingUrl("/login"); // 登录页面表单提交的地址http.csrf().disable(); // 禁用csrfhttp.logout().logoutSuccessUrl("/"); // 设置当注销用户时跳转的页面http.rememberMe().rememberMeParameter("remeber"); // 设置当前用户是否记住我默认保留14天}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {// 我使用的是通过在代码中硬性设置账号和密码来实现的,如果要通过数据库的来实现账号和密码验证的话可以在网上找找资料!// passwordEncoder()密码加密方式,我这里用到的是 new BCryptPasswordEncoder()加密//方法一通过连接数据库来登录用户auth.userDetailsService(customUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());//方法二把用户名和密码写入内存中/*auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("me").password(new BCryptPasswordEncoder().encode("123456")).roles("mail").and().withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("mail");*/}}
前端
主页面代码
<!DOCTYPE html>
<html lang="zh_cn" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head><meta charset="UTF-8"><link rel="stylesheet" th:href="@{/css/index.css}"><!--这个是用来设置浏览器访问的小图标的--><link rel="icon" th:href="@{/image/1.png}" sizes="16x16"> <script th:src="@{/js/index.js}"></script><title>ME中心</title>
</head>
<body><div id="index"> <div ><h1>欢迎来到ME的零度空间<span id="time" ></span></h1><div><div sec:authorize="!isAuthenticated()"><a th:href="@{/toLogin}" class="zhuxiao" >登录</a></div><div sec:authorize="isAuthenticated()"><a th:href="@{/logout}" class="zhuxiao" >注销</a></div></div><a th:href="@{/mail/mailView}" class="but" style="text-align: center;">发送邮件</a><a th:href="@{/mail/mailView}" class="but" style="text-align: center;">发送带附件的邮件</a><a href="" class="but" style="text-align: center;pointer-events: none;">待开发</a><a href="" class="but" style="text-align: center;pointer-events: none;">待开发</a><a href="" class="but" style="text-align: center;pointer-events: none;">待开发</a><a href="" class="but" style="text-align: center;pointer-events: none;">待开发</a><a href="" class="but" style="text-align: center;pointer-events: none;">...</a></div></div>
</body>
</html>
登录页面代码
<!DOCTYPE html>
<html lang="zh_cn" xmlns:th="http://www.thymeleaf.org">
<head> <meta charset="UTF-8"> <title>登录</title> <link rel="stylesheet" th:href="@{/css/login.css}"><link rel="icon" th:href="@{/css/login.css}" sizes="16x16">
</head>
<body> <div id="login"> <h1>登录</h1> <form th:action="@{/login}" method="post"><input type="text" required="required" placeholder="用户名" name="username"></input><input type="password" required="required" placeholder="密码" name="password"></input><span class="remember">记住我</span><input type="checkbox" name="remember" style="width: 50px"></input><button class="but" type="submit">登录</button></form> </div>
</body>
</html>
发送邮件页面代码
<!DOCTYPE html>
<html lang="zh_cn" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><link rel="stylesheet" th:href="@{/css/sendMail.css}"><link rel="icon" th:href="@{/image/1.png}" sizes="16x16"><title>发送邮件</title>
</head>
<body><div id="mail"> <h1>发送邮件</h1> <div ><span class="msg" th:text="${msg}"></span><a th:href="@{/index}" class="fanhuei">返回主页</a></div><form th:action="@{/mail/snedMail}" method="post"><div><span class="text">标题:</span><input type="text" required="required" placeholder="标题" name="subject"></input></div> <div><span class="text">内容:</span><input type="text" required="required" placeholder="内容" name="text"></input></div> <div><span class="text">接受者邮箱:</span><input type="email" required="required" placeholder="接受者邮箱" name="to"></input></div> <div><span class="text">发送者邮箱:</span><input type="email" required="required" placeholder="发送者邮箱" name="from"></input></div> <button class="but" type="submit">发送</button> </form> </div>
</body>
</html>
项目的制作原因
1.本人还处于后端的学习阶段虽然学了差差不多有一年多的时间了。
2.目前都是在学习SpringBoot、SpringCloud,微服务这一块的。
3.其实我一开始就是好玩的去做,也是因为在学到了在SpringBoot基础上实现邮件的发送产生了好奇。
3.就去想着结合我现在学的一些内容去整合的做一个网站但是在做的过程中去巩固关于mysql数据库的连接,使用SpringSecurity以及thymeleaf的使用
4.页面在一开始的时候也就是一个from表单这样子外加几个h1标签,目前这个页面是我在网上找的一些模板然后我去改了一下
项目总结
上面用到的核心功能在我的博客的笔记中也有,那些笔记是看狂神大佬的教学视频做的笔记
SpringSecurity配置:https://blog.csdn.net/yuran06/article/details/121561188?spm=1001.2014.3001.5501
Mybatis配置:https://blog.csdn.net/yuran06/article/details/121566373?spm=1001.2014.3001.5501
Thymeleaf配置:https://blog.csdn.net/yuran06/article/details/121566135?spm=1001.2014.3001.5501
关于邮箱的配置:https://blog.csdn.net/yuran06/article/details/121560041?spm=1001.2014.3001.5501
最后我希望我这样子因为好玩而去做了一个简单的个人邮件发送的小项目能对大家提供一些帮助,尤其是和我一样还在学习的小伙伴!!
源码地址:https://gitee.com/mehao123/sendMail/tree/master