Spring Boot + Spring MVC + Thymeleaf自定义登录失败,但未在控制台中显示任何错误

时间:2019-01-20 14:59:59

标签: java spring-boot spring-mvc thymeleaf

我正在尝试使用Spring Boot + Thymeleaf建立一个简单的注册/登录机制。

我设法成功注册了一个用户(检查了h2-console中的新条目),但是尝试登录时遇到了问题。

具体来说,该应用程序被重定向到/ login?错误页面,并且控制台上没有任何消息表明出了什么问题。

调试后,我发现该应用程序不会以我的Post控制器方法停止。

我为您提供代码:

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

    @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";
    }
}

请帮助,谢谢!

编辑:

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();
    }
}

1 个答案:

答案 0 :(得分:0)

您不必自己实施登录。

只需使用以下形式创建一个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会为您处理一切。