Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e983cf

Browse files
Sending verification mail
1 parent 023832c commit 0e983cf

File tree

9 files changed

+61
-10
lines changed

9 files changed

+61
-10
lines changed

‎src/main/java/com/naturalprogrammer/spring5tutorial/domain/User.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public static enum Role {
4545
@ElementCollection(fetch = FetchType.EAGER)
4646
@Enumerated(EnumType.STRING)
4747
private Collection<Role> roles = new HashSet<Role>();
48+
49+
@Column(length=36)
50+
private String verificationCode;
4851

4952
public Long getId() {
5053
return id;
@@ -87,6 +90,14 @@ public void setRoles(Collection<Role> roles) {
8790
this.roles = roles;
8891
}
8992

93+
public String getVerificationCode() {
94+
return verificationCode;
95+
}
96+
97+
public void setVerificationCode(String verificationCode) {
98+
this.verificationCode = verificationCode;
99+
}
100+
90101
@Override
91102
public Collection<? extends GrantedAuthority> getAuthorities() {
92103

‎src/main/java/com/naturalprogrammer/spring5tutorial/services/UserServiceImpl.java‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.naturalprogrammer.spring5tutorial.services;
22

3+
import java.util.UUID;
4+
35
import javax.annotation.PostConstruct;
6+
import javax.mail.MessagingException;
47

58
import org.apache.commons.logging.Log;
69
import org.apache.commons.logging.LogFactory;
@@ -15,6 +18,7 @@
1518
import com.naturalprogrammer.spring5tutorial.commands.UserCommand;
1619
import com.naturalprogrammer.spring5tutorial.domain.User;
1720
import com.naturalprogrammer.spring5tutorial.domain.User.Role;
21+
import com.naturalprogrammer.spring5tutorial.mail.MailSender;
1822
import com.naturalprogrammer.spring5tutorial.repositories.UserRepository;
1923
import com.naturalprogrammer.spring5tutorial.utils.MyUtils;
2024

@@ -35,12 +39,18 @@ public class UserServiceImpl implements UserService {
3539

3640
private PasswordEncoder passwordEncoder;
3741
private UserRepository userRepository;
42+
private MailSender mailSender;
43+
private String applicationUrl;
3844

3945
public UserServiceImpl(UserRepository userRepository,
40-
PasswordEncoder passwordEncoder) {
46+
PasswordEncoder passwordEncoder,
47+
MailSender mailSender,
48+
@Value("${applicationUrl}") String applicationUrl) {
4149

4250
this.userRepository = userRepository;
4351
this.passwordEncoder = passwordEncoder;
52+
this.mailSender = mailSender;
53+
this.applicationUrl = applicationUrl;
4454
}
4555

4656
@PostConstruct
@@ -74,8 +84,30 @@ public void signup(UserCommand userCommand) {
7484
User user = userCommand.toUser();
7585
user.setPassword(passwordEncoder.encode(user.getPassword()));
7686
user.getRoles().add(Role.UNVERIFIED);
87+
user.setVerificationCode(UUID.randomUUID().toString());
7788

7889
userRepository.save(user);
79-
MyUtils.afterCommit(() -> MyUtils.login(user));
90+
MyUtils.afterCommit(() -> {
91+
92+
MyUtils.login(user);
93+
try {
94+
95+
sendVerificationMail(user);
96+
97+
} catch (MessagingException e) {
98+
99+
log.warn("Sending verification mail to "
100+
+ user.getEmail() + " failed", e);
101+
}
102+
});
103+
}
104+
105+
private void sendVerificationMail(User user) throws MessagingException {
106+
107+
String verificationLink = applicationUrl + "/users/" +
108+
user.getVerificationCode() + "/verify";
109+
110+
mailSender.send(user.getEmail(), MyUtils.getMessage("verifySubject"),
111+
MyUtils.getMessage("verifyBody", verificationLink));
80112
}
81113
}

‎src/main/resources/application.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ security.user.name: user@example.com
2525
security.user.password: password
2626

2727
rememberMeKey: topSecret
28-
28+
applicationUrl: http://localhost:8080
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
hello: Hi
22
text: Visit {0} if you are below 18, else visit {1}
33

4-
signupSuccess: Signup succeeded
4+
signupSuccess: Signup succeeded. Please check your mailbox to get verified.
5+
6+
verifySubject: Please verify your email
7+
verifyBody: Hi,<br/><br/>Your email at XYZ is unverified. Please click on\
8+
the following link to get verified:<br/><br/>{0}
9+
10+
pleaseVerify: Hey, please verify yourself!

‎src/main/webapp/WEB-INF/jsp/hello-id.jsp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
22
<%@include file="includes/header.jsp"%>
3-
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
43

54
<spring:message code="hello" />, ${name}, your id is ${id}!
65

‎src/main/webapp/WEB-INF/jsp/hello.jsp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
22
<%@include file="includes/header.jsp"%>
3-
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
43

54
<spring:message code="hello" />, ${name}!
65

‎src/main/webapp/WEB-INF/jsp/includes/header.jsp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2+
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
23
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
34
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
45
<!DOCTYPE html>
@@ -103,6 +104,13 @@
103104
</div><!-- /.container-fluid -->
104105
</nav>
105106

107+
<sec:authorize access="hasRole('UNVERIFIED')">
108+
<div class="alert alert-warning alert-dismissible" role="alert">
109+
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
110+
<spring:message code="pleaseVerify" />
111+
</div>
112+
</sec:authorize>
113+
106114
<c:if test="${not empty flashMessage}">
107115
<div class="alert alert-${flashKind} alert-dismissible" role="alert">
108116
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>

‎src/main/webapp/WEB-INF/jsp/login.jsp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
22
<%@include file="includes/header.jsp"%>
3-
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
4-
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
53

64
<div class="panel panel-primary">
75
<div class="panel-heading">

‎src/main/webapp/WEB-INF/jsp/signup.jsp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
22
<%@include file="includes/header.jsp"%>
3-
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
4-
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
53

64
<div class="panel panel-primary">
75
<div class="panel-heading">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /