Spring security customized login page doesn't allow me in

Viewed 106

I have an issue when logging in to the application via customized login page. There is no error in the application console but the moment I click on log in, the URL changes from http://localhost:8080/login to http://localhost:8080/login?error. I cannot find out the reason for that, I will put my classes and view below and your help is much appreciated.

Security configuration class

package myProject.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@SuppressWarnings("deprecation")
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired
    private UserDetailsService userService;

    @Override
    public void configure(HttpSecurity theHttp) throws Exception {
        theHttp.authorizeRequests()
                .antMatchers("/design", "/order")
                    .access("hasRole('ROLE_USER')")
                .antMatchers("/", "/**")
                    .access("permitAll")
                .and()
                .formLogin()
                    .loginPage("/login")
                    .defaultSuccessUrl("/", true) 
                .and()
                .logout()
                    .logoutSuccessUrl("/login")
                .and()
                .csrf()
                    .ignoringAntMatchers("/h2-console/**")
                .and()
                .headers()
                    .frameOptions()
                    .sameOrigin();
    }

    @Bean
    public PasswordEncoder encoder() {
        return new StandardPasswordEncoder("53cr3t");
    }

    @Override
    public void configure(AuthenticationManagerBuilder theAuth) throws Exception {
        theAuth.userDetailsService(userService)
                .passwordEncoder(encoder()/*encoder*/);
    }

}

Login view

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

      <head>
        <title>My Project</title>
      </head>

      <body>
        <h1>Login</h1>
        <img th:src="@{/images/image.png}">

        <div th:if="${error}">
          Unable to login. Check your username and password.
        </div>

        <p>New here? Click
          <a th:href="@{/register}"> to rgister.</a>
        </p>

        <form method="POST" th:action="@{/login}" id="loginForm">

          <!-- User name -->
          <label for="username">Username: </label>
          <input type="text" name="username" id="username" /> <br/>
          <!-- Password -->
          <label for="password">Password: </label>
          <input type="password" name="password" id="password" /> <br/>

          <!-- Login button -->
          <input type="submit" value="Login"/>

        </form>

      </body>

</html>

This line was in consloe: - 2021-03-21 09:41:48.380 INFO 9276 --- [nio-8080-exec-3] c.vaadin.flow.spring.SpringInstantiator : The number of beans implementing 'I18NProvider' is 0. Cannot use Spring beans for I18N, falling back to the default behavior

1 Answers

The fault was in UserRepositoryDetailsService class. It is annotated as @Service but in my code the @Service in imported by import org.jvnet.hk2.annotations.Service;. However it should be imported from import org.springframework.stereotype.Service;. Once I did fix it all other issues solved.

Related