Commit 0399d3855f8a6934d2f966fcaf66e2bf476ecba1
1 parent
64343f46
Adding user form
- add UserForm and UserFormValidator - switch to spring boot 1.2.4 - some name fixes
Showing
8 changed files
with
75 additions
and
30 deletions
Show diff stats
pom.xml
@@ -9,12 +9,11 @@ | @@ -9,12 +9,11 @@ | ||
9 | <packaging>war</packaging> | 9 | <packaging>war</packaging> |
10 | 10 | ||
11 | <name>spring-mvc-sample</name> | 11 | <name>spring-mvc-sample</name> |
12 | - <description></description> | ||
13 | 12 | ||
14 | <parent> | 13 | <parent> |
15 | <groupId>org.springframework.boot</groupId> | 14 | <groupId>org.springframework.boot</groupId> |
16 | <artifactId>spring-boot-starter-parent</artifactId> | 15 | <artifactId>spring-boot-starter-parent</artifactId> |
17 | - <version>1.2.3.RELEASE</version> | 16 | + <version>1.2.4.RELEASE</version> |
18 | <relativePath/> <!-- lookup parent from repository --> | 17 | <relativePath/> <!-- lookup parent from repository --> |
19 | </parent> | 18 | </parent> |
20 | 19 |
src/main/java/fr/plil/sio/web/mvc/ApplicationMvcConfiguration.java
@@ -12,7 +12,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | @@ -12,7 +12,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
12 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | 12 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
13 | 13 | ||
14 | @Configuration | 14 | @Configuration |
15 | -public class ApplicationMvcConfiguration extends WebMvcConfigurerAdapter{ | 15 | +public class ApplicationMvcConfiguration extends WebMvcConfigurerAdapter { |
16 | 16 | ||
17 | private static final Logger logger = LoggerFactory.getLogger(ApplicationMvcConfiguration.class); | 17 | private static final Logger logger = LoggerFactory.getLogger(ApplicationMvcConfiguration.class); |
18 | 18 |
src/main/java/fr/plil/sio/web/mvc/LoginController.java
@@ -26,19 +26,19 @@ public class LoginController { | @@ -26,19 +26,19 @@ public class LoginController { | ||
26 | @RequestMapping(method = RequestMethod.POST) | 26 | @RequestMapping(method = RequestMethod.POST) |
27 | public String postLoginCheck(User user, BindingResult result) { | 27 | public String postLoginCheck(User user, BindingResult result) { |
28 | 28 | ||
29 | - User userFromDao = userRepository.findByUsername(user.getUsername()); | 29 | + User userFromRepository = userRepository.findByUsername(user.getUsername()); |
30 | 30 | ||
31 | - if (userFromDao == null) { | 31 | + if (userFromRepository == null) { |
32 | result.rejectValue("username","login.form.invalid"); | 32 | result.rejectValue("username","login.form.invalid"); |
33 | return "login"; | 33 | return "login"; |
34 | } | 34 | } |
35 | 35 | ||
36 | - if(!userFromDao.getPassword().equals(user.getPassword())) { | 36 | + if (!userFromRepository.getPassword().equals(user.getPassword())) { |
37 | result.rejectValue("username","login.form.invalid"); | 37 | result.rejectValue("username","login.form.invalid"); |
38 | return "login"; | 38 | return "login"; |
39 | } | 39 | } |
40 | - | ||
41 | - userSession.setUsername(userFromDao.getUsername()); | 40 | + |
41 | + userSession.setUsername(userFromRepository.getUsername()); | ||
42 | 42 | ||
43 | return "redirect:/"; | 43 | return "redirect:/"; |
44 | } | 44 | } |
src/main/java/fr/plil/sio/web/mvc/NewUserController.java
@@ -18,7 +18,7 @@ public class NewUserController { | @@ -18,7 +18,7 @@ public class NewUserController { | ||
18 | private UserSession userSession; | 18 | private UserSession userSession; |
19 | 19 | ||
20 | @Resource | 20 | @Resource |
21 | - private UserValidator userValidator; | 21 | + private UserFormValidator userFormValidator; |
22 | 22 | ||
23 | @RequestMapping(value = {"/newUser"}, method = RequestMethod.GET) | 23 | @RequestMapping(value = {"/newUser"}, method = RequestMethod.GET) |
24 | public ModelAndView getNewUserForm() { | 24 | public ModelAndView getNewUserForm() { |
@@ -32,7 +32,7 @@ public class NewUserController { | @@ -32,7 +32,7 @@ public class NewUserController { | ||
32 | result.rejectValue("username", "new.user.only.admin"); | 32 | result.rejectValue("username", "new.user.only.admin"); |
33 | } | 33 | } |
34 | 34 | ||
35 | - userValidator.validate(user, result); | 35 | + userFormValidator.validate(user, result); |
36 | 36 | ||
37 | boolean present = (userRepository.findByUsername(user.getUsername()) != null); | 37 | boolean present = (userRepository.findByUsername(user.getUsername()) != null); |
38 | 38 | ||
@@ -57,7 +57,7 @@ public class NewUserController { | @@ -57,7 +57,7 @@ public class NewUserController { | ||
57 | this.userSession = userSession; | 57 | this.userSession = userSession; |
58 | } | 58 | } |
59 | 59 | ||
60 | - public void setUserValidator(UserValidator userValidator) { | ||
61 | - this.userValidator = userValidator; | 60 | + public void setUserFormValidator(UserFormValidator userFormValidator) { |
61 | + this.userFormValidator = userFormValidator; | ||
62 | } | 62 | } |
63 | } | 63 | } |
@@ -0,0 +1,31 @@ | @@ -0,0 +1,31 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import javax.validation.constraints.Max; | ||
4 | +import javax.validation.constraints.Min; | ||
5 | +import javax.validation.constraints.NotNull; | ||
6 | + | ||
7 | +public class UserForm { | ||
8 | + | ||
9 | + @Min(3) | ||
10 | + @Max(16) | ||
11 | + private String username; | ||
12 | + | ||
13 | + @NotNull | ||
14 | + private String password; | ||
15 | + | ||
16 | + public String getUsername() { | ||
17 | + return username; | ||
18 | + } | ||
19 | + | ||
20 | + public void setUsername(String username) { | ||
21 | + this.username = username; | ||
22 | + } | ||
23 | + | ||
24 | + public String getPassword() { | ||
25 | + return password; | ||
26 | + } | ||
27 | + | ||
28 | + public void setPassword(String password) { | ||
29 | + this.password = password; | ||
30 | + } | ||
31 | +} |
src/main/java/fr/plil/sio/web/mvc/UserValidator.java renamed to src/main/java/fr/plil/sio/web/mvc/UserFormValidator.java
@@ -5,24 +5,37 @@ import org.springframework.validation.Errors; | @@ -5,24 +5,37 @@ import org.springframework.validation.Errors; | ||
5 | import org.springframework.validation.Validator; | 5 | import org.springframework.validation.Validator; |
6 | 6 | ||
7 | @Component | 7 | @Component |
8 | -public class UserValidator implements Validator { | 8 | +public class UserFormValidator implements Validator { |
9 | 9 | ||
10 | @Override | 10 | @Override |
11 | public boolean supports(Class<?> clazz) { | 11 | public boolean supports(Class<?> clazz) { |
12 | - return User.class.equals(clazz); | 12 | + return UserForm.class.equals(clazz); |
13 | } | 13 | } |
14 | 14 | ||
15 | - @Override | ||
16 | - public void validate(Object target, Errors errors) { | ||
17 | - User user = (User)target; | 15 | + private void checkUsername(String username, Errors errors) { |
18 | 16 | ||
19 | - if (user.getUsername().length() < 3) { | ||
20 | - errors.rejectValue("username", "validator.user.username.minimal.size"); | ||
21 | - } | 17 | + } |
18 | + | ||
19 | + private void checkPassword(String password, Errors errors) { | ||
20 | + boolean hasUppercase = !password.equals(password.toLowerCase()); | ||
21 | + boolean hasLowercase = !password.equals(password.toUpperCase()); | ||
22 | + boolean hasSpecial = !password.matches("[A-Za-z0-9 ]*"); | ||
22 | 23 | ||
23 | - if (user.getPassword().length() < 3) { | ||
24 | - errors.rejectValue("password", "validator.user.password.minimal.size"); | 24 | + if (!hasUppercase) { |
25 | + errors.rejectValue("password", "validator.user.form.must.contains.uppercase"); | ||
26 | + } | ||
27 | + if (!hasLowercase) { | ||
28 | + errors.rejectValue("password", "validator.user.form.must.contains.lowercase"); | ||
29 | + } | ||
30 | + if (!hasSpecial) { | ||
31 | + errors.rejectValue("password", "validator.user.form.must.contains.special.char"); | ||
25 | } | 32 | } |
26 | } | 33 | } |
27 | 34 | ||
28 | -} | 35 | + @Override |
36 | + public void validate(Object target, Errors errors) { | ||
37 | + UserForm user = (UserForm) target; | ||
38 | + checkUsername(user.getUsername(), errors); | ||
39 | + checkPassword(user.getPassword(), errors); | ||
40 | + } | ||
41 | +} | ||
29 | \ No newline at end of file | 42 | \ No newline at end of file |
src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java
@@ -17,7 +17,7 @@ public class NewUserControllerTest { | @@ -17,7 +17,7 @@ public class NewUserControllerTest { | ||
17 | private User user; | 17 | private User user; |
18 | private UserRepository userRepository; | 18 | private UserRepository userRepository; |
19 | private UserSession userSession; | 19 | private UserSession userSession; |
20 | - private UserValidator userValidator; | 20 | + private UserFormValidator userFormValidator; |
21 | 21 | ||
22 | @Before | 22 | @Before |
23 | public void createInstances() { | 23 | public void createInstances() { |
@@ -33,8 +33,8 @@ public class NewUserControllerTest { | @@ -33,8 +33,8 @@ public class NewUserControllerTest { | ||
33 | userSession = new UserSession(); | 33 | userSession = new UserSession(); |
34 | userSession.setUsername("admin"); | 34 | userSession.setUsername("admin"); |
35 | newUserController.setUserSession(userSession); | 35 | newUserController.setUserSession(userSession); |
36 | - userValidator = new UserValidator(); | ||
37 | - newUserController.setUserValidator(userValidator); | 36 | + userFormValidator = new UserFormValidator(); |
37 | + newUserController.setUserFormValidator(userFormValidator); | ||
38 | } | 38 | } |
39 | 39 | ||
40 | @Test | 40 | @Test |
src/test/java/fr/plil/sio/web/mvc/UserValidatorTest.java renamed to src/test/java/fr/plil/sio/web/mvc/UserFormValidatorTest.java
1 | package fr.plil.sio.web.mvc; | 1 | package fr.plil.sio.web.mvc; |
2 | 2 | ||
3 | -import static org.junit.Assert.*; | ||
4 | import org.junit.Before; | 3 | import org.junit.Before; |
5 | import org.junit.Test; | 4 | import org.junit.Test; |
6 | import org.springframework.validation.BeanPropertyBindingResult; | 5 | import org.springframework.validation.BeanPropertyBindingResult; |
7 | import org.springframework.validation.Errors; | 6 | import org.springframework.validation.Errors; |
8 | 7 | ||
9 | -public class UserValidatorTest { | 8 | +import static org.junit.Assert.assertFalse; |
9 | +import static org.junit.Assert.assertTrue; | ||
10 | + | ||
11 | +public class UserFormValidatorTest { | ||
10 | 12 | ||
11 | private User user; | 13 | private User user; |
12 | - private UserValidator validator; | 14 | + private UserFormValidator validator; |
13 | private Errors results; | 15 | private Errors results; |
14 | 16 | ||
15 | @Before | 17 | @Before |
16 | public void createInstances() { | 18 | public void createInstances() { |
17 | - validator = new UserValidator(); | 19 | + validator = new UserFormValidator(); |
18 | user = new User(); | 20 | user = new User(); |
19 | user.setUsername("abc"); | 21 | user.setUsername("abc"); |
20 | user.setPassword("abc"); | 22 | user.setPassword("abc"); |