【发布时间】:2021-02-21 17:16:30
【问题描述】:
我是 Spring boot 和 Spring boot security 的新手。但是,使用现有的示例代码,我编写了一个可以正常工作的代码。在我的设计中,我有一个单独的登录页面,名为login.html。现在,我改变了我的设计,我的登录和注册在主页中的单个form 中,这似乎有问题。
这里我更详细地解释了这个问题:
- 我打开主页,然后单击登录按钮。 2)我输入用户名和密码,然后单击登录按钮。然后,我按预期看到了
dashboard页面。到目前为止,一切顺利。
首页,如下所示:
这是home.html中的登录/注册表单的代码:
<form th:action="@{/signup}" method="post">
<input type="hidden" name="action" id="action" value="">
<div>
<input type="email" name="email" id="inputEmail" placeholder="email" required>
</div>
<div>
<input type="password" name="password" id="inputPassword" placeholder="password" required>
</div>
<button type="submit">SIGN IN</button>
</form>
这里是signin/signup 控制器:
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult, @RequestParam("action") String action) {
String act = action;
ModelAndView modelAndView = new ModelAndView();
User userExists = userService.findUserByEmail(user.getEmail());
if(action.equalsIgnoreCase("signup")) {
if (userExists != null) {
bindingResult
.rejectValue("email", "error.user",
"There is already a user registered with the username");
}
if (bindingResult.hasErrors()) {
modelAndView.setViewName("signup");
} else {
userService.saveUser(user);
modelAndView.addObject("successMessage", "User has been registered successfully");
modelAndView.addObject("user", new User());
modelAndView.setViewName("home");
}
}else if(action.equalsIgnoreCase("signin")) {
modelAndView.addObject("currentUser", userExists);
modelAndView.addObject("firstname", userExists.getFirstname());
modelAndView.addObject("lastname", userExists.getLastname());
modelAndView.addObject("adminMessage", "Content Available Only for Users with Admin Role");
modelAndView.setViewName("dashboard");
}
return modelAndView;
}
到目前为止,一切正常,登录后,我被转发到dashboard 页面,如下所示:
现在,如果我单击应该将我转发到个人资料页面的John,或者单击应该将我转发到另一个页面的submit,它会返回到用户未登录的主页。
这是仪表板中的表单,当我单击submit 按钮时,它应该调用控制器方法:
<form action="/addReview" method="POST" name="addReviewForm"onsubmit="return validateForm()">
<input type="hidden" id="category"name="category" />
<input type="text" id="first_input" name="first_point" placeholder="Point 1">
<input type="text" id="seventh_input" name="seventh_point" placeholder="Point 7">
<button type="submit">submit</button></form>
这里是安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
// .antMatchers("/login").permitAll()
.antMatchers("/signup").permitAll()
.antMatchers("/search").permitAll()
.antMatchers("/dashboard/**").hasAuthority("ADMIN").anyRequest()
.authenticated().and().csrf().disable().formLogin().successHandler(customizeAuthenticationSuccessHandler)
.loginPage("/").failureUrl("/?error=true")
.usernameParameter("email")
.passwordParameter("password")
.and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/").and().exceptionHandling();
}
我认为问题出在.loginPage("/").failureUrl("/?error=true"),但我不确定。我没有任何单独的登录页面。
同时,函数validateForm() 被正确调用。只是控制器方法没有被调用。
我希望有人可以帮助我。
提前致谢,
##更新 1:##
我更改并简化了代码,以便更容易找到问题。
现在,我的home.html 中有以下表格:
<form th:action="@{/}" method="post">
<input type="email" name="email" id="inputEmail" placeholder="email" required>
<input type="password" name="password" id="inputPassword" placeholder="password" required>
<input type="submit" value="SIGN IN" />
以及以下配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/search").permitAll()
.antMatchers("/dashboard/**").hasAuthority("ADMIN").anyRequest()
.authenticated()
.and().csrf().disable().formLogin().successHandler(customizeAuthenticationSuccessHandler)
.loginPage("/")
.failureUrl("/?error=true")
.usernameParameter("email")
.passwordParameter("password").and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/").and().exceptionHandling();
}
还有以下控制器:
@RequestMapping(value = "/", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult, @RequestParam("email") String email) {
ModelAndView modelAndView = new ModelAndView();
User userExists = userService.findUserByEmail(user.getEmail());
modelAndView.addObject("currentUser", userExists);
modelAndView.addObject("firstname", userExists.getFirstname());
modelAndView.addObject("lastname", userExists.getLastname());
modelAndView.setViewName("dashboard");
return modelAndView;
}
@RequestMapping(value = {"/","/home"}, method = RequestMethod.GET)
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("home");
return modelAndView;
}
使用此代码,我什至看不到仪表板。点击sign in 按钮返回http://localhost:8080/?error=true 页面。
【问题讨论】:
标签: java html spring spring-mvc spring-security