首页 > 解决方案 > 登录在带有 Thymeleaf 的 Spring MVC 中不起作用

问题描述

我的登录没有做任何事情。我做了一个类似的项目,它在那里工作。注册正在工作,但登录按钮没有做任何事情。任何想法为什么不工作?

我将放下我的 DemoApplication、SecurityConfig 和 login.html。

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <link href = '../css/login.css' rel = 'stylesheet'>
    <title> Login </title>
</head>
<body>
<div class = "panel">
    <form class="login-form"  th:action="@{/DS_login}" method="post">
    <input id = "loginButton" type="image" src="../img/b1.gif" name = "login" value="Login">
    <p> Login </p>
    <br>
    <br>
    <label> username </label>
    <br>
    <input type = "text" name = "username">
    <br>
    <label> password </label>
    <br>
    <input type = "password" name = "password">
    <br>
    <p id = "wrongLogin">Wrong credentials</p>
    </form>

    <p class="message">Not registered?  <a th:href = "@{/DS_Register}" href = "register.html">Register</a></p>
</div>

</body>
</html>

-DemoApplication

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
public class DemoApplication implements WebMvcConfigurer{

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/DS_login").setViewName("login");
    }
}

-SecurityConfig

package com.example.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/user/**").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/img/**").permitAll()
                .antMatchers("/DS_Register").permitAll()
                .anyRequest().authenticated().and().formLogin().loginPage("/DS_login").permitAll();

        http.formLogin()
                .loginPage("/DS_login")
                .defaultSuccessUrl("/DS_Home",true);
    }
}

- - 请帮忙 - -

标签: javaspringhibernatespring-mvc

解决方案


您应该避免使用 type="image" 因为它是有问题的,因为传递值的能力被禁用。

请改用按钮。

<button type="submit" name="someName" value="someValue"><img src="someImage.png" alt="SomeAlternateText"></button>

推荐阅读