Wie login, wenn ein Benutzer klickt auf die E-Mail-Aktivierungs-link?(JSF 2.0)
Schicke ich ein Konto Aktivierungs-E-mail, um meinen Benutzern die Verwendung von javamail. In dieser E-Mail gibt es einen link, wenn darauf geklickt wird, sollte der Benutzer sein Konto aktivieren, umgeleitet auf die Hauptseite der Anwendung und Holen Sie sich angemeldet haben. Wie kann ich das tun? Kann ich fügen Sie einen Aufruf einer Methode der Aufruf einer managed bean in diesem link, den ich senden als HTML-Vorlage?
Dies ist der EJB, die ich für das senden der E-Mail-Vorlage:
@Stateless(name = "ejbs/EmailServiceEJB")
public class EmailServiceEJB implements IEmailServiceEJB {
@Resource(name = "mail/myMailSession")
private Session mailSession;
public void sendAccountActivationLinkToBuyer(String destinationEmail,
String name) {
//Destination of the email
String to = destinationEmail;
String from = "[email protected]";
try {
Message message = new MimeMessage(mailSession);
//From: is our service
message.setFrom(new InternetAddress(from));
//To: destination given
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Uspijesna registracija");
//How to found at http://www.rgagnon.com/javadetails/java-0321.html
message.setContent(generateActivationLinkTemplate(), "text/html");
Date timeStamp = new Date();
message.setSentDate(timeStamp);
//Prepare a multipart HTML
Multipart multipart = new MimeMultipart();
//Prepare the HTML
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(generateActivationLinkTemplate(), "text/html");
htmlPart.setDisposition(BodyPart.INLINE);
//PREPARE THE IMAGE
BodyPart imgPart = new MimeBodyPart();
String fileName = "logoemailtemplate.png";
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
if (classLoader == null) {
System.out.println("IT IS NULL AGAIN!!!!");
}
}
DataSource ds = new URLDataSource(classLoader.getResource(fileName));
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader("Content-ID", "<logoimg_cid>");
imgPart.setDisposition(MimeBodyPart.INLINE);
imgPart.setFileName("logomailtemplate.png");
multipart.addBodyPart(htmlPart);
multipart.addBodyPart(imgPart);
//Set the message content!
message.setContent(multipart);
System.out.println("MIME!!!!");
System.out.println(multipart.getContentType());
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
private String generateActivationLinkTemplate() {
String htmlText = "";
htmlText = "<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td><img src=\"cid:logoimg_cid\"/></td> </tr> <tr> <td height=\"220\"> <p>Thanks for Joining Site.com</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> <p>Username:<br /> Password: </p> <p>To confirm your email click <a href=\"#\">here</a>.</p></td> </tr> <tr> <td height=\"50\" align=\"center\" valign=\"middle\" bgcolor=\"#CCCCCC\">www.site.com | [email protected] | +38200 123 456</td> </tr></table>";
return htmlText;
}
}
Dies ist die EJB -, dass sparen in der Sitzung des Benutzerstatus(Log-in):
public class AuthentificationEJB implements IAuthentificationEJB {
@PersistenceContext
private EntityManager em;
//Login
public boolean saveUserState(String email, String password) {
//1-Send query to database to see if that user exist
Query query = em
.createQuery("SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");
query.setParameter("emailparam", email);
query.setParameter("passwordparam", password);
//2-If the query returns the user(Role) object, store it somewhere in
//the session
List<Object> tmpList = query.getResultList();
if (tmpList.isEmpty() == false) {
Role role = (Role) tmpList.get(0);
if (role != null && role.getEmail().equals(email)
&& role.getPassword().equals(password)) {
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("userRole", role);
//3-return true if the user state was saved
System.out.println(role.getEmail() + role.getPassword());
return true;
}
}
//4-return false otherwise
return false;
}
Kann ich die Methode aufrufen saveUserState(email,password)
von der Vorlage, die ich manuell in der Methode generateActivationLinkTemplate()
? So erhält der Benutzer weitergeleitet auf die Hauptseite der Anwendung, und der Benutzer wird gespeichert in der session -
Du musst angemeldet sein, um einen Kommentar abzugeben.
Wenn Sie den Aktivierungsschlüssel als request-parameter in einem link wie http://example.com/activate.xhtml?key=somelonganduniquekey, dann verwenden Sie einfach
@ManagedProperty
zu lassen, JSF setzen der request-parameter in der Bohne und führen die Validierung und Anmeldung job in der@PostConstruct
.mit einem
activate.xhtml
die Aussehen wie diesejava.util.UUID
.role.setActivationKey(UUID.randomUUID().toString());
und wenn ich aktiviere das Konto bei der ActivationEJB nachdem ich callsetAccountStatus(AccountStattus.ACTIVATED.toString());
was ich tun zerstören Sie den Aktivierungsschlüssel, wie diese:tmpRole.setActivationKey(null);
ich denke, es ist ok so.