Skip to content
Advertisement

All Spring-Boot Application always show a popup login

I’m using Spring and Spring Boot framework with Spring Security for a simple login projects with simple UI. In all my projects, using STS, after starting my application and run the localhost address without errors, i receive a popup login window and not my html page. If i use user and default security password i always get a Whitelabel Error Page as results. Furthermore, if i use incorrect value the popup remain, if instead i use the default access value i get this message.

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Aug 23 20:14:22 CEST 2017
There was an unexpected error (type=Not Found, status=404).
No message available

Here is a simple project code. All the html files are in template folder as you can see in project structure. I also try to use server.error.whitelabel.enabled=false in my application.properties file.`

Why has the html interface never been shown?

Project Structure

Application

package com.cyberdemon.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootUiLoginApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootUiLoginApplication.class, args);
    }
}

SecurityConfig

package config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.WebSecurityConfigurerAdapter;

@Configuration
@EnableAutoConfiguration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select username,password, enabled from users where username=?")
                .authoritiesByUsernameQuery("select username, role from user_roles where username=?");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/", "/home").permitAll().antMatchers("/admin").hasRole("ADMIN")
                .anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout()
                .permitAll();
        http.exceptionHandling().accessDeniedPage("/403");
    }

}

WebController

package controller;

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

@Controller
public class WebController {

    @RequestMapping(value = { "/", "home" })
    public String home() {
        return "home";
    }

    @RequestMapping(value = { "/welcome" })
    public String welcome() {
        return "welcome";
    }

    @RequestMapping(value = "/admin")
    public String admin() {
        return "admin";
    }

    @RequestMapping(value = { "/login" })
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/403")
    public String Error403() {
        return "403";
    }

}

Pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cyberdemon.springboot</groupId>
    <artifactId>springboot_UI_Login</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot_UI_Login</name>
    <description>Project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

properties

spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=toor

Console

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.6.RELEASE)

2017-08-23 19:57:05.949  INFO 7496 --- [           main] c.c.s.SpringbootUiLoginApplication       : Starting SpringbootUiLoginApplication on DESKTOP-CYBERDEMON with PID 7496 (started by GD in C:UsersmeDocumentsworkspace-stsspringboot_ui_login)
2017-08-23 19:57:05.949  INFO 7496 --- [           main] c.c.s.SpringbootUiLoginApplication       : No active profile set, falling back to default profiles: default
2017-08-23 19:57:06.121  INFO 7496 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6cc7b4de: startup date [Wed Aug 23 19:57:06 CEST 2017]; root of context hierarchy
2017-08-23 19:57:07.293  INFO 7496 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-23 19:57:07.300  INFO 7496 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-08-23 19:57:07.300  INFO 7496 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-08-23 19:57:07.409  INFO 7496 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-08-23 19:57:07.409  INFO 7496 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1288 ms
2017-08-23 19:57:07.566  INFO 7496 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-23 19:57:07.566  INFO 7496 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-23 19:57:07.566  INFO 7496 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-23 19:57:07.566  INFO 7496 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-08-23 19:57:07.566  INFO 7496 --- [ost-startStop-1] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2017-08-23 19:57:07.566  INFO 7496 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-23 19:57:07.847  INFO 7496 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6cc7b4de: startup date [Wed Aug 23 19:57:06 CEST 2017]; root of context hierarchy
2017-08-23 19:57:07.894  INFO 7496 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-23 19:57:07.894  INFO 7496 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-23 19:57:07.925  INFO 7496 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-23 19:57:07.925  INFO 7496 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-23 19:57:07.956  INFO 7496 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-23 19:57:08.519  INFO 7496 --- [           main] b.a.s.AuthenticationManagerConfiguration : 

Using default security password: 21cf3953-b4e3-48ac-87fa-9041c41b3ff8

2017-08-23 19:57:08.567  INFO 7496 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/css/**'], Ant [pattern='/js/**'], Ant [pattern='/images/**'], Ant [pattern='/webjars/**'], Ant [pattern='/**/favicon.ico'], Ant [pattern='/error']]], []
2017-08-23 19:57:08.645  INFO 7496 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/**']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@12fcc71f, org.springframework.security.web.context.SecurityContextPersistenceFilter@5d43409a, org.springframework.security.web.header.HeaderWriterFilter@76563d26, org.springframework.security.web.authentication.logout.LogoutFilter@102ecc22, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@4eba373c, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@6ede46f6, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@10a0fe30, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5679e96b, org.springframework.security.web.session.SessionManagementFilter@23b8d9f3, org.springframework.security.web.access.ExceptionTranslationFilter@261ea657, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@6cd64ee8]
2017-08-23 19:57:08.864  INFO 7496 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-08-23 19:57:08.911  INFO 7496 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-23 19:57:08.926  INFO 7496 --- [           main] c.c.s.SpringbootUiLoginApplication       : Started SpringbootUiLoginApplication in 3.164 seconds (JVM running for 3.428)
2017-08-23 19:57:24.008  INFO 7496 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-08-23 19:57:24.008  INFO 7496 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-08-23 19:57:24.021  INFO 7496 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 13 ms

Advertisement

Answer

You need to restructure your packages to that they fall under the package of your SpringbootUiLoginApplication or use @ComponentScan to tell the Spring Boot where to find your bean classes.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement