我正在尝试创建一个简单的注册和登录系统。当我尝试注册用户时,我会向输入内容提供所有信息,例如用户名,密码和Confirmpassword,但是当我按下“提交”按钮时,它会将我重定向到/ login,即使它应该重定向到/ welcome页面,如我在控制器。当我在控制器中的特定映射中添加一个简单的调试时,没有任何反应,我以为该方法甚至没有执行,有人可以看一下这段代码并告诉我这里出了什么问题吗?
@Controller
public class UserController {
private UserService userService;
private SecurityService securityService;
@Autowired
public UserController(UserService userService, SecurityService securityService) {
this.userService = userService;
this.securityService = securityService;
}
@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registration(Model model) {
// model.addAttribute("user", new User());
//
return "registration";
}
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@RequestParam("username") String username, @RequestParam("password")
String password, @RequestParam("confirmPassword") String confirmPassword,
Model model, HttpServletRequest request) {
User foundUser = userService.findByUsername(username);
if (foundUser != null) {
model.addAttribute("msgError", "That user already exists");
return "registration";
}
if (username.equals("") || username.length() < 2) {
model.addAttribute("msgError", "Wrong username");
return "registration";
}
if (!password.equals(confirmPassword)) {
model.addAttribute("msgError", "Passwords can not be different");
return "registration";
}
User user = new User(request.getParameter("username"), request.getParameter("password"), request.getParameter("confirmPassword"));
securityService.autoLogin(username, password);
userService.addUser(user);
model.addAttribute("msgSuccess", "User registered successfully");
return "redirect:/welcome";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model, String error, String logout) {
if (error != null)
model.addAttribute("msgError", "Your username and password is invalid");
if (logout != null)
model.addAttribute("msgError", "You have been logged out successfully");
return "login";
}
}
注册的JSP如下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Watchers - Registration</title>
</head>
<body>
<div class="container">
<form method="POST" action="registration" class="sign-form">
<h2 class="sign-form-heading">Create an new account</h2>
<p><input type="text" name="username" /></p>
<p><input type="text" name="password" /></p>
<p><input type="text" name="confirmPassword" /></p>
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
我认为这是因为您的表单$ rails db:seed
值。
尝试使用spring的taglib准备动作值
在此示例中,其前缀为action
spring
编辑:
参考您的评论,我要问几个问题:
如何通过web.xml中的spring-security或security-constraints保护/保护Web应用程序?
<spring:url value="/registration" var="url"/>
<form method="POST" action="${url}" class="sign-form">
路径是否也受到保护/保护?如果是,并且您的登录配置引用了/registration
,您将被重定向到/login