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

为spring boot应用程序创建默认安全登录页面

司徒骞尧
2023-03-14
package com.example.demo;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableAdminServer
@SpringBootApplication
public class DemoApplication {

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

    @Configuration
public static class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
        http.logout().logoutUrl("/logout");
        http.csrf().disable();

        http.authorizeRequests().antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**").permitAll();
        http.authorizeRequests().antMatchers("/**").authenticated();

        http.httpBasic();
    }
}

共有1个答案

缪征
2023-03-14

Springboot管理服务器已经有了登录页面,不需要为它创建显式模板。虽然我相信它应该是可定制的,但还没有深入到它。下面是我的Springboot管理默认登录页面的工作解决方案。

主应用程序类

@EnableAdminServer
@SpringBootApplication
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}

Spring安全配置

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {  
    private final String adminContextPath;
    public WebSecurityConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }   
    @Override
    protected void configure(HttpSecurity http) throws Exception {    
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                .httpBasic().and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
}

在管理服务器上登录的凭据在Application.Properties中定义为

spring.security.user.name=admin
spring.security.user.password=Admin#123

如果有帮助就告诉我。

编辑:我试图使用您提供的文件复制场景。pom.xml缺少一些依赖项。解决后,我可以成功登录到管理服务器。你能按下面的方式更新你的pom然后重试吗。请参考我在pom.xml中添加的注释

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version> <!-- EspringDev CHANGED this version -->
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>admin</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.0.2</spring-boot-admin.version> <!-- EspringDev CHANGED this version -->
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

       <!-- EspringDev ADDED below 2 dependencies -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty</artifactId>
            <version>0.8.6.RELEASE</version>
        </dependency>
      <!-- dependencies -->


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>
 类似资料:
  • 通过设置安全码,从而达到隐藏登录页的效果,保证页面的私有性。 /app/Application/Admin/Conf/config.php return array( 'LOGIN_MAX_FAILD' => 5, 'BAN_LOGIN_TIME' => 30, //登录失败5次之后需等待30分钟才可再次登录 'ADMIN_PANEL_SECURITY_CODE' => '

  • 我已经设计了我自己的登录和注册页面,我想使用。如何使用它们而不是default。是否有任何API我可以调用或可能是我的后端会这样做?我还读到有用于keycloak的spring适配器,我可以使用它们吗?任何链接到任何例子都是好的。 我的第二个问题是,在注册时,我可以在中添加更多的用户详细信息,如地址、dob、性别吗?因为我的注册页面需要这些信息。

  • 我正试图创建一些PHP代码与登录表单的网站,如果用户将尝试登录用户名和密码,页面将显示“欢迎…”。当用户试图输入用户名和密码时,显示的网站是空白的,上面什么都没有。另外,我已经用用户名和密码创建了mysql数据库。 我主页上的这个登录表单index.html: 这是我的php页面login.php: 在我的欢迎页面-第1页。html我包含了一些php代码:

  • 我知道这个问题已经被问过很多次了,一直都有一个答案是关于使用一个可执行的jar或制作一个。exe使用launch4j或类似应用程序。 我可能听起来像个新手,实际上我是。 我一直在用一个Java项目尝试一些东西。我已经成功地制作了一个可执行的jar和一个. exe文件。这都要归功于你之前在SO中的回答:) 但是,我想为Windows创建一个安装程序。例如,按Next 2-3次(显示所有条款和条件等)

  • 问题内容: 我知道这个问题已经被问过很多次了,而且一直有一个答案说明使用可执行jar或使用launch4j或类似应用程序制作.exe。 我听起来像是一个新手,实际上是我。 我一直在尝试Java项目中的一些事情。我已经成功地制作了一个可执行的jar和一个.exe文件。非常感谢您之前在SO中的回答:) 但是,我想为Windows创建一个安装程序。像是,按下一步2至3次(显示所有条款和条件等),然后用户

  • 我是一个XCode呆子,我到处找答案。这是我第一次在XCode中构建任何东西,我在ViewController.swift中使用Firebase创建了一个登录窗口。一旦用户登录,我希望他们到另一个屏幕,使用MapKit等,我如何自动链接登录窗口成功到第二个页面? 我已经创建了一个新的cocoa touch类文件作为MapViewController.Swift-我只是在登录函数的末尾调用map函数