增加发送邮件服务

整理包名、类名
master
panqihua 5 years ago
parent 0240d36984
commit 38102abcfe
  1. 1
      build.gradle
  2. 64
      src/main/java/com/community/pocket/api/ManagerController.java
  3. 26
      src/main/java/com/community/pocket/entity/po/EmailCode.java
  4. 2
      src/main/java/com/community/pocket/entity/po/Forum.java
  5. 23
      src/main/java/com/community/pocket/entity/po/Manager.java
  6. 23
      src/main/java/com/community/pocket/entity/po/Token.java
  7. 21
      src/main/java/com/community/pocket/entity/vo/Common.java
  8. 24
      src/main/java/com/community/pocket/entity/vo/EmailType.java
  9. 10
      src/main/java/com/community/pocket/entity/vo/JSONResponse.java
  10. 2
      src/main/java/com/community/pocket/entity/vo/ManagerLogin.java
  11. 2
      src/main/java/com/community/pocket/entity/vo/ManagerRegister.java
  12. 2
      src/main/java/com/community/pocket/entity/vo/Result.java
  13. 2
      src/main/java/com/community/pocket/repository/ForumRepo.java
  14. 4
      src/main/java/com/community/pocket/repository/ManagerDao.java
  15. 2
      src/main/java/com/community/pocket/repository/ManagerRepo.java
  16. 39
      src/main/java/com/community/pocket/util/EmailService.java
  17. 15
      src/main/resources/application.yaml

@ -21,6 +21,7 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-data-rest' implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-devtools' testImplementation 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') { testImplementation('org.springframework.boot:spring-boot-starter-test') {

@ -1,12 +1,10 @@
package com.community.pocket.api; package com.community.pocket.api;
import com.community.pocket.Result; import com.community.pocket.entity.po.Manager;
import com.community.pocket.domain.Manager; import com.community.pocket.entity.po.Token;
import com.community.pocket.domain.Res; import com.community.pocket.entity.vo.*;
import com.community.pocket.domain.Token;
import com.community.pocket.domain.form.ManagerLogin;
import com.community.pocket.domain.form.ManagerRegister;
import com.community.pocket.repository.ManagerDao; import com.community.pocket.repository.ManagerDao;
import com.community.pocket.util.EmailService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -14,6 +12,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List;
@RestController @RestController
@RequestMapping("/api/manager") @RequestMapping("/api/manager")
@ -25,16 +24,22 @@ public class ManagerController {
@Autowired @Autowired
private ManagerDao managerDao; private ManagerDao managerDao;
@Autowired
private Common common;
@Autowired
private EmailService emailService;
// 登陆 // 登陆
@PostMapping("login") @PostMapping("login")
public Res<Token> login(@RequestBody ManagerLogin managerLogin){ public JSONResponse<Token> login(@RequestBody ManagerLogin managerLogin){
if(StringUtils.isEmpty(managerLogin.getManagerName())){ if(StringUtils.isEmpty(managerLogin.getManagerName())){
return new Res<>(Result.FAIL,"管理员不能为空!",null); return new JSONResponse<>(Result.FAIL,"管理员不能为空!",null);
}else if(StringUtils.isEmpty(managerLogin.getPassword())){ }else if(StringUtils.isEmpty(managerLogin.getPassword())){
return new Res<>(Result.FAIL,"密码不能为空!",null); return new JSONResponse<>(Result.FAIL,"密码不能为空!",null);
//校验账号密码 //校验账号密码
}else if(!managerDao.login(managerLogin)){ }else if(!managerDao.login(managerLogin)){
return new Res<>(Result.FAIL,"账号或密码错误!",null); return new JSONResponse<>(Result.FAIL,"账号或密码错误!",null);
} }
else{ else{
// 验证通过生成令牌响应给服务端 // 验证通过生成令牌响应给服务端
@ -47,27 +52,46 @@ public class ManagerController {
token.setUseTime(c.getTimeInMillis()); token.setUseTime(c.getTimeInMillis());
// 生成token // 生成token
token.setToken(DigestUtils.md5DigestAsHex((token.getCreateTime()+""+token.getUseTime()+"").getBytes())); token.setToken(DigestUtils.md5DigestAsHex((token.getCreateTime()+""+token.getUseTime()+"").getBytes()));
return new Res<>(Result.OK,"登陆成功",token); token.setManagerName(managerLogin.getManagerName());
return new JSONResponse<>(Result.OK,"登陆成功",token);
}
}
/**
* 获取邮箱类型
* @return
*/
@GetMapping("emailType")
public JSONResponse<List<EmailType>> getEmailType(){
return new JSONResponse<>(Result.OK,"获取邮箱类型成功", common.getSupportType());
}
@PostMapping("sendcode")
public JSONResponse<String> sendEmail(String sender){
if(emailService.sendCode(sender)){
return new JSONResponse<>(Result.OK,"发送邮件成功",null);
}else{
return new JSONResponse<>(Result.OK,"发送邮件失败",null);
} }
} }
// 注册 // 注册
@PostMapping("register") @PostMapping("register")
public Res<String> register(@RequestBody ManagerRegister manager){ public JSONResponse<String> register(@RequestBody ManagerRegister manager){
if(StringUtils.isEmpty(manager.getManagerName())){ if(StringUtils.isEmpty(manager.getManagerName())){
return new Res<>(Result.FAIL,"管理员不能为空",null); return new JSONResponse<>(Result.FAIL,"管理员不能为空",null);
}else if(StringUtils.isEmpty(manager.getPassword())){ }else if(StringUtils.isEmpty(manager.getPassword())){
return new Res<>(Result.FAIL,"密码不能为空",null); return new JSONResponse<>(Result.FAIL,"密码不能为空",null);
}else if(StringUtils.isEmpty(manager.getConfirmPassword())){ }else if(StringUtils.isEmpty(manager.getConfirmPassword())){
return new Res<>(Result.FAIL,"确认密码不能为空",null); return new JSONResponse<>(Result.FAIL,"确认密码不能为空",null);
}else if(!manager.getPassword().equals(manager.getConfirmPassword())){ }else if(!manager.getPassword().equals(manager.getConfirmPassword())){
return new Res<>(Result.FAIL,"两次密码输入不一致",null); return new JSONResponse<>(Result.FAIL,"两次密码输入不一致",null);
}else if(StringUtils.isEmpty(manager.getMobie())){ }else if(StringUtils.isEmpty(manager.getMobie())){
return new Res<>(Result.FAIL,"手机号不能为空",null); return new JSONResponse<>(Result.FAIL,"手机号不能为空",null);
}else if(StringUtils.isEmpty(manager.getEmail())){ }else if(StringUtils.isEmpty(manager.getEmail())){
return new Res<>(Result.FAIL,"邮箱不能为空",null); return new JSONResponse<>(Result.FAIL,"邮箱不能为空",null);
}else if(managerDao.hasUser(manager.getManagerName())){ }else if(managerDao.hasUser(manager.getManagerName())){
return new Res<>(Result.FAIL,"用户已存在",null); return new JSONResponse<>(Result.FAIL,"用户已存在",null);
}else{ }else{
// 表单数据转换到管理员实体,调用dao层持久化到数据库 // 表单数据转换到管理员实体,调用dao层持久化到数据库
Manager m=new Manager(); Manager m=new Manager();
@ -76,7 +100,7 @@ public class ManagerController {
m.setMobie(manager.getMobie()); m.setMobie(manager.getMobie());
m.setEmail(manager.getEmail()); m.setEmail(manager.getEmail());
managerDao.save(m); managerDao.save(m);
return new Res<>(Result.OK,"注册成功",null); return new JSONResponse<>(Result.OK,"注册成功",null);
} }
} }

@ -0,0 +1,26 @@
package com.community.pocket.entity.po;
/**
* 邮箱验证码
*/
public class EmailCode {
//发送邮箱
private String emailAddress;
//验证码
private String code;
//发送时间
private Long sendTime=System.currentTimeMillis();
//有效时间
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}

@ -1,4 +1,4 @@
package com.community.pocket.domain; package com.community.pocket.entity.po;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;

@ -1,12 +1,11 @@
package com.community.pocket.domain; package com.community.pocket.entity.po;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
//管理员 //管理员
public class Manager { public class Manager {
@Id
private String id;
// 管理员 // 管理员
@Id
private String managerName; private String managerName;
// 密码 // 密码
private String password; private String password;
@ -15,15 +14,7 @@ public class Manager {
// 邮箱 // 邮箱
private String email; private String email;
private String a; private Token token;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getManagerName() { public String getManagerName() {
return managerName; return managerName;
@ -56,4 +47,12 @@ public class Manager {
public void setEmail(String email) { public void setEmail(String email) {
this.email = email; this.email = email;
} }
public Token getToken() {
return token;
}
public void setToken(Token token) {
this.token = token;
}
} }

@ -1,25 +1,18 @@
package com.community.pocket.domain; package com.community.pocket.entity.po;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
//登陆成功记录的令牌信息 //登陆成功记录的令牌信息
public class Token { public class Token {
@Id
private String id;
//令牌 //令牌
@Id
private String token; private String token;
//创建时间 //创建时间
private Long createTime; private Long createTime;
// 有效时间 // 有效时间
private Long useTime; private Long useTime;
// 管理员名
public String getId() { private String managerName;
return id;
}
public void setId(String id) {
this.id = id;
}
public String getToken() { public String getToken() {
return token; return token;
@ -44,4 +37,12 @@ public class Token {
public void setUseTime(Long useTime) { public void setUseTime(Long useTime) {
this.useTime = useTime; this.useTime = useTime;
} }
public String getManagerName() {
return managerName;
}
public void setManagerName(String managerName) {
this.managerName = managerName;
}
} }

@ -0,0 +1,21 @@
package com.community.pocket.entity.vo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "email")
public class Common {
private List<EmailType> supportType;
public List<EmailType> getSupportType() {
return supportType;
}
public void setSupportType(List<EmailType> supportType) {
this.supportType = supportType;
}
}

@ -0,0 +1,24 @@
package com.community.pocket.entity.vo;
public class EmailType {
//邮箱标志
private String name;
//邮箱后缀
private String suffix;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}

@ -1,9 +1,7 @@
package com.community.pocket.domain; package com.community.pocket.entity.vo;
import com.community.pocket.Result;
//接口统一返回json响应体 //接口统一返回json响应体
public class Res<T> { public class JSONResponse<T> {
// 响应结果 // 响应结果
private Result result; private Result result;
// 响应详细结果 // 响应详细结果
@ -11,10 +9,10 @@ public class Res<T> {
// 自定义其他响应信息 // 自定义其他响应信息
private T body; private T body;
public Res() { public JSONResponse() {
} }
public Res(Result result, String message, T body) { public JSONResponse(Result result, String message, T body) {
this.result = result; this.result = result;
this.message = message; this.message = message;
this.body = body; this.body = body;

@ -1,4 +1,4 @@
package com.community.pocket.domain.form; package com.community.pocket.entity.vo;
public class ManagerLogin { public class ManagerLogin {
private String managerName; private String managerName;

@ -1,4 +1,4 @@
package com.community.pocket.domain.form; package com.community.pocket.entity.vo;
public class ManagerRegister { public class ManagerRegister {
// 管理员 // 管理员

@ -1,4 +1,4 @@
package com.community.pocket; package com.community.pocket.entity.vo;
public enum Result { public enum Result {
OK, OK,

@ -1,6 +1,6 @@
package com.community.pocket.repository; package com.community.pocket.repository;
import com.community.pocket.domain.Forum; import com.community.pocket.entity.po.Forum;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@ -1,8 +1,8 @@
package com.community.pocket.repository; package com.community.pocket.repository;
import com.community.pocket.domain.Manager; import com.community.pocket.entity.po.Manager;
import com.community.pocket.domain.form.ManagerLogin; import com.community.pocket.entity.vo.ManagerLogin;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;

@ -1,7 +1,7 @@
package com.community.pocket.repository; package com.community.pocket.repository;
import com.community.pocket.domain.Manager; import com.community.pocket.entity.po.Manager;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@ -0,0 +1,39 @@
package com.community.pocket.util;
import com.community.pocket.entity.po.EmailCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class EmailService {
@Autowired
private JavaMailSenderImpl javaMailSender;
@Autowired
private MongoTemplate mongoTemplate;
public boolean sendCode(String sender) {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("口袋社区激活邮箱");
message.setText("欢迎注册口袋社区后台服务,您获取到的验证码是:xxxx");
message.setTo(sender);
message.setFrom("15920722180@163.com");
try {
javaMailSender.send(message);
EmailCode emailCode=new EmailCode();
emailCode.setCode(new Random().nextInt(1000)+"");
emailCode.setEmailAddress(sender);
mongoTemplate.save(emailCode);
return true;
} catch (MailException e) {
e.printStackTrace();
return false;
}
}
}

@ -3,3 +3,18 @@ spring:
data: data:
mongodb: mongodb:
database: demo database: demo
mail:
username:
password:
host: smtp.163.com
properties:
mail:
smtp:
ssl:
enable: true
#邮箱配置
email:
supportType:
- name: 网易
suffix: '@163.com'
Loading…
Cancel
Save