Creating a spring security login page results in a default page

Asked 2 years ago, Updated 2 years ago, 122 views

I am currently creating a login page with springboot, gradle, and spring security.
I would like to display html loginForm.html on thymeleaf.
Whatever you enter in the URL on the local host is skipped to /login and
The default spring security login page (attached screenshot) appears.
When I added http.httpBasic().disable(); to a similar question, I tried it, but it didn't work.
Please let me know.

buildgradle

plugins{
    id'org.springframework.boot'version'2.4.1'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id'java'
}

group='com.example'
version = '0.0.1 - SNAPSHOT'
sourceCompatibility='1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation'org.springframework.boot:spring-boot-starter-web'
    // Add springsecurity to dependencies
    implementation'org.springframework.boot:spring-boot-starter-security'
    // Add thymeleaf extension library to dependency
    implementation'org.tymeleaf.extras:tymeleaf-extras-springsecurity5'
    runtimeOnly 'mysql:mysql-connector-java'
}

SecurityConfig.java

@EnableWebSecurity
public class SecurityConfig extensions WebSecurityConfigurerAdapter {

    //  hash algorithm
    @ Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @ Override
    protected void configure(HttpSecurity http)throws Exception {

//       http.httpBasic().disable();

        // Configuring Authorization
        http.authorizeRequests()
                .antMatchers("/loginForm").permitAll()//loginForm is accessible to all users
                .antMatchers("/admin").hasAnyAuthority("ADMIN")//ADMIN user only
                .anyRequest().authenticated(); // Ask for authentication except for permitted items

        //  login process
        http.formLogin()
                .loginProcessingUrl("/login")// Path of login process
                .loginPage("/loginForm")//Specify Login Page
                .usernameParameter("email")// Email address of login page
                .passwordParameter ("password") // Login Page Password
                .defaultSuccessUrl("/home", true) // Path on Successful Login
                .failureUrl("/loginForm?error"); // Path on Login Failure
}

loginController

@Controller
public class LoginController {

    @GetMapping("/loginForm")
    public String getLogin() {
        return "loginForm";
    }
}

Photo of the default page displayed

java spring spring-boot thymeleaf spring-security

2022-09-29 22:29

1 Answers

I didn't know the cause, but I remade it and solved myself.Thank you.


2022-09-29 22:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.