У меня есть контроллер пружины, который отправляет электронное письмо пользователю после того, как он создает новую учетную запись, которая содержит ссылку, которая выглядит примерно так: «Чтобы подтвердить свою учетную запись, пожалуйста, нажмите здесь: http://localhost:8082/confirm-account?token=THE_TOKEN", но у меня есть беспокоиться о создании веб-страницы с использованием углового, который появляется, когда пользователь нажимает на ссылку. Вы можете мне помочь?
Вот контроллер весной:
package com.example.demo.controller;
import com.example.demo.domain.ConfirmationToken;
import com.example.demo.domain.Response;
import com.example.demo.domain.User;
import com.example.demo.repository.ConfirmationTokenRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.EmailSenderService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.*;
import java.util.Calendar;
@RestController
@CrossOrigin
public class UserAccountController {
@Autowired
private UserRepository userRepository;
@Autowired
private UserService userService;
@Autowired
private ConfirmationTokenRepository confirmationTokenRepository;
@Autowired
private EmailSenderService emailSenderService;
@RequestMapping(value="/register", method= RequestMethod.POST)
@ResponseBody
public ResponseEntity<Response> registerUser( @RequestBody User user)
{
userService.saveUser(user);
ConfirmationToken confirmationToken = new ConfirmationToken(user);
confirmationTokenRepository.save(confirmationToken);
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(user.getEmail());
mailMessage.setSubject("Complete Registration!");
mailMessage.setFrom("ddailyservice@gmail.com");
mailMessage.setText("To confirm your account, please click here : "
+"http://localhost:8082/confirm-account?token="+confirmationToken.getConfirmationToken());
emailSenderService.sendEmail(mailMessage);
return new ResponseEntity<Response>(new Response("User is saved successfully"), HttpStatus.OK) ;
}
@RequestMapping(value="/confirm-account", method= { RequestMethod.GET})
@ResponseBody
public ResponseEntity<Response> confirmUserAccount(@RequestParam("token") String confirmationToken)
{
ConfirmationToken token = confirmationTokenRepository.findByConfirmationToken(confirmationToken);
User user = token.getUser();
Calendar calendar = Calendar.getInstance();
if((token.getExpiryDate().getTime() - calendar.getTime().getTime()) <=0) {
return new ResponseEntity<Response>(new Response("token is expired"), HttpStatus.OK);
}
if(token != null)
{
user=userRepository.findByUsernameIgnoreCase(token.getUser().getUsername());
user.setEnabled(true);
userRepository.save(user);
}
else
{
return new ResponseEntity<Response>(new Response("token is broken"), HttpStatus.OK);
}
return new ResponseEntity<Response>(new Response("account verified"), HttpStatus.OK);
}
public UserRepository getUserRepository() {
return userRepository;
}
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
public ConfirmationTokenRepository getConfirmationTokenRepository() {
return confirmationTokenRepository;
}
public void setConfirmationTokenRepository(ConfirmationTokenRepository confirmationTokenRepository) {
this.confirmationTokenRepository = confirmationTokenRepository;
}
public EmailSenderService getEmailSenderService() {
return emailSenderService;
}
public void setEmailSenderService(EmailSenderService emailSenderService) {
this.emailSenderService = emailSenderService;
}
}
И подтверждение взятое лицо:
package com.example.demo.domain;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
@Entity
public class ConfirmationToken {
private final static int EXPIRATION = 60*24;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="token_id")
private long tokenid;
@Column(name="confirmation_token")
private String confirmationToken;
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate;
@OneToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = {CascadeType.REMOVE}, orphanRemoval = true)
@JoinColumn(nullable = false, name = "user_id")
private User user;
private Date expiryDate;
public Date getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate;
}
private Date calculateExpiryDate(int expiryTimeInMinutes) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Timestamp(calendar.getTime().getTime()));
calendar.add(Calendar.MINUTE, expiryTimeInMinutes);
return new Date(calendar.getTime().getTime());
}
public ConfirmationToken() {
}
public ConfirmationToken(User user) {
this.user = user;
confirmationToken = UUID.randomUUID().toString();
Calendar calendar = Calendar.getInstance();
createdDate = new Date(calendar.getTime().getTime());
this.expiryDate = calculateExpiryDate(EXPIRATION);
}
public ConfirmationToken(final String confirmationToken) {
this.confirmationToken = confirmationToken;
this.expiryDate = calculateExpiryDate(EXPIRATION);
}
public String getConfirmationToken() {
return confirmationToken;
}
public void setConfirmationToken(String confirmationToken) {
this.confirmationToken = confirmationToken;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public long getTokenid() {
return tokenid;
}
public void setTokenid(long tokenid) {
this.tokenid = tokenid;
}
}