forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
13 changed files
with
264 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...urity-login-and-registration/src/main/java/org/baeldung/event/OnRegistrationComplete.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.baeldung.event; | ||
|
||
import org.springframework.context.ApplicationEvent; | ||
|
||
@SuppressWarnings("serial") | ||
public class OnRegistrationComplete extends ApplicationEvent { | ||
|
||
public final Registration registration; | ||
|
||
public OnRegistrationComplete(Registration source) { | ||
super(source); | ||
this.registration=source; | ||
} | ||
|
||
public Registration getRegistration() { | ||
return registration; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
spring-security-login-and-registration/src/main/java/org/baeldung/event/Registration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.baeldung.event; | ||
|
||
import java.util.Locale; | ||
import org.baeldung.persistence.model.User; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.ApplicationEventPublisherAware; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class Registration implements ApplicationEventPublisherAware { | ||
|
||
private ApplicationEventPublisher eventPublisher; | ||
|
||
public String getAppUrl() { | ||
return appUrl; | ||
} | ||
|
||
public Locale getLocale() { | ||
return locale; | ||
} | ||
|
||
public void setAppUrl(String appUrl) { | ||
this.appUrl = appUrl; | ||
} | ||
|
||
public void setLocale(Locale locale) { | ||
this.locale = locale; | ||
} | ||
|
||
public void setUser(User user) { | ||
this.user = user; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
private String appUrl; | ||
private Locale locale; | ||
private User user; | ||
|
||
public Registration() { | ||
super(); | ||
} | ||
|
||
public void deliver() { | ||
this.eventPublisher.publishEvent(new OnRegistrationComplete(this)); | ||
} | ||
|
||
@Override | ||
public void setApplicationEventPublisher( | ||
ApplicationEventPublisher applicationEventPublisher) { | ||
this.eventPublisher = applicationEventPublisher; | ||
|
||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...-login-and-registration/src/main/java/org/baeldung/event/service/RegistrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.baeldung.event.service; | ||
|
||
|
||
import java.util.UUID; | ||
import org.baeldung.event.OnRegistrationComplete; | ||
import org.baeldung.persistence.model.User; | ||
import org.baeldung.persistence.service.IUserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class RegistrationService implements | ||
ApplicationListener<OnRegistrationComplete> { | ||
@Autowired | ||
private IUserService service; | ||
@Autowired | ||
private MessageSource messages; | ||
|
||
@Autowired | ||
private JavaMailSender mailSender; | ||
|
||
@Override | ||
public void onApplicationEvent(OnRegistrationComplete event) { | ||
this.confirmRegistration(event); | ||
} | ||
|
||
private void confirmRegistration(OnRegistrationComplete event) { | ||
User user = event.getRegistration().getUser(); | ||
String token = UUID.randomUUID().toString(); | ||
service.addVerificationToken(user, token); | ||
String recipientAddress = user.getEmail(); | ||
String subject = "Registration Confirmation"; | ||
String confirmationUrl = event.getRegistration().getAppUrl() | ||
+ "/regitrationConfirm.html?token=" + token; | ||
String message = messages.getMessage("message.regSucc", null, event | ||
.getRegistration().getLocale()); | ||
SimpleMailMessage email = new SimpleMailMessage(); | ||
email.setTo(recipientAddress); | ||
email.setSubject(subject); | ||
email.setText(message + " \r\n" + "http://localhost:8080" | ||
+ confirmationUrl); | ||
mailSender.send(email); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
...y-login-and-registration/src/main/java/org/baeldung/persistence/service/IUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
package org.baeldung.persistence.service; | ||
|
||
import org.baeldung.persistence.model.User; | ||
import org.baeldung.persistence.model.VerificationToken; | ||
import org.baeldung.validation.service.EmailExistsException; | ||
|
||
public interface IUserService { | ||
|
||
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException; | ||
|
||
//OCT 21 EMAIL VERIFICATION | ||
public User getRegisteredUser(String email); | ||
|
||
public User getUser(String verificationToken); | ||
|
||
public void verifyRegisteredUser(User user); | ||
|
||
public void addVerificationToken(User user, String token); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.