简体   繁体   中英

My loginpage won't show on springboot application

I am currently implementing a login page of an application I want to make. I am now trying to get the loginpage (html file) to display when I visit http://localhost:3307/loginpage. But every time I do this I keep getting "Whitelabel Error Page". It is directing me to localhost:3307/loginpage but its giving this error instead of showing my html page. From my understanding this happens when you've made a mistake and its not actually interacting with the file.Would appreciate any help I can get, thanks.

My application Properties (I normally use the correct username/password)

server.port=3307

spring.datasource.url=jdbc:mysql://localhost:3306/bookcollection
spring.datasource.driverClassName= com.mysql.cj.jdbc.Driver
spring.datasource.username=
spring.datasource.password=

spring.jpa.properties.hibernate.format_sql=true
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop

My WebSecurity. I allowed access to the /loginpage.

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    private final UserService userService;
    private final BCryptPasswordEncoder passwordEncoder;
    
    public WebSecurityConfig(UserService userService, BCryptPasswordEncoder passwordEncoder) {
        this.userService = userService;
        this.passwordEncoder = passwordEncoder;
    }
    
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeHttpRequests()
                .antMatchers("/api/v*/registration/**")
                .permitAll()
                .anyRequest()
                .authenticated().and()
                .formLogin().loginPage("/loginpage").permitAll();
        
        return http.build();
    }

    
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        
    }
    
    @Bean
    public DaoAuthenticationProvider daoAutenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setPasswordEncoder(passwordEncoder);
        provider.setUserDetailsService(userService);
        return provider;
    }
}

This is my LoginController. From my understanding @RequestMapping is by default a Get request. I am using that print statement to see if anything would pop up in the console (it doesn't).

@Controller
public class LoginController {
    
    @GetMapping
    public String getLogin(Model model) {

        return "loginpage";
    }
}

This is my html file which is placed in resources/templates

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
</head>
<body>

</body>
</html>

The issue has been solved.

I decided to use Thymeleaf instead of JSP , and managed to get my page to be shown.

From the official Spring Boot documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.servlet.spring-mvc.static-content

By default, resources are mapped on /**, but you can tune that with the spring.mvc.static-path-pattern property. For instance, relocating all resources to /resources/** can be achieved as follows:

spring.mvc.static-path-pattern=/resources/**

You can also customize the static resource locations by using the spring.web.resources.static-locations property (replacing the default values with a list of directory locations). The root servlet context path, "/", is automatically added as a location as well.

In addition to the “standard” static resource locations mentioned earlier, a special case is made for Webjars content. Any resources with a path in /webjars/** are served from jar files if they are packaged in the Webjars format.

Do not use the src/main/webapp directory if your application is packaged as a jar . Although this directory is a common standard, it works only with war packaging, and it is silently ignored by most build tools if you generate a jar.

So consider moving JSP pages to another allowed directory. Also please change @RestController to @Controller. It will not work with @RestController because of @ResponseBody which included in this annotation

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