Логин не работает весной MVC с Thymeleaf - PullRequest
1 голос
/ 19 апреля 2020

Мой логин ничего не делает. Я сделал похожий проект, и там он работал. Реестр работает, но кнопка «Вход» ничего не делает. Любые идеи, почему не работает?

Я положу мои DemoApplication, SecurityConfig и логин. 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);
    }
}

---- просьба помочь ----

1 Ответ

0 голосов
/ 19 апреля 2020

Вам следует избегать использования type = "image", так как это проблематично c, поскольку возможность передачи значения отключена.

Вместо этого используйте кнопку.

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