[英]Spring Boot + Spring MVC + Thymeleaf custom login fails without showing any error in console
I am trying to make a simple registration/login mechanism using Spring Boot + Thymeleaf. 我正在尝试使用Spring Boot + Thymeleaf建立一个简单的注册/登录机制。
I managed to register a user successfully (checked the new entry in the h2-console) but I have a problem while trying to log in. 我设法成功注册了一个用户(检查了h2-console中的新条目),但是尝试登录时遇到问题。
Specifically, the application is redirected to /login?error page and there is no message at the console indicating what is wrong. 具体来说,该应用程序将重定向到/ login?error页面,并且控制台上没有任何消息表明出了什么问题。
After debugging I discovered that the application does not stop at my Post controller method. 调试后,我发现该应用程序不会在我的Post控制器方法处停止。
I provide you with my code: 我为您提供代码:
login.html 的login.html
<form th:action="@{/login}" method="post" th:object="${user}">
<div class="form-group">
<label for="username">Username</label>
<input th:field="*{username}" type="text" id="username" name="username" class="form-control" autofocus="autofocus"
placeholder="Username">
</div>
<div class="form-group">
<label for="password">Password</label>:
<input th:field="*{password}" type="password" id="password" name="password" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit"
name="login-submit"
id="login-submit"
class="form-control btn btn-success"
value="Log In">
</div>
<div class="col-sm-6 col-sm-offset-3">
<a th:href="@{/register}">Register</a>
</div>
</div>
</div>
</form>
UserController.java UserController.java
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;
@GetMapping("/login")
public String login(Model model) {
model.addAttribute("user", new User());
return "login";
}
@PostMapping("/login")
public String login(@ModelAttribute("user") User user) {
securityService.login(user.getUsername(), user.getPassword());
return "redirect:/index";
}
@GetMapping("/register")
public String register(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping("/register")
public String register(@ModelAttribute("user") User user) {
userService.save(user);
return "redirect:/login";
}
}
Please help, thanks in advance! 请帮助,在此先感谢!
EDIT: 编辑:
SecurityConfig.java SecurityConfig.java
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/h2-console/**", "/register**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();
http.csrf().disable();
http.headers().frameOptions().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
@Bean("authenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
You don't have to implement the Login by yourself. 您不必自己实施登录。
Simply create a login.html with a form like this: 只需使用以下形式创建一个login.html:
<form action="#" th:action="@{/login}" method="post">
<input class="form-control" id="username" name="username" th:autofocus="true"/>
<input class="form-control" id="password" name="password"
<button>Login</button>
</form>
Spring will handle everything for you. Spring将为您处理一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.