diff --git a/spring-security-login-error-handling/.classpath b/spring-security-login-error-handling/.classpath index 1151b0d257..5cd1e45c53 100644 --- a/spring-security-login-error-handling/.classpath +++ b/spring-security-login-error-handling/.classpath @@ -17,16 +17,18 @@ - + - + + + diff --git a/spring-security-login-error-handling/.project b/spring-security-login-error-handling/.project index 818a1e1a48..47dec50119 100644 --- a/spring-security-login-error-handling/.project +++ b/spring-security-login-error-handling/.project @@ -26,7 +26,7 @@ - org.eclipse.wst.validation.validationbuilder + org.jboss.tools.jst.web.kb.kbbuilder @@ -40,6 +40,11 @@ + + org.eclipse.wst.validation.validationbuilder + + + org.eclipse.jem.workbench.JavaEMFNature @@ -50,5 +55,6 @@ org.eclipse.wst.common.project.facet.core.nature org.eclipse.wst.jsdt.core.jsNature org.hibernate.eclipse.console.hibernateNature + org.jboss.tools.jst.web.kb.kbnature diff --git a/spring-security-login-error-handling/pom.xml b/spring-security-login-error-handling/pom.xml index 4224ab29b3..a98aceed2e 100644 --- a/spring-security-login-error-handling/pom.xml +++ b/spring-security-login-error-handling/pom.xml @@ -7,13 +7,11 @@ spring-security-login-and-registration war 1.0.0-BUILD-SNAPSHOT - org.springframework.boot spring-boot-starter-parent 1.1.5.RELEASE - @@ -29,9 +27,6 @@ org.springframework spring-context-support - - - org.slf4j @@ -40,57 +35,47 @@ ch.qos.logback logback-classic - - org.slf4j jcl-over-slf4j - org.slf4j log4j-over-slf4j - javax.inject javax.inject ${javax.inject.version} - javax.servlet javax.servlet-api - javax.servlet.jsp javax.servlet.jsp-api ${javax.servlet.jsp-api.version} - javax.servlet jstl - org.springframework.security spring-security-taglibs - junit junit test - org.springframework.data @@ -104,7 +89,6 @@ org.hibernate hibernate-validator - mysql @@ -129,7 +113,6 @@ ${guava.version} - spring-security-login-and-registration @@ -144,22 +127,16 @@ 3.1.1.RELEASE 3.2.5.RELEASE 1.6.10 - 1.7.6 1.1.1 - 2.3.2-b01 - 1 - 1.4.5.RELEASE - 17.0 - diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java index 4be1696e6e..7fcfce196a 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java @@ -23,6 +23,7 @@ public class Role { @JoinColumn(name = "user_id") private User user; + @Column(name = "role") private Integer role; diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/PasswordMatches.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/PasswordMatches.java new file mode 100644 index 0000000000..eb98a73d2c --- /dev/null +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/PasswordMatches.java @@ -0,0 +1,22 @@ +package org.baeldung.persistence.service; + +import javax.validation.Constraint; +import javax.validation.Payload; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({TYPE,ANNOTATION_TYPE}) +@Retention(RUNTIME) +@Constraint(validatedBy = PasswordMatchesValidator.class) +@Documented +public @interface PasswordMatches { + + String message() default "Passwords don't match"; + Class[] groups() default {}; + Class[] payload() default {}; +} diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/PasswordMatchesValidator.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/PasswordMatchesValidator.java new file mode 100644 index 0000000000..18a70637e5 --- /dev/null +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/PasswordMatchesValidator.java @@ -0,0 +1,16 @@ +package org.baeldung.persistence.service; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class PasswordMatchesValidator implements ConstraintValidator { + + @Override + public void initialize(PasswordMatches constraintAnnotation) { + } + @Override + public boolean isValid(Object obj, ConstraintValidatorContext context){ + UserDto user = (UserDto) obj; + return user.getPassword().equals(user.getMatchingPassword()); + } +} diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/RepositoryService.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/RepositoryService.java index 0cda60da26..06730f1b65 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/RepositoryService.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/RepositoryService.java @@ -12,7 +12,6 @@ import org.springframework.stereotype.Service; public class RepositoryService implements UserService { @Autowired private UserRepository repository; - @Autowired private Environment env; diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java index 345800201d..bc2bd020c9 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java @@ -1,9 +1,24 @@ package org.baeldung.persistence.service; +import javax.validation.constraints.NotNull; +import org.hibernate.validator.constraints.NotEmpty; +@PasswordMatches public class UserDto { + @NotNull + @NotEmpty private String firstName; + @NotNull + @NotEmpty private String lastName; + @NotNull + @NotEmpty private String password; + @NotNull + @NotEmpty + private String matchingPassword; + @ValidUsername + @NotNull + @NotEmpty private String username; private Integer role; @@ -46,6 +61,12 @@ public class UserDto { public void setPassword(String password) { this.password = password; } + public String getMatchingPassword() { + return matchingPassword; + } + public void setMatchingPassword(String matchingPassword) { + this.matchingPassword = matchingPassword; + } @Override public String toString() { diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UsernameValidator.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UsernameValidator.java new file mode 100644 index 0000000000..67a7753801 --- /dev/null +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UsernameValidator.java @@ -0,0 +1,28 @@ +package org.baeldung.persistence.service; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class UsernameValidator implements ConstraintValidator { + private Pattern pattern; + private Matcher matcher; + private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; + + @Override + public void initialize(ValidUsername constraintAnnotation) { + } + + @Override + public boolean isValid(String username, ConstraintValidatorContext context) { + return (validateEmail(username)); + } + + public boolean validateEmail(String email) { + pattern = Pattern.compile(EMAIL_PATTERN); + matcher = pattern.matcher(email); + return matcher.matches(); + } +} diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/ValidUsername.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/ValidUsername.java new file mode 100644 index 0000000000..9aed811713 --- /dev/null +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/ValidUsername.java @@ -0,0 +1,24 @@ +package org.baeldung.persistence.service; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({ TYPE, FIELD, ANNOTATION_TYPE }) +@Retention(RUNTIME) +@Constraint(validatedBy = UsernameValidator.class) +@Documented +public @interface ValidUsername { + + String message() default "Invalid Email"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java index 59ca9d6765..4dc7f039fe 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java @@ -1,7 +1,9 @@ package org.baeldung.spring; import java.util.Locale; -import org.baeldung.persistence.service.UserValidator; + +import org.baeldung.persistence.service.PasswordMatchesValidator; +import org.baeldung.persistence.service.UsernameValidator; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -81,11 +83,17 @@ public class MvcConfig extends WebMvcConfigurerAdapter { messageSource.setCacheSeconds(0); return messageSource; } + + @Bean + public UsernameValidator usernameValidator() { + UsernameValidator userNameValidator = new UsernameValidator(); + return userNameValidator; + } @Bean - public UserValidator userValidator() { - UserValidator userValidator = new UserValidator(); - return userValidator; + public PasswordMatchesValidator passwordMatchesValidator() { + PasswordMatchesValidator passwordMatchesValidator = new PasswordMatchesValidator(); + return passwordMatchesValidator; } } \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java b/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java index 34637aef49..e41cb26783 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java @@ -5,16 +5,14 @@ import org.baeldung.persistence.model.User; import org.baeldung.persistence.service.EmailExistsException; import org.baeldung.persistence.service.UserDto; import org.baeldung.persistence.service.UserService; -import org.baeldung.persistence.service.UserValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.Errors; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -23,17 +21,11 @@ import org.springframework.web.servlet.ModelAndView; @Controller public class RegistrationController { + private final Logger LOGGER = LoggerFactory.getLogger(getClass()); - private UserService service; - @Autowired - private UserValidator validator; - - @InitBinder - protected void initBinder(WebDataBinder binder) { - binder.setValidator(this.validator); - } + private MessageSource messages; @Autowired public RegistrationController(UserService service) { @@ -50,13 +42,11 @@ public class RegistrationController { @RequestMapping(value = "/user/registration", method = RequestMethod.POST) public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid UserDto userAccountData, BindingResult result, WebRequest request, Errors errors) { - boolean goodEmailCheck = validator.validateEmail(userAccountData.getUsername()); - if (!goodEmailCheck) - result.rejectValue("username", "message.badEmail"); - User registered = null; + + User registered = new User(); if (!result.hasErrors()) registered = createUserAccount(userAccountData, result); - if (registered == null && !userAccountData.getUsername().isEmpty() && goodEmailCheck) { + if (registered == null) { result.rejectValue("username", "message.regError"); } if (result.hasErrors()) { diff --git a/spring-security-login-error-handling/src/main/resources/application.properties b/spring-security-login-error-handling/src/main/resources/application.properties index 1144b5f03f..425018eea6 100644 --- a/spring-security-login-error-handling/src/main/resources/application.properties +++ b/spring-security-login-error-handling/src/main/resources/application.properties @@ -8,3 +8,4 @@ init-db=false hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=false hibernate.hbm2ddl.auto=create-drop + diff --git a/spring-security-login-error-handling/src/main/resources/messages_en.properties b/spring-security-login-error-handling/src/main/resources/messages_en.properties index de6630ec92..0d4393fb5e 100644 --- a/spring-security-login-error-handling/src/main/resources/messages_en.properties +++ b/spring-security-login-error-handling/src/main/resources/messages_en.properties @@ -14,4 +14,18 @@ label.user.email=Email label.user.firstName=First name label.user.lastName=Last name label.user.password=Password -label.login=Login here \ No newline at end of file +label.login=Login here +ValidUsername.user.username=Invalid Username (Email) +UniqueUsername.user.username=An account with that username/email already exists +NotNull.user.firstName=First name required +NotEmpty.user.firstName=First name required +NotNull.user.lastName=Last name required +NotEmpty.user.lastName=Last name required +NotNull.user.username=Username(Email) required +NotEmpty.user.username=Username(Email) required +NotNull.user.password=Password required +NotEmpty.user.password=Password required +NotNull.user.matchingPassword=Required +NotEmpty.user.matchingPassword=Required +PasswordMatches.user:Password does not match! +Email.user.username=Invalid Username (Email) \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/resources/messages_es_ES.properties b/spring-security-login-error-handling/src/main/resources/messages_es_ES.properties index 3f870472a4..2084b14667 100644 --- a/spring-security-login-error-handling/src/main/resources/messages_es_ES.properties +++ b/spring-security-login-error-handling/src/main/resources/messages_es_ES.properties @@ -14,4 +14,18 @@ label.user.email=Email label.user.firstName=Nombre label.user.lastName=Apellido label.user.password=Clave -label.login=Autehtifiquese aqui \ No newline at end of file +label.login=Autehtifiquese aqui +ValidUsername.user.username=Email no es valido +UniqueUsername.user.username=Ya existe una cuenta con ese nombre de usuario +NotNull.user.firstName=Por favor ingrese su nombre +NotEmpty.user.firstName=Por favor ingrese su nombre +NotNull.user.lastName=Por favor ingrese su apellido +NotEmpty.user.lastName=Por favor ingrese su apellido +NotNull.user.username=Por favor ingrese su cuenta de email +NotEmpty.user.username=Por favor ingrese su cuenta de email +NotNull.user.password=Por favor ingrese su clave +NotEmpty.user.password=Por favor ingrese su clave +NotNull.user.matchingPassword=Campo obligatirio +NotEmpty.user.matchingPassword=Campo obligatrio +PasswordMatches.user:Las claves no coinciden! +Email.user.username=Email no es valido diff --git a/spring-security-login-error-handling/src/main/resources/webSecurityConfig.xml b/spring-security-login-error-handling/src/main/resources/webSecurityConfig.xml index 7c362885ae..70e80d9023 100644 --- a/spring-security-login-error-handling/src/main/resources/webSecurityConfig.xml +++ b/spring-security-login-error-handling/src/main/resources/webSecurityConfig.xml @@ -5,8 +5,6 @@ xsi:schemaLocation=" http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> - - @@ -16,7 +14,6 @@ - - + " rel="stylesheet"> +<<<<<<< HEAD +======= +>>>>>>> FETCH_HEAD

Hello Admin

@@ -17,6 +20,4 @@ ">Logout ">Home - - diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp index b6ddea4d52..2243df768c 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp @@ -5,20 +5,16 @@ " rel="stylesheet"> -

This is the landing page for the admin

- This text is only visible to a user
- This text is only visible to an admin
- ">Logout ">Administrator Page diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp index fbde8700a6..79461bef1d 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp @@ -6,7 +6,6 @@ " rel="stylesheet"> Home -

Welcome back home!

diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp index 7b8d96b255..9b6502c907 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp @@ -1,11 +1,15 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<<<<<<< HEAD +<%@ taglib prefix="sec" + uri="http://www.springframework.org/security/tags"%> +======= <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> +>>>>>>> FETCH_HEAD <%@ page session="true"%> " rel="stylesheet"> -

This is the homepage for the user

@@ -23,5 +27,8 @@ ">Home ">Administrator Page +<<<<<<< HEAD +======= +>>>>>>> FETCH_HEAD \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp index 6c46cd3936..59e9cdc22b 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp @@ -5,7 +5,6 @@ " rel="stylesheet"> Home -

diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp index c8b60e9a3e..ecc8965f7d 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp @@ -37,13 +37,11 @@ } -

Login

English | Spanish
- diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp index b3a154b36c..d87e41fc6c 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp @@ -11,17 +11,16 @@ - -
- -
-
Logged Out - + +
+ +
+
Login diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp index d0d72e0ded..ae90159475 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp @@ -36,6 +36,11 @@ + + + + + diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp index 8932b6dae1..323780263d 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp @@ -12,7 +12,6 @@ Registration Success -

User: