简体   繁体   中英

Spring Boot Security custom login page doesn't load

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??

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!

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.So you need to include your own html page files, then they are accessible through your system. At least that was what worked for us in our project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM