我的服务等级:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/register").permitAll()
.antMatchers("/confirm").permitAll();
}
}
我的pojo:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "email", nullable = false, unique = true)
@Email(message = "Please provide a valid e-mail")
@NotEmpty(message = "Please provide an e-mail")
private String email;
@Column(name = "password")
@Transient
private String password;
@Column(name = "first_name")
@NotEmpty(message = "Please provide your first name")
private String firstName;
@Column(name = "last_name")
@NotEmpty(message = "Please provide your last name")
private String lastName;
@Column(name = "enabled")
private boolean enabled;
@Column(name = "confirmation_token")
private String confirmationToken;
public String getConfirmationToken() {
return confirmationToken;
}
public void setConfirmationToken(String confirmationToken) {
this.confirmationToken = confirmationToken;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean getEnabled() {
return enabled;
}
public void setEnabled(boolean value) {
this.enabled = value;
}
我的控制器:
@Controller
public class RegisterController {
private BCryptPasswordEncoder bCryptPasswordEncoder;
private UserService userService;
private EmailService emailService;
@Autowired
public RegisterController(BCryptPasswordEncoder bCryptPasswordEncoder,
UserService userService, EmailService emailService) {
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
this.userService = userService;
this.emailService = emailService;
}
// Return registration form template
@RequestMapping(value="/register", method = RequestMethod.GET)
public ModelAndView showRegistrationPage(ModelAndView modelAndView, User user){
modelAndView.addObject("user", user);
modelAndView.setViewName("register");
return modelAndView;
}
// Process form input data
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView processRegistrationForm(ModelAndView modelAndView, @Valid User user, BindingResult bindingResult, HttpServletRequest request) {
// Lookup user in database by e-mail
User userExists = userService.findByEmail(user.getEmail());
System.out.println(userExists);
if (userExists != null) {
modelAndView.addObject("alreadyRegisteredMessage", "Oops! There is already a user registered with the email provided.");
modelAndView.setViewName("register");
bindingResult.reject("email");
}
if (bindingResult.hasErrors()) {
modelAndView.setViewName("register");
} else { // new user so we create user and send confirmation e-mail
// Disable user until they click on confirmation link in email
user.setEnabled(false);
// Generate random 36-character string token for confirmation link
user.setConfirmationToken(UUID.randomUUID().toString());
userService.saveUser(user);
String appUrl = request.getScheme() + "://" + request.getServerName();
SimpleMailMessage registrationEmail = new SimpleMailMessage();
registrationEmail.setTo(user.getEmail());
registrationEmail.setSubject("Registration Confirmation");
registrationEmail.setText("To confirm your e-mail address, please click the link below:\n"
+ appUrl + "/confirm?token=" + user.getConfirmationToken());
registrationEmail.setFrom("noreply@domain.com");
emailService.sendEmail(registrationEmail);
modelAndView.addObject("confirmationMessage", "A confirmation e-mail has been sent to " + user.getEmail());
modelAndView.setViewName("register");
}
return modelAndView;
}
// Process confirmation link
@RequestMapping(value="/confirm", method = RequestMethod.GET)
public ModelAndView confirmRegistration(ModelAndView modelAndView, @RequestParam("token") String token) {
User user = userService.findByConfirmationToken(token);
if (user == null) { // No token found in DB
modelAndView.addObject("invalidToken", "Oops! This is an invalid confirmation link.");
} else { // Token found
modelAndView.addObject("confirmationToken", user.getConfirmationToken());
}
modelAndView.setViewName("confirm");
return modelAndView;
}
// Process confirmation link
@RequestMapping(value="/confirm", method = RequestMethod.POST)
public ModelAndView confirmRegistration(ModelAndView modelAndView, BindingResult bindingResult, @RequestParam Map<String, String> requestParams, RedirectAttributes redir) {
modelAndView.setViewName("confirm");
Zxcvbn passwordCheck = new Zxcvbn();
Strength strength = passwordCheck.measure(requestParams.get("password"));
if (strength.getScore() < 3) {
//modelAndView.addObject("errorMessage", "Your password is too weak. Choose a stronger one.");
bindingResult.reject("password");
redir.addFlashAttribute("errorMessage", "Your password is too weak. Choose a stronger one.");
modelAndView.setViewName("redirect:confirm?token=" + requestParams.get("token"));
System.out.println(requestParams.get("token"));
return modelAndView;
}
// Find the user associated with the reset token
User user = userService.findByConfirmationToken(requestParams.get("token"));
// Set new password
user.setPassword(bCryptPasswordEncoder.encode(requestParams.get("password")));
// Set user to enabled
user.setEnabled(true);
// Save user
userService.saveUser(user);
modelAndView.addObject("successMessage", "Your password has been set!");
return modelAndView;
}
以下是我得到的错误,即使我将MySql降级到5.7:
org.springframework.web.util.NestedServletException:请求处理失败;嵌套的异常是org.springframework.dao.InvalidDataAccessResourceUsageException:无法获取JDBC连接。 SQL [n / a];嵌套的异常是org.hibernate.exception.SQLGrammarException:无法获取JDBC连接
我的application.properties:
# ===============================
# TOMCAT
# ===============================
server.address=127.0.0.1
server.error.whitelabel.enabled=false
server.tomcat.accesslog.enabled=true
# ===============================
# SMTP EMAIL
# ===============================
spring.mail.host = smtp.mailhost.com
spring.mail.username = mailUser
spring.mail.password = mailPass
spring.mail.port = 587
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable = true
# ===============================
# = LOGGING
# ===============================
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
# ===============================
# = DATA SOURCE
# ===============================
spring.datasource.url=jdbc:mysql://localhost:3308/point?useLegacyDatetimeCode=false&serverTimezone=Africa/pretoria
spring.datasource.username=root
spring.datasource.password=langton
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=5
spring.datasource.tomcat.test-on-borrow=true
# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
# ===============================
# = Thymeleaf configurations
# ===============================
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false