1
1
package de .cwansart .unipoll ;
2
2
3
3
import org .springframework .beans .factory .annotation .Autowired ;
4
+ import org .springframework .http .HttpStatusCode ;
4
5
import org .springframework .stereotype .Controller ;
5
6
import org .springframework .ui .Model ;
6
7
import org .springframework .web .bind .annotation .GetMapping ;
7
8
import org .springframework .web .bind .annotation .ModelAttribute ;
8
9
import org .springframework .web .bind .annotation .PostMapping ;
10
+ import org .springframework .web .bind .annotation .RequestParam ;
11
+ import org .springframework .web .server .ResponseStatusException ;
9
12
10
13
class LoginForm {
11
14
private String password ;
@@ -23,20 +26,24 @@ public class LoginController {
23
26
private AuthService auth ;
24
27
25
28
@ GetMapping ("/login" )
26
- public String login (Model model ) {
29
+ public String login (@ RequestParam ( name = "p" , required = true ) String page , Model model ) {
27
30
if (auth .isAuthenticated ()) {
28
- return "redirect:/create" ;
31
+ return "redirect:/" + page ;
29
32
}
30
33
model .addAttribute ("loginForm" , new LoginForm ());
34
+ model .addAttribute ("page" , page );
31
35
return "login" ;
32
36
}
33
37
34
38
@ PostMapping ("/login" )
35
- public String doLogin (@ ModelAttribute ("loginForm" ) LoginForm loginForm , Model model ) {
39
+ public String doLogin (@ ModelAttribute ("loginForm" ) LoginForm loginForm , @ RequestParam ( name = "p" , required = true ) String page , Model model ) {
36
40
if (!auth .login (loginForm .getPassword ())) {
37
41
model .addAttribute ("error" , "Invalid password!" );
38
42
} else {
39
- return "redirect:/create" ;
43
+ if (!page .equals ("create" ) && !page .equals ("list" )) {
44
+ throw new ResponseStatusException (HttpStatusCode .valueOf (400 ), "unknown redirect page" );
45
+ }
46
+ return "redirect:/" + page ;
40
47
}
41
48
model .addAttribute ("loginForm" , new LoginForm ());
42
49
return "login" ;
0 commit comments