Skip to content

Commit c783c3b

Browse files
committed
邮箱验证
1 parent 6d06714 commit c783c3b

8 files changed

Lines changed: 185 additions & 27 deletions

File tree

admin/src/main/java/info/xiaomo/admin/controller/AdminUserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public HashMap<String, Object> getAll(@PathVariable("start") int start, @PathVar
109109
return result;
110110
}
111111

112-
@RequestMapping("deleteById/{id}")
112+
@RequestMapping(value = "deleteById/{id}",method = RequestMethod.GET)
113113
public HashMap<String, Object> deleteUserById(@PathVariable("id") Long id) throws UserNotFoundException {
114114
HashMap<String, Object> result = new HashMap<>();
115115
AdminModel adminModel = service.deleteAdminUserById(id);

core/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@
7777
<groupId>org.springframework</groupId>
7878
<artifactId>spring-context-support</artifactId>
7979
</dependency>
80+
<dependency>
81+
<groupId>javax.mail</groupId>
82+
<artifactId>mail</artifactId>
83+
</dependency>
84+
<dependency>
85+
<groupId>javax.activation</groupId>
86+
<artifactId>activation</artifactId>
87+
</dependency>
8088
</dependencies>
8189
<build>
8290
<plugins>

core/src/main/java/info/xiaomo/core/model/UserModel.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@
2323
@Table(name = "user")
2424
public class UserModel extends BaseModel {
2525

26-
private String userName;
26+
/**
27+
* 登录用户
28+
*/
29+
private String Email;
2730

2831
private String nickName;
2932

3033
private String password;
3134

32-
private String email;
35+
/**
36+
* 1己激活 0 未激活
37+
*/
38+
private int validateStatus=0;//激活状态
39+
40+
private String validateCode;//激活码
3341

3442
private int gender;
3543

@@ -39,14 +47,6 @@ public class UserModel extends BaseModel {
3947

4048
private String address;
4149

42-
public String getUserName() {
43-
return userName;
44-
}
45-
46-
public void setUserName(String userName) {
47-
this.userName = userName;
48-
}
49-
5050
public String getNickName() {
5151
return nickName;
5252
}
@@ -64,11 +64,11 @@ public void setPassword(String password) {
6464
}
6565

6666
public String getEmail() {
67-
return email;
67+
return Email;
6868
}
6969

7070
public void setEmail(String email) {
71-
this.email = email;
71+
Email = email;
7272
}
7373

7474
public int getGender() {
@@ -102,4 +102,20 @@ public String getAddress() {
102102
public void setAddress(String address) {
103103
this.address = address;
104104
}
105+
106+
public int getValidateStatus() {
107+
return validateStatus;
108+
}
109+
110+
public void setValidateStatus(int validateStatus) {
111+
this.validateStatus = validateStatus;
112+
}
113+
114+
public String getValidateCode() {
115+
return validateCode;
116+
}
117+
118+
public void setValidateCode(String validateCode) {
119+
this.validateCode = validateCode;
120+
}
105121
}

core/src/main/java/info/xiaomo/core/service/UserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface UserService {
2424

2525
UserModel findUserById(Long id);
2626

27-
UserModel findUserByUserName(String userName);
27+
UserModel findUserByEmail(String userName);
2828

2929
UserModel addUser(UserModel model);
3030

core/src/main/java/info/xiaomo/core/service/impl/UserServiceImpl.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import info.xiaomo.core.exception.UserNotFoundException;
55
import info.xiaomo.core.model.UserModel;
66
import info.xiaomo.core.service.UserService;
7+
import info.xiaomo.core.untils.MailUtil;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.data.domain.Page;
910
import org.springframework.data.domain.Pageable;
@@ -38,12 +39,23 @@ public UserModel findUserById(Long id) {
3839
}
3940

4041
@Override
41-
public UserModel findUserByUserName(String userName) {
42-
return dao.findUserByUserName(userName);
42+
public UserModel findUserByEmail(String email) {
43+
return dao.findUserByUserName(email);
4344
}
4445

4546
@Override
4647
public UserModel addUser(UserModel model) {
48+
StringBuilder sb = new StringBuilder("点击下面链接激活账号,48小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!</br>");
49+
sb.append("<a href=\"http://localhost:8080/springmvc/user/register?action=activate&email=");
50+
sb.append(model.getEmail());
51+
sb.append("&validateCode=");
52+
sb.append(model.getValidateCode());
53+
sb.append("\">http://localhost:8080/springmvc/user/register?action=activate&email=");
54+
sb.append(model.getEmail());
55+
sb.append("&validateCode=");
56+
sb.append(model.getValidateCode());
57+
sb.append("</a>");
58+
MailUtil.send(model.getEmail(), sb.toString());
4759
model.setCreateTime(new Date());
4860
model.setUpdateTime(new Date());
4961
return dao.save(model);
@@ -76,9 +88,6 @@ public UserModel updateUser(UserModel model) throws UserNotFoundException {
7688
if (!Objects.equals(model.getPhone(), userUpdate.getPhone())) {
7789
userUpdate.setPhone(model.getPhone());
7890
}
79-
if (!Objects.equals(model.getUserName(), userUpdate.getUserName())) {
80-
userUpdate.setUserName(model.getUserName());
81-
}
8291
userUpdate.setUpdateTime(new Date());
8392
dao.save(userUpdate);
8493
return userUpdate;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package info.xiaomo.core.untils;
2+
3+
import javax.mail.*;
4+
import javax.mail.internet.InternetAddress;
5+
import javax.mail.internet.MimeMessage;
6+
import java.util.Date;
7+
import java.util.Properties;
8+
9+
/**
10+
* 把今天最好的表现当作明天最新的起点..~
11+
* いま 最高の表現 として 明日最新の始発..~
12+
* Today the best performance as tomorrow newest starter!
13+
* Created by IntelliJ IDEA.
14+
*
15+
* @author: xiaomo
16+
* @github: https://github.com/qq83387856
17+
* @email: hupengbest@163.com
18+
* @QQ_NO: 83387856
19+
* @Date: 2016/4/511:00
20+
* @Description:
21+
* @Copyright(©) 2015 by xiaomo.
22+
**/
23+
public class MailUtil {
24+
private static final String HOST = "smtp.163.com";
25+
private static final String PROTOCOL = "smtp";
26+
private static final int PORT = 25;
27+
private static final String FROM = "hupbentbest@163.com";//发件人的email
28+
private static final String PWD = "xiaomoxiaomo123";//发件人密码
29+
30+
/**
31+
* 获取Session
32+
*/
33+
private static Session getSession() {
34+
Properties props = new Properties();
35+
props.put("mail.smtp.host", HOST);//设置服务器地址
36+
props.put("mail.store.protocol", PROTOCOL);//设置协议
37+
props.put("mail.smtp.port", PORT);//设置端口
38+
props.put("mail.smtp.auth", true);
39+
40+
Authenticator authenticator = new Authenticator() {
41+
@Override
42+
protected PasswordAuthentication getPasswordAuthentication() {
43+
return new PasswordAuthentication(FROM, PWD);
44+
}
45+
46+
};
47+
return Session.getDefaultInstance(props, authenticator);
48+
}
49+
50+
public static void send(String toEmail, String content) {
51+
Session session = getSession();
52+
try {
53+
System.out.println("--send--" + content);
54+
// Instantiate a message
55+
Message msg = new MimeMessage(session);
56+
57+
//Set message attributes
58+
msg.setFrom(new InternetAddress(FROM));
59+
InternetAddress[] address = {new InternetAddress(toEmail)};
60+
msg.setRecipients(Message.RecipientType.TO, address);
61+
msg.setSubject("账号激活邮件");
62+
msg.setSentDate(new Date());
63+
msg.setContent(content, "text/html;charset=utf-8");
64+
65+
//Send the message
66+
Transport.send(msg);
67+
} catch (MessagingException mex) {
68+
mex.printStackTrace();
69+
}
70+
}
71+
}

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
<spring-context-support.version>4.2.1.RELEASE</spring-context-support.version>
4343
<spring-boot.version>1.3.3.RELEASE</spring-boot.version>
4444
<commons-beanutils.version>20030211.134440</commons-beanutils.version>
45+
<javax-mail.version>1.4.7</javax-mail.version>
46+
<javax.activation.version>1.1.1</javax.activation.version>
4547
</properties>
4648

4749
<parent>
@@ -111,6 +113,16 @@
111113
<artifactId>spring-context-support</artifactId>
112114
<version>${spring-context-support.version}</version>
113115
</dependency>
116+
<dependency>
117+
<groupId>javax.mail</groupId>
118+
<artifactId>mail</artifactId>
119+
<version>${javax-mail.version}</version>
120+
</dependency>
121+
<dependency>
122+
<groupId>javax.activation</groupId>
123+
<artifactId>activation</artifactId>
124+
<version>${javax.activation.version}</version>
125+
</dependency>
114126
</dependencies>
115127
</dependencyManagement>
116128

web/src/main/java/info/xiaomo/web/controller/UserController.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import info.xiaomo.core.model.UserModel;
66
import info.xiaomo.core.service.UserService;
77
import info.xiaomo.core.untils.MD5;
8+
import org.hibernate.service.spi.ServiceException;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.web.bind.annotation.*;
1011

12+
import java.text.ParseException;
13+
import java.util.Date;
1114
import java.util.HashMap;
1215

1316
/**
@@ -32,9 +35,9 @@ public class UserController extends BaseController {
3235
private UserService service;
3336

3437
@RequestMapping(value = "login", method = RequestMethod.POST)
35-
public HashMap<String, Object> login(@RequestParam String userName, @RequestParam String password) {
38+
public HashMap<String, Object> login(@RequestParam String email, @RequestParam String password) {
3639
HashMap<String, Object> result = new HashMap<>();
37-
UserModel userModel = service.findUserByUserName(userName);
40+
UserModel userModel = service.findUserByEmail(email);
3841
if (userModel == null) {
3942
result.put(code, notFound);
4043
} else {
@@ -51,7 +54,6 @@ public HashMap<String, Object> login(@RequestParam String userName, @RequestPara
5154

5255
@RequestMapping(value = "register", method = RequestMethod.POST)
5356
public HashMap<String, Object> register(
54-
@RequestParam String userName,
5557
@RequestParam String nickName,
5658
@RequestParam String password,
5759
@RequestParam String email,
@@ -60,15 +62,16 @@ public HashMap<String, Object> register(
6062
@RequestParam String address
6163
) {
6264
HashMap<String, Object> result = new HashMap<>();
63-
UserModel userModel = service.findUserByUserName(userName);
65+
UserModel userModel = service.findUserByEmail(email);
6466
if (userModel != null) {
6567
result.put(code, error);
6668
} else {
6769
userModel = new UserModel();
68-
userModel.setUserName(userName);
6970
userModel.setNickName(nickName);
7071
userModel.setEmail(email);
7172
userModel.setGender(gender);
73+
userModel.setValidateStatus(0);//默认未验证
74+
userModel.setValidateCode(MD5.encode(email));
7275
userModel.setPhone(phone);
7376
userModel.setAddress(address);
7477
userModel.setPassword(MD5.encode(password));
@@ -91,7 +94,6 @@ public UserModel findUserById(@PathVariable("id") Long id) {
9194

9295
@RequestMapping(value = "update", method = RequestMethod.POST)
9396
public HashMap<String, Object> update(
94-
@RequestParam String userName,
9597
@RequestParam String nickName,
9698
@RequestParam String password,
9799
@RequestParam String email,
@@ -100,12 +102,11 @@ public HashMap<String, Object> update(
100102
@RequestParam String address
101103
) throws UserNotFoundException {
102104
HashMap<String, Object> result = new HashMap<>();
103-
UserModel userModel = service.findUserByUserName(userName);
105+
UserModel userModel = service.findUserByEmail(email);
104106
if (userModel != null) {
105107
result.put(code, error);
106108
} else {
107109
userModel = new UserModel();
108-
userModel.setUserName(userName);
109110
userModel.setNickName(nickName);
110111
userModel.setEmail(email);
111112
userModel.setGender(gender);
@@ -123,4 +124,45 @@ public HashMap<String, Object> update(
123124
return result;
124125
}
125126

127+
/**
128+
* 处理激活
129+
*
130+
* @throws ParseException
131+
*/
132+
@RequestMapping(value = "validateEmail", method = RequestMethod.GET)
133+
public void validateEmail(@RequestParam String email, @RequestParam String validateCode) throws ServiceException, ParseException, UserNotFoundException {
134+
//数据访问层,通过email获取用户信息
135+
UserModel user = service.findUserByEmail(email);
136+
//验证用户是否存在
137+
if (user != null) {
138+
//验证用户激活状态
139+
if (user.getValidateStatus() == 0) {
140+
///没激活
141+
Date currentTime = new Date();//获取当前时间
142+
//验证链接是否过期
143+
currentTime.before(user.getCreateTime());
144+
if (currentTime.before(user.getUpdateTime())) {
145+
//验证激活码是否正确
146+
if (validateCode.equals(user.getValidateCode())) {
147+
//激活成功, //并更新用户的激活状态,为已激活
148+
System.out.println("==sq===" + user.getValidateStatus());
149+
user.setValidateStatus(1);//把状态改为激活
150+
System.out.println("==sh===" + user.getValidateStatus());
151+
service.updateUser(user);
152+
} else {
153+
throw new ServiceException("激活码不正确");
154+
}
155+
} else {
156+
throw new ServiceException("激活码已过期!");
157+
}
158+
} else {
159+
throw new ServiceException("邮箱已激活,请登录!");
160+
}
161+
} else {
162+
throw new ServiceException("该邮箱未注册(邮箱地址不存在)!");
163+
}
164+
165+
}
166+
167+
126168
}

0 commit comments

Comments
 (0)