java - 登录在带有 Thymeleaf 的 Spring MVC 中不起作用
问题描述
我的登录没有做任何事情。我做了一个类似的项目,它在那里工作。注册正在工作,但登录按钮没有做任何事情。任何想法为什么不工作?
我将放下我的 DemoApplication、SecurityConfig 和 login.html。
- 登录.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);
}
}
- - 请帮忙 - -
解决方案
您应该避免使用 type="image" 因为它是有问题的,因为传递值的能力被禁用。
请改用按钮。
<button type="submit" name="someName" value="someValue"><img src="someImage.png" alt="SomeAlternateText"></button>
推荐阅读
- sql-server-2008 - SQL Server 2008:Executenonquery 不返回受影响的行数
- automapper - AutoMapper.Collection.EFCore - 配置期间抛出错误
- java - 组件异常
- r - 如何在 Shiny App 中显示表的列名而不用点替换空格并删除表名?
- python - 如何使用下面代码中的 for 循环获取三个导出的 png 文件?
- git - Git:如何查找何时将提交合并到主控中?
- spring - 照片分享应用系统设计
- python - 检查 object-dtype 列值是浮点数还是字符串的函数
- asp.net-mvc - 解密始终加密的列 ASP.NET MVC 应用程序
- jquery - 我无法更改图像幻灯片