1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.samples.sprhib.web;
20
21 import org.apache.shiro.SecurityUtils;
22 import org.apache.shiro.authc.UsernamePasswordToken;
23 import org.apache.shiro.samples.sprhib.service.UserService;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Controller;
26 import org.springframework.ui.Model;
27 import org.springframework.validation.BindingResult;
28 import org.springframework.web.bind.annotation.ModelAttribute;
29 import org.springframework.web.bind.annotation.RequestMapping;
30 import org.springframework.web.bind.annotation.RequestMethod;
31
32
33
34
35 @Controller
36 public class SignupController {
37
38 private SignupValidator signupValidator = new SignupValidator();
39
40 private UserService userService;
41
42 @Autowired
43 public void setUserService(UserService userService) {
44 this.userService = userService;
45 }
46
47 @RequestMapping(value="/signup",method= RequestMethod.GET)
48 public String showSignupForm(Model model, @ModelAttribute SignupCommand command) {
49 return "signup";
50 }
51
52 @RequestMapping(value="/signup",method= RequestMethod.POST)
53 public String showSignupForm(Model model, @ModelAttribute SignupCommand command, BindingResult errors) {
54 signupValidator.validate(command, errors);
55
56 if( errors.hasErrors() ) {
57 return showSignupForm(model, command);
58 }
59
60
61 userService.createUser( command.getUsername(), command.getEmail(), command.getPassword() );
62
63
64 SecurityUtils.getSubject().login(new UsernamePasswordToken(command.getUsername(), command.getPassword()));
65
66 return "redirect:/s/home";
67 }
68
69 }