Home > Back-end >  Spring Boot Security custom login page doesn't load
Spring Boot Security custom login page doesn't load

Time:10-04

I'm trying to implement a custom login page on my Spring project, but when I run the project and try to access the login page, I get the following error:

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri Oct 01 04:43:26 BRT 2021 There was an unexpected error (type=Not Found, status=404).

Here are the files:

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

TemplateController.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class TemplateController {

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

    @GetMapping("courses")
    public String getCourses() {
        return "courses";
    }
}

ApplicationSecurityConfiguration.java

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable() 
        .authorizeRequests()
        .antMatchers("/", "index", "css/*", "js/*").permitAll()
        .antMatchers("api/**").hasRole(CUSTOMER.name())
        .anyRequest()
        .authenticated()
        .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .defaultSuccessUrl("/courses", true)
            .and()
            .rememberMe();
    }

The login.html is located in a folder called templates, inside of resources (default folder on Spring project), and it seems to be working fine, since its view is displayed Visual/Source on Eclipse.

Does anybody know what may be causing this error??

CodePudding user response:

Problem solved, guys. I just moved the TemplateController.java to com.example.demo.controller package. Now it runs perfectly!

But thank you everyone who tried to help me!

CodePudding user response:

Try adding the html file like this into the code:

.antMatchers("/overview", "/employees/*",
                        "terminal/*", "/user/*", "/create").permitAll()

Those are all html pages from my own project, I had the same issue and with adding them like this the security did not interfere with the mapping.

  • Related