当前位置: 首页 > 知识库问答 >
问题:

所有Spring Boot应用程序始终显示一个弹出式登录

谷泽宇
2023-03-14

我使用Spring和Spring Boot框架与Spring Security简单的登录项目与简单的用户界面。在我所有的项目中,使用STS,在启动我的应用程序并运行localhost地址后没有错误,我收到一个弹出的登录窗口,而不是我的html页面。如果我使用用户和默认的安全密码,我总是得到一个白色标签错误页作为结果。此外,如果我使用不正确的值,弹出窗口仍然存在,如果我使用默认的访问值,我会得到这条消息。

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

下面是一个简单的项目代码。正如您在项目结构中看到的,所有html文件都位于模板文件夹中。我还尝试使用服务器。错误白标签。在我的应用程序中,enabled=false。属性文件`

为什么html界面从未显示过?

项目结构

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);
    }
}
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");
    }

}
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";
    }

}
<?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>
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=toor
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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:\Users\me\Documents\workspace-sts\springboot_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

共有2个答案

贾烨
2023-03-14

跟进阿里的回答:

@SpringBootApplication意味着@Configuration、@EnableAutoConfiguration和@ComponentScan

所以,考虑改变:

@Configuration
@EnableAutoConfiguration
public class SecurityConfig extends WebSecurityConfigurerAdapter

致:

@Component
public class SecurityConfig extends WebSecurityConfigurerAdapter

并扫描整个包裹更改中的bean:

@SpringBootApplication
public class SpringbootUiLoginApplication

致:

@SpringBootApplication
@ComponentScan("com.cyberdemon")
public class SpringbootUiLoginApplication
申屠涛
2023-03-14

您需要重新构造您的包,使其属于SpringbootUiLoginApplication的包,或者使用@ComponentScan告诉SpringBoot在哪里可以找到bean类。

 类似资料:
  • 我知道APIG中有一个设置自定义域名的选项,并且在做了一些研究之后,APIG在后台设置了一个Cloudfront发行版,但由于我不是设置DNS服务的人,也没有权限在aws上更改此特定角色/区域的这些设置,我想知道问题是否在于自定义url没有通过APIG的选项设置?

  • 如果fcm消息包含“通知”部分,而应用程序在后台,则通知传递到系统托盘和数据中的附加意图。 问题是,我无法更改该通知的重要性,而且它们总是不显示弹出窗口。我为API26+修复了这个问题,我添加了通知通道 但对于API25-我找不到解决方案。一个建议是删除“notification”部分,只保留“data”,这将允许fcm将消息传递到 当应用程序在后台/前台,我将能够显示我的自定义通知。

  • 我有一个应用程序,可以让用户设置特定时间期限的任务。一旦过了最后期限,我希望应用程序在所有内容上显示一个视图(请求用户更新任务)。 我的问题是:-如何在当前活动的所有其他活动上显示我的应用程序的视图-如何在用户更新任务之前保持活动在顶部? 使用系统警报窗口这样的东西看起来合理吗?

  • 问题内容: 这个问题已经有了答案,但是我仍然不确定它是如何工作的。 我在footer.php中使用以下HTML: 和以下Javascript: 一切都很好,但是我只想对每个用户显示一次弹出窗口(也许使用所有论坛帖子都使用的cookie内容),但是我不知道确切如何将其合并到上面的JS中。 我知道我将必须在此页脚中加载cookie JS: 但这就是我的全部理解,有人能告诉我添加了Cookie的东西后J

  • 编辑1我知道这个问题已经问过了,但是没有人回答,stacktrace也没有添加到里面,所以我又问了一遍。 如果我安装了twitter应用程序,然后尝试登录,那么应用程序将成功地用twitter登录,但如果我从移动端卸载twitter应用程序,然后尝试登录应用程序,那么我收到以下错误,无法用twitter登录 未能获取请求令牌 我没有看到任何WebView。之后点击登录与twitter按钮。 E/T

  • 我是Spring靴的初学者。我制作了简单的crud应用程序,但在编译项目时没有任何错误,但在浏览器中看不到输出。我不知道为什么。 只有这样显示才能在浏览器中看到输出。我不知道为什么。 员工控制员看起来像这样 Index.html porm.xml spring.jpa.hibernate.ddl-car=无 spring.datasource.url=jdbc:mysql://localhost: