Bonjour ,
J'apprends actuellement Spring et lors d'un projet j'ai voulu ajouter une page inscription.
Le soucis c'est que après que j'appuis sur le bouton inscription spring me redirige vers login ?
Voici les sources , perso je ne comprends pas trop ce qui pose problème.
MvcConfig
Utilisateurs
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 package hello; import javax.validation.Valid; import org.springframework.context.annotation.Configuration; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/").setViewName("home"); registry.addViewController("/hello").setViewName("hello"); registry.addViewController("/login").setViewName("login"); registry.addViewController("/inscription").setViewName("inscription"); } @RequestMapping(value="/" ,method=RequestMethod.GET) public String showInscript(utilisateurs user) { return "inscription"; } @RequestMapping(value="/",method=RequestMethod.POST) public String checkUtilisateursInfo(@Valid utilisateurs user,BindingResult bindingResult) { if(bindingResult.hasErrors()) { return "inscription"; } return "redirect:/hello"; } }
WebSecurityConfig
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package hello; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class utilisateurs { @NotNull @Size(min=3,max=30) private String user; @NotNull @Size(min=3,max=30) private String pass; public utilisateurs(String user, String pass) { this.user = user; this.pass = pass; } /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } /** * @return the pass */ public String getPass() { return pass; } /** * @param pass the pass to set */ public void setPass(String pass) { this.pass = pass; } }
Home.html
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 package hello; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; @Configuration @EnableWebMvcSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/home").permitAll() .anyRequest().authenticated(); http .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Configuration protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter { @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("Giovanni").password("gio").roles("USER"); } } }
inscription.html
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Spring Security Example</title> </head> <body> <h1>Welcome!</h1> <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p> <p><a th:href="@{/inscription}"><input type="submit" value="S'inscrire"/></a></p> </body> </html>
Tout me semble correcte mais la page inscription s'affiche pas dans le navigateur après l'appuis sur mon bouton ???
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Inscription</title> </head> <body> <div th:if="${#fields.hasErrors('user')}" th:errors="*{user}"> Invalid username or password. </div> <div th:if="${#fields.hasErrors('pass')}" th:errors="*{pass}"> Invalid username or password. </div> <form action="#" th:action="@{/}" th:object="@{/utilisateurs}" method="post"> <div><label> User Name : <input type="text" name="username" th:field="*{user}"/> </label></div> <div><label> Password: <input type="password" name="password" th:field="*{pass}"/> </label></div> <div><input type="submit" value="S'inscrire"/></div> </form> </body> </html>
Je remercie d'avance celui/celle qui pourra m'aider à résoudre ce soucis.
Partager